Question
A googol (10^{100}) is a massive number: one followed by one-hundred zeros; 100^{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, ab, where a, b < 100, what is the maximum digital sum?
Haskell
digits :: Integer -> [Integer]
0 = []
digits = r : digits q
digits n where (q, r) = quotRem n 10
main :: IO ()
= print $ maximum [sum $ digits $ a^b | a <- [1..99], b <- [1..99]] main
$ ghc -O2 -o digit-sum digit-sum.hs
$ time ./digit-sum
real 0m0.066s
user 0m0.066s
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():
= 0
largest for a, b in product(list(range(1, 100)), list(range(1, 100))):
= sum(get_digits(a ** b))
s if s > largest:
= s
largest print(largest)
if __name__ == "__main__":
main()
$ time python3 digital-sum.py
real 0m0.225s
user 0m0.224s
sys 0m0.000s