Created
January 27, 2020 03:23
-
-
Save turnage/be75f5cab2d03728b4b7482f6e287563 to your computer and use it in GitHub Desktop.
Simple Prime Sieve
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
data Sieve = Sieve | |
{ limit :: Int | |
, position :: Int | |
, product :: Int | |
, sieve :: [Int] | |
, count :: Int | |
} | |
uniquePrimes :: Sieve -> Maybe (Int, Sieve) | |
uniquePrimes (Sieve limit position product sieve count) = if product * position > limit | |
then Nothing | |
else if isPrime position | |
then let count' = count + 1 | |
product' = product * position | |
in Just $ (count', Sieve limit position' product' (position : sieve) count') | |
else uniquePrimes $ Sieve limit position' product sieve count | |
where | |
position' = position + 1 | |
isPrime c = not $ any (\p -> c `mod` p == 0) sieve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment