Project-Euler / 16

Problem 16 asks:

What is the sum of the digits of the number 21000?

Very easy in Java thanks to BigInteger:

import java.math.BigInteger;

public class Problem16 {
    public static void main(String[] args) {
        BigInteger number = new BigInteger("2").pow(1000);
        int sum = 0;
        while (number.compareTo(BigInteger.ZERO) > 0) {
            sum += number.remainder(BigInteger.TEN).intValue();
            number = number.divide(BigInteger.TEN);
        }
        System.out.println(sum);
    }
}

Last updated on .