This week's giveaway is in the Android forum. We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line! See this thread for details.

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?

here is my code so far

this seems to work for the example(2^15) but not for the actual problem(2^1000)
my first system.out.println is equal to what i get from the windows calculator program
1.0715086071862673E301
then i cast it to a long and get this
9223372036854775807
which looks reasonable but gives the wrong answer. i am thinking the number is even too big for a long.
i am not sure this is even the best way to approach this problem. changing from a double to a long to a string and then back to a long then back to a string.
any help or observation appreciated.

thanks for the answer. that is what i thought. i will try BigInteger since i also need to learn that class better for something else. i still suspect there is a better way than changing it to a string but it was all i could think of.

My question is, how many bits long BigInteger is? Because whatever the datatype we choose, should be able to CONTAIN the final result. String can contain it but you can not do math operation on it.

Also somehow I feel shift operators might be of some help here. Not sure if this information is useful but also keep in mind that, 2^15 = 2^10 * 2^5. (Distribution of power)

One thing to slightly simplify it: you need BigInteger to contain 2^1000, but you don't need it to contain the sum of the digits. And you can convert a digit character to the value of the digit by simply subtracting '0'.

Mathew, exactly, but i didn't want to post the solution.

i dont see how i can solve it without BigInteger. however, many people have solved it in many other languages. now that i have solved it i can see their solutions