Problem 30

completed December 5, 2011

Comments

The bug I had to fix in this one was that I had the if statement in the wrong place. It needed to be evaluated at the end of running through all digits in the number, rather than after evaluating each digit. Noticing the difference it makes to indent the if statement one level.

Code

import re
def num2str(n):
	return re.split('_', re.sub('\B', '_', str(n)))

sum = 0
for n in range(2, 10**6):
	power = 0
	for i in num2str(n):
		power = int(i)**5 + power
	if n == power: 
		sum = sum + n
		print n, sum
		
print '=', sum