diff --git a/Haskell/ProjectEuler.hs b/Haskell/ProjectEuler.hs index f4b6d29..e3e2b69 100644 --- a/Haskell/ProjectEuler.hs +++ b/Haskell/ProjectEuler.hs @@ -1,7 +1,10 @@ module ProjectEuler ( isPrime +, digitSum ) where +import Data.Char (digitToInt) + isPrime :: (Integral n) => n -> Bool isPrime 1 = False isPrime 2 = True @@ -10,3 +13,8 @@ isPrime n = n `mod` 2 /= 0 && n `mod` 3 /= 0 && null [ x | x <- candidates, n `mod` x == 0 || n `mod` (x+2) == 0 ] where candidates = [5,11..limit] limit = floor(sqrt(fromIntegral n)) + 1 + + +digitSum :: (Integral a, Show a) => a -> Int +digitSum n = sum $ map digitToInt $ show n + diff --git a/Haskell/p020.hs b/Haskell/p020.hs index c83df31..b7bcccc 100644 --- a/Haskell/p020.hs +++ b/Haskell/p020.hs @@ -13,5 +13,5 @@ factorial n = n * factorial (n - 1) main = do let result = digitSum $ factorial 100 - putStrLn $ "Porject Euler, Problem 20\n" + putStrLn $ "Project Euler, Problem 20\n" ++ "Answer: " ++ (show result)