C# Implementation of Problem 16

View source code here on GitHub!

Problem Solution

class p0016
: Euler.IEuler
object Answer ()
 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*/
10using System;
11
12namespace Euler
13{
14    public class p0016 : IEuler
15    {
16        public object Answer()
17        {
18            ulong[] numbers = new ulong[18];
19            const ulong ten17 = 100000000000000000;
20            numbers[0] = 1;
21            for (ushort i = 0; i < 1000; i++)
22            {
23                for (byte j = 0; j < 18; j++)
24                    numbers[j] *= 2;
25                for (byte j = 0; j < 17; j++)
26                    if (numbers[j] > ten17)
27                    {
28                        numbers[j + 1] += numbers[j] / ten17;
29                        numbers[j] %= ten17;
30                    }
31            }
32            ulong answer = 0;
33            ulong power = 1;
34            for (byte i = 0; i < 19; i++)
35            {
36                for (byte j = 0; j < 18; j++)
37                    answer += (numbers[j] / power) % 10;
38                power *= 10;
39            }
40            return (short)answer;
41        }
42    }
43}

Tags: large-numbers, digit-sum, power