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 ?
You can do cool stuff with the BigInteger class. Makes this one easy.
import java.math.BigInteger;
public class Euler16
{
public static void main(String[] args)
{
System.out.print(“Problem 16:\n”);
Euler16 e = new Euler16();
System.out.print(“Answer = ” + e.Problem()+ “\n”);
}
public String Problem ()
{
BigInteger bigSucker = new BigInteger(“2”);
String longText = String.valueOf(bigSucker.pow(1000));
int total = 0;
for (int i = 0; i <= longText.length()-1; i++)
{
total +=Integer.parseInt(String.valueOf(longText.charAt(i)));
}
return String.valueOf(total);
}}