Created
November 15, 2013 23:46
-
-
Save jliszka/7493732 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Data.List | |
import Test.QuickCheck | |
import qualified Data.Map as Map | |
nats = [1..] | |
divides a b = b `mod` a == 0 | |
sieve (n:t) m = case Map.lookup n m of | |
Nothing -> n : sieve t (Map.insert (n*n) [n] m) | |
Just ps -> sieve t (foldl reinsert (Map.delete n m) ps) | |
where | |
reinsert m p = Map.insertWith (++) (n+p) [p] m | |
primes = 2 : sieve (map (\n -> n*2+1) nats) Map.empty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment