{-# OPTIONS_GHC -O2 -fno-cse #-} primesTMWE = 2:3:5:7: gapsW 11 wheel (joinT3 $ rollW 11 wheel primes') where gapsW k ws@(w:t) cs@(c:u) | k==c = gapsW (k+w) t u | True = k : gapsW (k+w) t cs rollW k ws@(w:t) ps@(p:u) | k==p = scanl (\c d->c+p*d) (p*p) ws : rollW (k+w) t u | True = rollW (k+w) t ps joinT3 ((x:xs): ~(ys:zs:t)) = x : union xs (union ys zs) `union` joinT3 (pairs t) wheel = 2:4:2:4:6:2:6:4:2:4:6:6:2:6:4:2:6:4:6:8:4:2:4:2: 4:8:6:4:6:2:4:6:2:6:6:4:2:4:6:2:6:4:2:4:2:10:2:10:wheel main = mapM_ print . take 10 . map fst . tail . iterate (splitAt 10.snd) $ ([],take 100 primesTMWE) pairs ((x:xs):ys:t) = (x : union xs ys) : pairs t union (x:xs) (y:ys) = case (compare x y) of LT -> x : union xs (y:ys) EQ -> x : union xs ys GT -> y : union (x:xs) ys
Standard input is empty
[2,3,5,7,11,13,17,19,23,29] [31,37,41,43,47,53,59,61,67,71] [73,79,83,89,97,101,103,107,109,113] [127,131,137,139,149,151,157,163,167,173] [179,181,191,193,197,199,211,223,227,229] [233,239,241,251,257,263,269,271,277,281] [283,293,307,311,313,317,331,337,347,349] [353,359,367,373,379,383,389,397,401,409] [419,421,431,433,439,443,449,457,461,463] [467,479,487,491,499,503,509,521,523,541]