Euler Problem 16

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);
    }

}



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s