-
-
Save fibo/1203756 to your computer and use it in GitHub Desktop.
Crivello Haskell
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 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