完全数

煮詰まったときの頭の体操.



divisors :: Int -> [Int]
divisors n = divisor' n 1
where
divisor' n m | n >= m * 2 && n `mod` m == 0 = m : divisor' n (m + 1)
| n >= m * 2 && n `mod` m /= 0 = divisor' n (m + 1)
| n < m * 2 = []


isPerfectNumber :: Int -> Bool
isPerfectNumber n = let l = divisors n in
foldr (+) 0 l == n

リストモナド使おうと思ったけど使うところが無かった.