Hello everyone, Im in my 2nd course for programming and learning c++, for a final I was tasked with solving a euler problem from hackerrank. I have code done but when submitted, it only passes 1 of the 10 cases in hackerrank. I need pass them all or atleast most of them. Future thanks for any help.
(2^10000)^2 = 2^20000, which is a number with over 6000 decimal digits, and most computers aren't able to handle directly integers with more than 20 digits, so you'll need to figure out some way to add the three least significant decimal digits that doesn't involve putting that number into an int.
She's right! FWIW that's what 2^20,000 looks like. Somebody can do the digit count but all we need know is that C++ doesn't handle integers anywhere near that size. Python does! But that doesn't help if C++ is mandatory.
So it is fair to say if you need to handle numbers that big, let alone square numbers on the way, you need to re-think your program.
A number is an array of digits, or even a <vector> etc of them and that's probably where you need to focus. Remember also if you take two numbers in the array and process them (+, -, * or divide) you will have the last digit plus a carryover.
Of course, int square = pow(base,digit); isn't the square of base, unless digit is always 2. Besides, pow() returns a floating value not an integer. See: http://www.cplusplus.com/reference/cmath/pow/
Maybe if you copy the question, or the Euler problem number being addressed a more definitive answer can be given.
(For instance
Euler Problem 16 is:
2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
What is the sum of the digits of the number 2^1000?
)
Thank you guys for your replies, I see, so int is not the datatype to which to handle the square of the bases. Float should be used I assume. Also yes, I forgot to include the Euler problem.
Float won't handle all those digits either. [Edit: see dutch's comment below] You need to come up with an algorithm that doesn't require storing all the digits. Hint: When multiplying AxB, if you only want to know the least significant 3 digits then you only have to multiply the 3 least significant digits of A and B.
The question is "What is the sum of the digits of the number 2**N for N from 1 to 10000?"
It doesn't mention the "least significant 3 digits" that I can see.
#include <iostream>
#include <vector>
usingnamespace std;
void doubleIt( vector<int> &number )
{
int N = number.size();
for ( int &i : number ) i += i; // double digits
for ( int i = 0; i < N - 1; i++ ) // do the "carries" (maximum of 1 for doubling)
{
if ( number[i] > 9 )
{
number[i] -= 10;
number[i+1]++;
}
}
if ( number[N-1] > 9 )
{
number[N-1] -= 10;
number.push_back( 1 );
}
}
void writeIt( const vector<int> &number )
{
int N = number.size();
for ( int i = N - 1; i >= 0; i-- ) cout << number[i];
}
int sumIt( const vector<int> &number )
{
int sum = 0;
for ( int i : number ) sum += i;
return sum;
}
int main()
{
/*
vector<int> A = { 1 };
int M = 20;
for ( int i = 1; i <= M; i++ ) // First M powers of 2 as a check
{
doubleIt( A );
cout << "2^" << i << " = "; writeIt( A ); cout << '\n';
}
*/
// Business
int N = 10000;
vector<int> B = { 1 };
vector<int> SUMS( 1 + N ); SUMS[0] = 1;
for ( int i = 1; i <= N; i++ )
{
doubleIt( B );
SUMS[i] = sumIt( B );
}
cout << "2^" << N << " = "; writeIt( B ); cout << '\n';
cout << "The sum of the digits in 2^" << N << " is " << SUMS[N] << '\n';
}
The question is "What is the sum of the digits of the number 2**N for N from 1 to 10000?"
It doesn't mention the "least significant 3 digits" that I can see.
I don't know, I haven't seen a link to the actual question yet.