8

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).

Sil
  • 17,976
Dave
  • 243
  • (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
  • 1
    @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
  • 1
    @Inceptio won't that give the repeated sum? This is just looking for the sum. – Dave May 19 '13 at 15:59
  • @Dave: Yes, you are right. – TonyK May 19 '13 at 16:00
  • Just do it with Python. Python doesn't convert large integers to scientific notation. It just shows the integer as it is no matter the size. From that point, I assume you can figure out the rest. ;=) – Rodrigo Salinas Ojeda Aug 10 '18 at 18:48
  • 1
    The sum of the digits of $2^{1000}$ is twice the sum of digits of $2^{999}$ minus 9 times the number of carries. Is there a way to find out how many digits of $2^{999}$ are $≥5$ without actually calculating $2^{999}$? – Noel Yap Aug 06 '20 at 19:12

4 Answers4

2

Just note that no problem in Project Euler requires you to use bigger numbers than $2^{64}-1$ (or $2^{63}-1$ for signed). However sometimes it is just too painful to avoid big numbers, so it is just easier to use them.

That being said, I don't think there is a mathematical trick for this problem, although I think the problem really just asks you to implement your own multiplication in terms of base $10$ digits. You know already how to do that on paper! Try $2\cdot 2$, $4 \cdot 2$, $8 \cdot 2$, $\dots$ by hand, notice how digits are carried over. Try to formalize this into an algorithm that uses array for digits. Test it for small values, then use it for $2^{1000}$ (just repeat $1000$ times). Finally it is only about the sum.

It might feel a bit unsatisfactory that all you are required to do is multiplication, but hey, it is just $16$-th problem yet!

Sil
  • 17,976
2

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.

TonyK
  • 68,059
  • 2
    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
  • 1
    @TonyK: Hope is not a bad thing. – Inceptio May 19 '13 at 15:58
  • 2^1000 is a huge number to calculate – blade Dec 02 '18 at 03:23
  • @blade: well, I can do it in my head, as long as you want the answer in binary. Or hexadecimal. Now you just have to convert it :-) – TonyK Dec 02 '18 at 03:35
0

Spoiler alert

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
  • 3
    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 05 '16 at 00:42
  • Python has built-in BigInt behavior. Here is a Python gist. – Galen Oct 13 '22 at 21:14
0

Here are 2 hints that really helped me along to get to the solution:

1)

"We can use an array, or hash map, to store the digits of any big number. Every time we multiply each position (the values) of the array/hash map by two, we need to start from the ‘One’ position to carry over the digits.

With this mechanism, we can multiply 1000 times 2 quite quickly. We don’t even need 1000 arrays – one array is enough when we sum the result directly."

Source: https://www.educative.io/edpresso/what-is-the-power-digit-sum

You may want to stop reading that page before looking at the implementation code towards the bottom of that page.

2.

"Consider an array of ints with the digits, and repeatedly multiply by 2."

Source: https://www.hackerrank.com/contests/projecteuler/challenges/euler016/forum/comments/189057

This one isn't extremely insightful, but it did help light a lightbulb in my head. To go a bit further (and without revealing everything), how can you cache results as you "repeatedly multiply by 2"?

Edit: Note that my comment #2 is a rewording of Sil's answer (https://math.stackexchange.com/a/2879132/162331) above, particularly "Try 2⋅2, 4⋅2, 8⋅2, … by hand, notice how digits are carried over. Try to formalize this into an algorithm that uses array for digits.".

ximiki
  • 142