From 91a8d44801ddaba325399435117385c62126a03a Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Thu, 21 Nov 2024 22:00:18 +0100 Subject: [PATCH] Add Haskell solution for Problem 34 --- Haskell/p034.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Haskell/p034.hs diff --git a/Haskell/p034.hs b/Haskell/p034.hs new file mode 100644 index 0000000..89544f4 --- /dev/null +++ b/Haskell/p034.hs @@ -0,0 +1,23 @@ +-- 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