Skip to content

Instantly share code, notes, and snippets.

@cpfiffer
Created March 25, 2018 16:32
Show Gist options
  • Save cpfiffer/a9aa9de34ab1e9d05dfcf172c4d9d6b9 to your computer and use it in GitHub Desktop.
Save cpfiffer/a9aa9de34ab1e9d05dfcf172c4d9d6b9 to your computer and use it in GitHub Desktop.
Solves problem 21 of Project Euler with list comprehensions.
module P21 (properDivisors, sumDivisors, sumAmicable, isAmicable, amicableList) where
properDivisors :: Int -> [Int]
properDivisors x = [xs | xs <- [1..x-1], x `mod` xs == 0]
sumDivisors :: Int -> Int
sumDivisors x = sum $ properDivisors x
sumAmicable = sum [x | x <- [1..10000], isAmicable x]
amicableList = [x | x <- [1..10000], isAmicable x]
isAmicable x = if sumDivisors otherNumber == x && not (x == otherNumber) then True else False
where otherNumber = sumDivisors x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment