Skip to content

Instantly share code, notes, and snippets.

@osfameron
Created September 8, 2011 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save osfameron/1203752 to your computer and use it in GitHub Desktop.
Save osfameron/1203752 to your computer and use it in GitHub Desktop.
Crivello Haskell
import Data.List
getPrimes :: Int -> Int -> [Int] -> [Int]
getPrimes p1 p2 ps = map fst
. filter (all (/=0) . snd)
$ makeTable p1 p2 ps
makeTable :: Int -> Int -> [Int] -> [(Int, [Int])]
makeTable p1 p2 ps =
let from = p1^2
to = p2^2
mkCycle p = drop (from `mod` p) (cycle [0..p-1])
cs = map mkCycle ps
in zip [from..to] (transpose cs)
primes :: [Int]
primes = primes' [2,3] [2,3]
primes' :: [Int] -> [Int] -> [Int]
primes' (p1:p2:queue) all =
let ps = takeWhile (<=p2) all
next = getPrimes p1 p2 ps
in p1 : primes' ((p2:queue)++next) (all++next)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment