33 lines
899 B
Haskell
33 lines
899 B
Haskell
module ProjectEuler
|
|
( isPrime
|
|
, digitSum
|
|
, sumProperDivisors
|
|
, countDivisors
|
|
) where
|
|
|
|
import Data.Char (digitToInt)
|
|
import Data.List (nub)
|
|
|
|
isPrime :: (Integral n) => n -> Bool
|
|
isPrime 1 = False
|
|
isPrime 2 = True
|
|
isPrime 3 = True
|
|
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
|
|
|
|
|
|
sumProperDivisors :: (Integral a) => a -> a
|
|
sumProperDivisors n = sum [ if x /= y then x + y else x | x <- [2..ceiling $ sqrt $ fromIntegral n], let y = n `div` x, n `mod` x == 0 ] + 1
|
|
|
|
|
|
countDivisors :: (Integral a) => a -> Int
|
|
countDivisors n = length $ nub $ concat [ [x, n `div` x] | x <- [1..limit], n `mod` x == 0 ]
|
|
where limit = floor $ sqrt $ fromIntegral n
|
|
|