Project Euler: Problem 16 – Power digit sum

Problem 16:

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
 
What is the sum of the digits of the number 21000?

Idea:

Another problem made trivial by the BigInteger class.

int answer = 0;

BigInteger number = new BigInteger("2");
number = number.pow(1000);

String tmp = number.toString();
for(int i = 0; i < tmp.length(); i++) {
   answer += Integer.parseInt(""+tmp.charAt(i));
}

System.out.println("Answer: "+answer);
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *