16. Sum of 2^1000 Digits

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?

import java.math.BigInteger;
public class Problem16 {
    public void sumofdigits() {
        final String bignumber = BigInteger.valueOf(2).pow(1000).toString(10);
        long result = 0;
        for (int i = 0; i < bignumber.length(); i++) {
                result += Integer.valueOf(String.valueOf(bignumber.charAt(i)));
        }
        System.out.println(bignumber);
    }

    public static void main(String[] args) {
        Problem16 p = new Problem16();
        p.sumofdigits();
    }
}
Advertisement

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

%d bloggers like this: