Back to Project Euler

Previous Problem Next Problem

Problem 16: Power Digit Sum

\(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}\)?

Solution

Once again, Python's code types save us from annoyingly complicated programming. The C-based solutions in the thread all seem to manually implement large integers, but Python has those built in, and I see no reason not to make full use of them!

Python Code

def p16(b=2, p=1000):
	n, res = b ** p, 0
	while n > 0:
		res += n % 10
		n //= 10
	return res

Or, alternately, an approach that relies on string conversion but takes only a single line:

p16 = lambda b=2, p=1000: sum(map(int, str(b ** p)))

Previous Problem Next Problem

Back to Project Euler