euler 30 sums of powers of digits

Feb 18, 2015  

Another quick and easy solution. This doesn’t mean it’s an easy problem!; no, it’s more like playing a level at the rookie level. The hard and “correct” solution would be to work it out on pen-and-paper using Number Theory.

Statutory Warning: Spoilers ahead

digits :: Int -> [Int]
digits n = digitsHelper n []
  where
    digitsHelper num dList =
      if num > 0
      then digitsHelper (div num 10) $ (mod num 10) : dList
      else dList

sumFifthPow :: Int -> Int
sumFifthPow n = sum $ map (^ 5) $ digits n

euler30 :: Int -> Int
euler30 limit = sum $ [x | x <- [2 .. limit], x == sumFifthPow x]

Again, this version needs “manual intervention”, since I used the rough approximation that if euler30 1000000 and euler30 10000000 gave the same result, this was probably the right answer. It was.