Skip to content

Instantly share code, notes, and snippets.

@fibo
Forked from osfameron/gist:1203752
Created September 8, 2011 16:00
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 fibo/1203756 to your computer and use it in GitHub Desktop.
Save fibo/1203756 to your computer and use it in GitHub Desktop.
Crivello Haskell
import Data.List
import Debug.Trace
-- primes = 2 : 3 : zipWith getPrime primes (tail primes)
checkZeros :: [Int] -> Bool
checkZeros ms = any isZero ms
where isZero 0 = True
isZero _ = False
calculateMods :: Int -> [Int] -> [Int]
calculateMods n ps = map moduloOfN ps
where moduloOfN p = n `mod` p
-- calculateMods n ps = map (mod n) ps
primesInRange :: Int -> Int -> [Int] -> [Int]
primesInRange start end ps = filter isPrime [start .. end]
where isPrime n = not (checkZeros (calculateMods n ps))
-- NB: use transpose & cycle to calculate list, instead of mod every time!
-- primes :: [Int]
-- foldl? unfoldr? ???
primes = 2 : 3 : stepIt primes
-- primes = 2 : 3 : zipWith getPrime primes (tail primes)
stepIt ps = (trace (show $ take 2 ps) revstep) . reverse $ ps
revstep ps@(ultimo:penultimo:_) = primesInRange (penultimo^2) (ultimo^2) ps
-- 2 3 5 7 11 13
-- 3 5 7 11 13 17
-- where stepIt ps = ps : primesInRange (penultimo ps)^2 (ultimo ps)^2 ps
-- zipN :: [[Int]] -> [[Int]]
-- zipN ls = [map (head) ls] : zipN [map (tail) ls]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment