I am trying to figure out how to solve this problem (Project Euler):
n! means n × (n − 1) × ... × 3 × 2 × 1
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of
the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
Find the sum of the digits in the number 100!
Using BigInt is not an option, I am trying to figure out how to implement a solution using only c++.
I thought maybe splitting the big numbers to an array of about 7 digit's long or something like that and then deal with them but i still cant figure out how to do this either..
thanks in advance!
Try this
#include "iostream"
#include "vector"
int n,remainder,sum;
int main ()
{
std::vector <int> digits(5000);
std::cin>>n;
digits[0]=1;
digits[1]=1;
for (int k=2;k<n+1;k++) {
for (int i=1;i<=digits[0];i++) {
digits[i]=digits[i]*k+remainder;
remainder=0;
if (digits[i]>9) {
remainder=digits[i]/10;
digits[i]%=10;
if (i==digits[0])
digits[0]++;
}
}
}
for (int i=digits[0];i>=1;i--)
sum+=digits[i];
std::cout<<sum;
}
Related
My program is to sum an arithmetic sequence from 1 to n like
-1 2 -3 4 -5 6 -7 etc...
But
n (1 ≤ n ≤ 10^15).
And when I execute this program, I am faced with that the sum variable is overflowing although I use data type long long
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
long long n;
cin >>n;
if (n%2!=0)
{
long long sump = ((n-1)/4.0) * (2+(n-1));
long long sumn = ((n+1)/4.0) * (-1-n);
long long sum = sump + sumn;
cout << sum << endl;
}
else
{
long long sump = ((n)/4.0) * (2+(n));
long long sumn = ((n)/4.0) * (-1-(n-1));
long long sum = sump + sumn;
cout << sum << endl;
}
}
If you see such big numbers in questions, then most often you cannot solve that by brute force, running big loops and the like.
You need to find an analytical or mathematical solution.
Even, if you do not know the solution, you may look at example values.
If you look at your series, then you can see something like the below:
Index Sign Series Sum
0 1 0 0
1 -1 -1 -1
2 1 2 1
3 -1 -3 -2
4 1 4 2
5 -1 -5 -3
6 1 6 3
7 -1 -7 -4
8 1 8 4
9 -1 -9 -5
10 1 10 5
As you can see,
if n is even, then the result is n/2
if n is odd then the result is -((n/2)+1)
And this can be easily implemented . . .
Here is the link to the question. Essentially, it asks to find the kth number having digit sum as 10. I have tried multiple solutions and also looked upon solutions online. Specifically this one (also shared below). The one with constant time talks about outliers in Arithmetic Progression and uses it to find the nth number having sum as 10. Obviously, the code is incorrect as it fails for test cases when k=1000 etc.
#include <bits/stdc++.h>
using namespace std;
int findNth(int n)
{
int nthElement = 19 + (n - 1) * 9;
int outliersCount = (int)log10(nthElement) - 1;
// find the nth perfect number
nthElement += 9 * outliersCount;
return nthElement;
}
int main()
{
cout << findNth(5) << endl;
return 0;
}
Eventually, I ended up writing combination of Arithmetic Progression + brute force as below
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
int main() {
int n;
cin >> n;
int count = 0;
ll i = 19;
for (; ; i += 9) {
int curr = i;
int localSum = 0;
while (curr) {
localSum += curr%10;
curr /= 10;
}
if (localSum == 10) {
count += 1;
}
if (count == n) {
break;
}
}
cout << i << endl;
return 0;
}
I am wondering, if there is no constant time or better algorithm that does not require me to calculate the sum, but my algorithm always hops in a way that I have number whose digit sum is 10?
Here is a Python solution that you can translate into C++.
cached_count_ds_l = {}
def count_digit_sum_length (s, l):
k = (s, l)
if k not in cached_count_ds_l:
if l < 2:
if s == 0:
return 1
elif l == 1 and s < 10:
return 1
else:
return 0
else:
ans = 0
for i in range(min(10, s+1)):
ans += count_digit_sum_length(s-i, l-1)
cached_count_ds_l[k] = ans
return cached_count_ds_l[k]
def nth_of_sum (s, n):
l = 0
while count_digit_sum_length(s, l) < n:
l += 1
digits = []
while 0 < l:
for i in range(10):
if count_digit_sum_length(s-i, l-1) < n:
n -= count_digit_sum_length(s-i, l-1)
else:
digits.append(str(i))
s -= i
l -= 1
break
return int("".join(digits))
print(nth_of_sum(10, 1000))
The idea is to use dynamic programming to find how many numbers there are of a given maximum length with a given digit sum. And then to use that to cross off whole blocks of numbers on the way to finding the right one.
The main logic goes like this:
0 numbers of length 0 sum to 10
- need longer
0 numbers of length 1 sum to 10
- need longer
9 numbers of length 2 sum to 10
- need longer
63 numbers of length 3 sum to 10
- need longer
282 numbers of length 4 sum to 10
- need longer
996 numbers of length 5 sum to 10
- need longer
2997 numbers of length 6 sum to 10
- answer has length 6
Looking for 1000th number of length 6 that sums to 10
- 996 with a leading 0 sum to 10
- Need the 4th past 99999
- 715 with a leading 1 sum to 10
- Have a leading 1
Looking for 4th number of length 5 that sums to 9
- 495 with a leading 0 sum to 9
- Have a leading 10
Looking for 4th number of length 4 that sums to 9
- 220 with a leading 0 sum to 9
- Have a leading 100
Looking for 4th number of length 3 that sums to 9
- 55 with a leading 0 sum to 9
- Have a leading 1000
Looking for 4th number of length 2 that sums to 9
- 1 with a leading 0 sum to 9
- Need the 3rd past 9
- 1 with a leading 1 sum to 9
- Need the 2nd past 19
- 1 with a leading 2 sum to 9
- Need the 1st past 29
- 1 with a leading 3 sum to 9
- Have a leading 10003
Looking for 1st number of length 1 that sums to 6
- 0 with a leading 0 sum to 6
- Need the 1st past 0
- 0 with a leading 1 sum to 6
- Need the 1st past 1
- 0 with a leading 2 sum to 6
- Need the 1st past 2
- 0 with a leading 3 sum to 6
- Need the 1st past 3
- 0 with a leading 4 sum to 6
- Need the 1st past 4
- 0 with a leading 5 sum to 6
- Need the 1st past 5
- 1 with a leading 6 sum to 6
- Have a leading 100036
And it finishes in a fraction of a second.
Incidentally the million'th is 20111220000010, the billionth is 10111000000002000000010000002100, and the trillionth is 10000000100000100000100000000000001000000000000100000000010110001000.
This question already has answers here:
What is special about numbers starting with zero?
(4 answers)
Closed 5 years ago.
I recently came across the following when I was testing my code for various value of x.
I will try to illustrate only the issue.
#include <iostream>
int main()
{
int x = 01234;
std:: cout << x ;
return 0;
}
Output:
when x = 1234 , 1234
x = 01234 , 668
x = 001234 , 668
x = 240 , 240
x = 0240 , 160
x = 00240 , 160
For mostly any number starting with 0 I get a different value.
eg: x = 0562 gives 370 and so on.
I tried using various online C++ compilers but all give same output.
I tried to google the issue but couldn't find an appropriate answer.
Looks like you've been hit with an octal literal! Any number literal beginning with just 0 is interpreted in base 8.
01234 = 1 × 8^3 + 2 × 8^2 + 3 × 8^1 + 4 × 8^0
= 1 × 512 + 2 × 64 + 3 × 8 + 4 × 1
= 512 + 128 + 24 + 4
= 668
0240 = 2 × 8^2 + 4 × 8^1 + 0 × 8^0
= 2 × 64 + 4 × 8 + 0 × 1
= 128 + 32
= 160
The number 01234 is in octal (base 8) when you prepend a 0 you define the number as an octal. When you then print it in decimal you get it's decimal equivalent
I have question that asks how values such as c are computed in terms of binary numbers. Im researching it but now but figured id ask here if anyone has somewhere they can send me or explain how this works.
int main()
{
int a 10, int b = 12, int c, int d;
int c = a << 2; //output 40
}
Well, I'm not answering with C++ code, as the question is not really related to the language.
The integer ten is written 10 in base 10 as it's equal to 1 * 10^1 + 0 * 10^0.
Binary is base 2, so let's try to write ten as a sum of powers of 2.
10 = 8 + 2
That is 2^3 + 2^1.
Let's switch to binary (using only two digits : 0 and 1).
2^3 is written 1000
2^1 is written 10
Their sum is 1010 in binary.
"<<" is the operation that shift left binary digits by a certain amount (beware of overflow).
So 1010 << 2 is 101000
That is in decimal 2^5 + 2^3 = 32 + 8 = 40
You can also think of "<< N" as being a multiplication by 2^N of an integer.
My challenge is to output the total number of five-digit numbers that have a digit 5, but no digits 8. My only two answers so far have been 0, or 90000. Can anyone help me?
#include <iostream>
using namespace std;
int main() {
int number;
int counter=10000;
int ncounter=0;
while (counter >= 10000 && counter <= 99999) {
int n1,n2,n3,n4,n5;
counter = counter + 1;
n1 = number%10;
number /= 10;
n2 = number%10;
number /= 10;
n3 = number%10;
number /= 10;
n4 = number%10;
number /=10;
n5 = number%10;
number /= 10;
if (n1 == 5||n2 == 5||n3 == 5||n4 == 5||n5 == 5)
if (n1!=8)
if (n2!=8)
if (n3!=8)
if(n4!=8)
if (n5!=8)
ncounter=ncounter+1;
}
cout<<ncounter<<endl;
return 0;
}
(num with 5 but not 8) = (num without 8) - (num with neither 8 nor 5) = 8*9*9*9*9 - 7*8*8*8*8= 23816
Each number is a selection of 5 digits (with repetitions).
Since you cannot select the digit 8, you have 9 possible digits, so this problem is equivalent to the same problem, base 9 (instead of base 10).
If you make 1 digit a 5, there are 4 non-5 and non-8 digits remaining. The number of these can be calculated as 8^4 (because there are 8 available digits to choose from, and you need to choose 4 of these). With a single 5, there are 5 ways to position the 5, so multiply by 5.
Similarly with 2 5's, there are 10 ways to position the 5s relative to other digits.
Therefore, we have the following table:
number of digits==5 remaining digits ways to position 5s
1 8^4 5
2 8^3 10 = 5*4/2
3 8^2 10
4 8^1 5
5 8^0 1
There are 5*8^4 + 10*8^3 + 10*8^2 + 5*8^1 + 8^0 = 26281 numbers <10^5 with a 5 but not an 8.
There are 4*8^3 + 6*8^2 + 4*8^1 + 8^0 = 2465 numbers <10^4 with a 5 but not an 8. Therefore, there are 23816 numbers satisfying your criteria.
This is actually a mathematical problem. Here we have three conditions:
First digit is not zero, as it should be a five-digit number.
No digits are 8
One or more digits are 5
There can be numbers with one to five 5s, where first digit is or is not 5 (except for 55555). That's nine cases to count.
If first digit is not 5, it has 7 options: [1234679]; if any other digit is not 5, it has 8 options: [12346790].
Here C(5) is number of combinations to place 5s, and C(o) - to place other digits.
N(5). 1st? C(5) C(o)
1 Y 1 * 8^4
1 N 4 * 7*8^3
2 Y 4 * 8^3
2 N 6 * 7*8^2
3 Y 6 * 8^2
3 N 4 * 7*8
4 Y 4 * 8
4 N 1 * 7
5 Y 1
Sum: 23816