Last active
January 4, 2017 21:23
-
-
Save bjackson/ab619ecb85cdfbe34fe4b365384593a1 to your computer and use it in GitHub Desktop.
Prime generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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