Perfect numbers: Difference between revisions
Content added Content deleted
(→{{header|Haskell}}: Added a Haskell variant) |
|||
Line 1,157: | Line 1,157: | ||
mapM_ print $ take 10 perfect |
mapM_ print $ take 10 perfect |
||
mapM_ (print . (\x -> (x, isPerfect x))) [6, 27, 28, 29, 496, 8128, 8129]</lang> |
mapM_ (print . (\x -> (x, isPerfect x))) [6, 27, 28, 29, 496, 8128, 8129]</lang> |
||
or, using a simple restriction of the search space: |
|||
<lang haskell>isPerfect :: Int -> Bool |
|||
isPerfect n = |
|||
let lows = filter ((0 ==) . rem n) [1 .. floor (sqrt (fromIntegral n))] |
|||
in 1 < n && |
|||
quot |
|||
(sum $ |
|||
lows ++ |
|||
concatMap |
|||
(\x -> |
|||
let y = quot n x |
|||
in [ y |
|||
| x /= y ]) |
|||
lows) |
|||
2 == |
|||
n |
|||
main :: IO () |
|||
main = print $ filter isPerfect [1 .. 10000]</lang> |
|||
{{Out}} |
|||
<pre>[6,28,496,8128]</pre> |
|||
=={{header|HicEst}}== |
=={{header|HicEst}}== |