2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
well, i dont think there will be any data type which will hold the value of 2^1000
i calculated the value using google and found that
2^ 1000 = 1.07150861e301
EDIT: If you want to store it, create your own data type. Make a structure that contains multiple integers for parts of the integer, but raised to a different power of ten.
If you want to sum its digits without storing, that's a different story, and a different calculation altogether. Make a special request if you want me to give a very generic solution to that problem.
@Albatross
will u tell me how would i do that by giving a simple example on how to compute it, divide the number into parts and store it ?
please also tell me how to compute the sum without knowing the exact value.
You'll need the exact value in order to compute the sum.
Just keep an array of integers where each element stores one digit of the result. Eg, to represent "128" in this
way, you'd have an array of 3 integers:
int array[3] = { 1, 2, 8 };
Now write a function that multiplies the number stored in the array by 2 (multiply it in place).
I did it too! ^^ Took me 15 ms to calculate 2^1000 and sum the digits and less than 1 ms to calculate 256^125 (which is the same) and find the sum :P This isn't an assignment right? I mean I can't imagine his teacher saying: "I want you to solve the 20 first problems from project euler within the next week!". So why don't we give him some more help?
I'll go with jsmith's method but I'll use a vector of ints to store the digits coz during the multiplication you may need to add more digits (or you could just use an array of 350 digits (ints), more than you'll need for this problem in any step of the solution...). Consider the following:
x=(abc)10
That is x=100*a + 10*b + c
Now, 2*x=100*2*a + 10*2*b + 2*c=1000*d' + 100*a' + 10*b' +c'
(note that we may have an extra digit here)
You can calculate the new values for the digits like this:
c'=2*c
b'=2*b+c'/10
a'=2*a+b'/10
d'=a'/10
c'%=10
b'%=10
a'%=10
Note that you can easily modify this to cover for multiplication with any given k (instead of just 2). But then you'll have to be a little careful coz you may have more than one new digits at a time... If you do this using a vector put the least significant digit in the beginning of the vector so that you can simply push_back new digits when necessary.
HINT: The result is LEET increased a bit! LOL, that made a rhyme, neat! LOL, that also made a rhyme! (Ok, I'll just stop here...)