fork download
  1. {-# OPTIONS_GHC -O2 -fno-cse #-}
  2. primesTMWE = ([2,3,5,7 :: Int]++) . _Y $ (11:) .
  3. gapsW 13 (tail wheel) . joinT3 . rollW 11 wheel
  4.  
  5. _Y g = g (_Y g) -- telescopic
  6. -- g xs where xs = g xs -- two-stage
  7.  
  8. gapsW k ws@(w:t) cs@(c:u) | k==c = gapsW (k+w) t u
  9. | True = k : gapsW (k+w) t cs
  10. rollW k ws@(w:t) ps@(p:u) | k==p = scanl (\c d->c+p*d) (p*p) ws
  11. : rollW (k+w) t u
  12. | True = rollW (k+w) t ps
  13. joinT3 ((x:xs): ~(ys:zs:t)) = x : union xs (union ys zs)
  14. `union` joinT3 (pairs t)
  15. 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:
  16. 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
  17.  
  18. main = do
  19. x <- read `fmap` getLine -- 100k 1mln 2mln 4mln
  20. mapM_ print -- 6.8MB 6.8MB 6.8MB 6.8MB -- !!!!!
  21. . take 2 . map fst -- 0.13s 2.01s 4.67s 10.71s -- [Integer], tele, no -O2
  22. . tail . iterate (splitAt 10.snd) -- n^1.19 n^1.22 n^1.20 -- !!!!
  23. $ ([], drop (x-10) primesTMWE) -- 0.08s 1.31s 3.18s 7.93s -- [Int], telescopic
  24.  
  25. pairs (xs:ys:t) = union xs ys : pairs t -- 0.08s 1.41s 3.36s 7.96s -- [Int], two-stage
  26. union (x:xs) (y:ys) = case (compare x y) of
  27. LT -> x : union xs (y:ys)
  28. EQ -> x : union xs ys
  29. GT -> y : union (x:xs) ys
Success #stdin #stdout 0.08s 6780KB
stdin
100000
stdout
[1299553,1299583,1299601,1299631,1299637,1299647,1299653,1299673,1299689,1299709]
[1299721,1299743,1299763,1299791,1299811,1299817,1299821,1299827,1299833,1299841]