Fix typo and update ProjectEuler library
This commit is contained in:
parent
2b8c7e0bce
commit
4245a2d19b
@ -1,7 +1,10 @@
|
|||||||
module ProjectEuler
|
module ProjectEuler
|
||||||
( isPrime
|
( isPrime
|
||||||
|
, digitSum
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
import Data.Char (digitToInt)
|
||||||
|
|
||||||
isPrime :: (Integral n) => n -> Bool
|
isPrime :: (Integral n) => n -> Bool
|
||||||
isPrime 1 = False
|
isPrime 1 = False
|
||||||
isPrime 2 = True
|
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 ]
|
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]
|
where candidates = [5,11..limit]
|
||||||
limit = floor(sqrt(fromIntegral n)) + 1
|
limit = floor(sqrt(fromIntegral n)) + 1
|
||||||
|
|
||||||
|
|
||||||
|
digitSum :: (Integral a, Show a) => a -> Int
|
||||||
|
digitSum n = sum $ map digitToInt $ show n
|
||||||
|
|
||||||
|
@ -13,5 +13,5 @@ factorial n = n * factorial (n - 1)
|
|||||||
|
|
||||||
main = do
|
main = do
|
||||||
let result = digitSum $ factorial 100
|
let result = digitSum $ factorial 100
|
||||||
putStrLn $ "Porject Euler, Problem 20\n"
|
putStrLn $ "Project Euler, Problem 20\n"
|
||||||
++ "Answer: " ++ (show result)
|
++ "Answer: " ++ (show result)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user