# HINT for summing digits of a large power

I recently started working through the Project Euler challenges, but I've got stuck on #16 (http://projecteuler.net/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}$?

(since I'm a big fan of generality, my interpretation is to find a solution to the sum of digits of $a^b$ in base $c$, and obviously I'm trying to solve it without resorting to "cheats" like arbitrary-precision numbers).

I guess this is simpler than I'm making it, but I've got no interest in being told the answer so I haven't been able to do a lot of internet searching (too many places just give these things away). So I'd appreciate a hint in the right direction.

I know that $2^{1000} = 2^{2*2*2*5*5*5} = (((((2^2)^2)^2)^5)^5)^5$, and that the repeated sum of digits of powers of 2 follows the pattern $2, 4, 8, 7, 5, 1$, and that the last digit can be determined by an efficient pow-mod algorithm (which I already have from an earlier challenge), but I haven't been able to get further than that… (and I'm not even sure that those are relevant).

-
(I'm not sure about the relevant tags so please feel free to alter them!) – Dave May 19 '13 at 15:41
That is solved by $92093$, just hope that we have at least one user on this who have solved it with a proof. – Inceptio May 19 '13 at 15:53
@Inceptio I expect the vast majority of those users used Java's built-in BigInteger to do this without any thought, but I consider that cheating. – Dave May 19 '13 at 15:54
You are supposed to use $\mod 9$, since it uses the digit sum. – Inceptio May 19 '13 at 15:57
@Inceptio won't that give the repeated sum? This is just looking for the sum. – Dave May 19 '13 at 15:59

In this case, I'm afraid you just have to go ahead and calculate $2^{1000}$. There are various clever ways to do this, but for numbers this small a simple algorithm is fast enough.

The very simplest is to work in base 10 from the start. Faster is to work in binary, and convert to base 10 at the end, but this conversion is not completely trivial. A compromise (on a 32-bit machine) would be to work in base 1000000000, so that you can double a number without overflow. Then at the end the conversion to base 10 is much simpler.

-
really? That doesn't seem to be in the spirit of Project Euler (although #13 effectively asks for an arbitrary-precision number implementation)… I expected there was a mathematical trick to simplify the calculation. – Dave May 19 '13 at 15:53
If you find one, let me know! (Just don't waste too much of your life looking for it.) – TonyK May 19 '13 at 15:55
Well I'll leave this a bit to see if anybody can suggest anything, but if not I'll have to resign myself to calculating the actual number (and I'll accept your answer). – Dave May 19 '13 at 15:57
Sometimes the truth can be painful... – TonyK May 19 '13 at 15:57
@TonyK: Hope is not a bad thing. – Inceptio May 19 '13 at 15:58

It's not the most honorable solution, but in C# you can use the BigInteger class....

 var powerDigits = BigInteger.Pow(2, 1000).ToString();
var sum = 0;
for (var i = 0; i < powerDigits.Length; i++)
sum += (int) Char.GetNumericValue(powerDigits[i]);
Console.WriteLine(sum); //1366

-
I said in the comments, "I expect the vast majority of those users used Java's built-in BigInteger to do this without any thought, but I consider that cheating", which is the same thing just in a different language. Anyway, in the end I just wrote my own bigint-like methods (writing it from scratch at least maintains some of the challenge) – Dave Jan 5 at 0:42