Skip to content

Instantly share code, notes, and snippets.

@bjackson
Last active January 4, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjackson/ab619ecb85cdfbe34fe4b365384593a1 to your computer and use it in GitHub Desktop.
Save bjackson/ab619ecb85cdfbe34fe4b365384593a1 to your computer and use it in GitHub Desktop.
Prime generation
isqrt :: Int -> Int
isqrt = floor . sqrt . fromIntegral
genPrime :: Int -> Int
genPrime n =
[x | x <- 2 : [3, 5..], isPrime x] !! (n - 1)
isPrime :: Int -> Bool
isPrime 1 = False
isPrime n =
n == 2 || (odd n && not (any p ([3, 5..isqrt n])))
where p x = n `mod` x == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment