Skip to content

Instantly share code, notes, and snippets.

@GrAndSE
Created September 21, 2011 14:12
Show Gist options
  • Save GrAndSE/1232131 to your computer and use it in GitHub Desktop.
Save GrAndSE/1232131 to your computer and use it in GitHub Desktop.
Prime numbers searching using Haskell
getDivisors num
| num < 1 = []
| otherwise = [x | x <- [n | n <- primeNumbers (num - 1), n <= ((floor.sqrt.fromIntegral) (num + 1))], num `mod` x == 0 ]
primeNumbers :: Integer -> [Integer]
primeNumbers 2 = [2]
primeNumbers num =
if getDivisors num == []
then primeNumbers (num - 1) ++ [num]
else primeNumbers (num - 1)
main :: IO()
main = print (primeNumbers 1000)
@md2perpe
Copy link

It would probably be more efficient to use

n * n <= num

instead of

((floor.sqrt.fromIntegral) (num + 1))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment