Java Implementation of Problem 16

View source code here on GitHub!

public class p0016 implements IEuler
Object answer()
Returns:

The answer to Project Euler problem 16

 1/*
 2Project Euler Problem 16
 3
 4Problem:
 5
 62**15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
 7
 8What is the sum of the digits of the number 2**1000?
 9*/
10package euler;
11
12public class p0016 implements IEuler {
13    @Override
14    public Object answer() {
15        long[] numbers = new long[18];
16        long ten17 = 100000000000000000L;
17        numbers[0] = 1;
18        for (short i = 0; i < 1000; i++) {
19            for (byte j = 0; j < 18; j++)
20                numbers[j] *= 2;
21            for (byte j = 0; j < 17; j++)
22                if (numbers[j] > ten17) {
23                    numbers[j + 1] += numbers[j] / ten17;
24                    numbers[j] %= ten17;
25                }
26        }
27        long answer = 0;
28        long power = 1;
29        for (byte i = 0; i < 19; i++) {
30            for (byte j = 0; j < 18; j++)
31                answer += (numbers[j] / power) % 10;
32            power *= 10;
33        }
34        return (short) answer;
35    }
36}

Tags: large-numbers, digit-sum, power