I whipped a reasonably fast brute-force perfect number finding function in Haskell, after seeing a similar thing in mathematica.
It works in under 3 seconds when I search up to 1000, but 10000 is too much.
isDivisor :: Integral a => a -> a -> Bool
isDivisor n a = mod n a == 0
sumDivisors :: Integral a => a -> a
sumDivisors a =
foldr (\n -> if isDivisor a n then (+ n) else id) 0 [1..(a `div` 2)]
isPerfect :: Integral a => a -> Bool
isPerfect n = n == sumDivisors n
-- The main prime-finding function:
upUntil :: Integral a => a -> [a]
upUntil n = filter isPerfect [1..n]
It certainly isn't the fastest, or the most elegant, but I'm unsure how I can improve it.
I would like to concentrate on the isDivisor
function in particular. Is there a more efficient way of checking for divisibility?