Haskell – beauty of couding – get perfect numbers

Recently did some haskell coding and just some lines to explain my enthusiasm.

Let’s try to calculate  perfect numbers:

First I have helper function factors to get number factors:

factors :: Int -> [Int]
factors a = [ x | x <- [1..a], y <- [1..a], x*y == a ]

and the main function is:

perfects :: Int -> [Int]
perfects x = [a | a <- [1..x], sum (init(factors a)) == a, a <= x]

to get perfect numbers under limit believe me you want to limit the calculation 🙂

Posted in IT