I am stuck with a unique problem.
We have to find the sum of digits in 2^1000.
The code below is giving correct output.
However when I change the value of digit from 1000 to anything <=604,
It shows wrong output.
The number of digit in 2^1000 is 302 and sum of digit is 1366.
Also note that 604=2*302,
so is there any relation??
Please solve my problem..
#include<iostream>
using namespace std;
#define digit 1000
int main()
{
int num[digit]={0},i,j=0,ctr,carry,length=1;
num[digit-1]=1;
while (j<1000)
{ carry=0;
for (i=0;i<length;++i)
{ ctr=num[digit-1-i]*2;
num[digit-1-i]=(carry+ctr)%10;
carry=(carry+ctr)/10;
if (i==length-1&&carry!=0)
{ ++length;
num[length]=carry;
}
}
++j;
}
int sum=0;
for (i=0;i<length;++i)
sum+=num[digit-1-i];
cout<<sum;
return 0;
}