This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori 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
then i cast it to a long and get this
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.
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'.