27 lines
724 B
Haskell

-- 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
--
-- Find the sum of all numbers which are equal to the sum of the factorial of their digits.
--
-- Note: as 1! = 1 and 2! = 2 are not sums they are not included.
import Data.Char
factorial :: (Integral a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n - 1)
factDigits :: [Int]
factDigits = map factorial [0 .. 9]
equalsDigitFactorial :: Int -> Bool
equalsDigitFactorial n =
let fact_digit_sum = sum $ [factDigits !! x | x <- map digitToInt (show n)]
in fact_digit_sum == n
main = do
let result = sum $ filter equalsDigitFactorial [10 .. 9999999]
putStrLn $
"Project Euler, Problem 34\n"
++ "Answer: "
++ show result