Euler Solution 56

From ProgSoc Wiki

Jump to: navigation, search

Solutions for Problem 56

What is the largest sum of the digits of numbers given by ab for 0<a,b<101.

Haskell by SanguineV

Runtime: 843.28 ms (on aglaope)

{- Calculate the sum of the digits of an integer -}
sumDigs :: Integer -> Integer
sumDigs n | n < 10 = n
sumDigs n = (mod n 10) + (sumDigs (div n 10))

{- Find the power of n^p, avoids Haskell type issues by doing myself -}
pow :: Integer -> Integer -> Integer
pow 1 p = 1
pow n 1 = n
pow n p = n * (pow n (p - 1))

{- The fun bit, fold over the fold of the map of the sum of the digits of a^b -}
main = print (foldl max 0 
               (map (\z -> 
                 (foldl (\x y -> max x (sumDigs (pow y z)))
                 0 [1..100])) [1..100]))

Anothe brute force solution... (un)fortunately these don't work for the higher level problems.

Python by Althalus

Runtime: 788 ms

def getDigitSum(n):
        n = str(n)
        return sum([int(i) for i in n])

answer = 0
for a in range(1,100):
        for b in range(1,100):
                digit_sum = getDigitSum(a**b)
                if digit_sum > answer: answer = digit_sum

print answer
Personal tools