Sunday, 21th February, 2010

Project Euler Problem 56

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.

Considering natural numbers of the form, ab, where a, b <=100, what is the maximum digital sum?

Noting that Python has pretty swift large number handling, and built in sets, this is one of the easier ones to bash out. Seriously quick and hacky:

exponents = set()
# Generate a set of all the exponents
for a in xrange(101):
    for b in xrange(101):
        exponents.add(a**b)  
	
max = 0
# Find the maximum sum of the digits.
for exp in exponents:
    sumi = sum([int(x) for x in str(exp)]) # sum of digits
    if sumi > max:
        max = sumi
	
print max

It’s not pretty, it’s not quick, and there’s very little actual maths behind it, but it works in an acceptable time.

tuna@Pythagoras:~/Code/Python/Euler$ time python euler56.py 
XXXXXX
	
real	0m0.680s
user	0m0.604s
sys	0m0.004s

Done! :-D