### calculating 2^1000 and its sum of the digits

Hi, i have an exercise from eulers 9A where I have to compute 2^1000 and find the sum of its integer. How do I do it?

My first intake is this:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````// this is the pseudocode copied from my function (defined): // I want to store numbers in an array with mod of 0x7fffffffffffffff mutable deque vec; void power2(int x) { vec.push_back(0); for (int i = 1; i <= x; i += 64) vec.push_back(0); int i = 1; unsigned long long mod = 0x7fffffffffffffff; while (i < 64 && i < x) { vec[1] = (unsigned long long)pow(2, i); i++; } if (i >= 64) { int carry; vec[2] = vec[1] % mod; vec[1] /= mod; for (; i < x; i++) { vec[floor(i / 64)] = (unsigned long long)pow(2, i%64); if (vec[floor(i / 64)] == mod) { carry = vec[floor(i / 64)] % mod; vec[floor(i / 64)] /= mod; while (carry > 0) { vec[floor(i / 64) + 1] = carry; carry--; } } } } } // THis is how I sum the digits: void digitsum(int x = 1) { if (!vec.empty()) { for (int i = 1; i < vec.size(); i++) { while (vec[i] != 0) { vec[0] += vec[i] % 10; vec[i] /= 10; } } } else { vec.clear(); vec.push_back(0); while (x != 0) { vec[0] += (x % 10); x /= 10; } } } // Calling function. void power2sum(int x) { vec.clear(); power2(x); digitsum(x); } ``````

I've tried to debug but I couldn't find the solution to calculate big integers.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990`` ``````#include #include #include using namespace std; const int MOD = 1000000000; const int WMOD = 9; unsigned long long sumDigits( unsigned long long n ) { return n < 10 ? n : sumDigits( n / 10 ) + n % 10; } //===================================================================== class BigInt { public: vector digits; // digits stored in REVERSE order; BigInt( unsigned long long n = 0 ); // construct from integer }; //------------------ BigInt::BigInt( unsigned long long n ) { digits.clear(); if ( n == 0 ) { digits.push_back( 0 ); } else { while ( n ) { digits.push_back( n % MOD ); n /= MOD; } } } //------------------ BigInt operator * ( const BigInt &a, const BigInt &b ) { BigInt result = 0; int asize = a.digits.size(); int bsize = b.digits.size(); for ( int i = 0; i < asize; i++ ) // Multiply by the ith digit of a and i tens { unsigned long long carry = 0; for ( int j = 0; j < bsize || carry > 0; j++ ) { if ( i + j < result.digits.size() ) carry += result.digits[i+j]; if ( j < bsize ) carry += a.digits[i] * b.digits[j]; int digit = carry % MOD; carry /= MOD; if ( i + j < result.digits.size() ) result.digits[i+j] = digit; else result.digits.push_back( digit ); } } return result; } //------------------ ostream &operator << ( ostream &out, const BigInt &a ) { out << a.digits.back(); for ( int i = a.digits.size() - 2; i >= 0; i-- ) out << setw( WMOD ) << setfill( '0' ) << a.digits[i]; return out; } //====================================================================== int main() { BigInt Two50 = 1ull << 50; BigInt result = Two50; for ( int n = 2; n <= 20; n++ ) result = result * Two50; unsigned long long sumd = 0; for ( auto d : result.digits ) sumd += sumDigits( d ); cout << "2^1000 = " << result << '\n'; cout << "The sum of its digits = " << sumd << '\n'; }``````

 ```2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376 The sum of its digits = 1366```

Cheers Ganado! I obviously had lots of fun the last time I had a go at that challenge, too!
BOI, i wasn't expected that....