Skip to content

Instantly share code, notes, and snippets.

@jliszka
Created November 15, 2013 23:46
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 jliszka/7493732 to your computer and use it in GitHub Desktop.
Save jliszka/7493732 to your computer and use it in GitHub Desktop.
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