Project Euler Problem 56 Solution

Question

A googol (1010010^{100}) is a massive number: one followed by one-hundred zeros; 100100100^{100} 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, abab, where a,b<100a, b < 100, what is the maximum digital sum?

Haskell

digits :: Integer -> [Integer]
digits 0 = []
digits n = r : digits q
    where (q, r) = quotRem n 10

main :: IO ()
main = print $ maximum [sum $ digits $ a^b | a <- [1..99], b <- [1..99]]
$ ghc -O2 -o digit-sum digit-sum.hs
$ time ./digit-sum
real   0m0.084s
user   0m0.080s
sys    0m0.000s

Python

#!/usr/bin/env python
from itertools import *

def get_digits(n):
    return [int(digit) for digit in str(n)]

def main():
    largest = 0
    for a, b in product(list(range(1, 100)), list(range(1, 100))):
        s = sum(get_digits(a ** b))
        if s > largest:
            largest = s
    print(largest)

if __name__ == "__main__":
    main()
$ time python3 digital-sum.py
real   0m0.200s
user   0m0.196s
sys    0m0.000s