language: Haskell (ghc-7.4.1)
date: 108 days 3 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{-# OPTIONS_GHC -O2 -fno-cse #-}
primesTMWE = 2:3:5:7: gapsW 11 wheel (joinT3 $ rollW 11 wheel primes')
  where
    primes' = 11: gapsW 13 (tail wheel) (joinT3 $ rollW 11 wheel primes')
 
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 = do 
   x <- read `fmap` getLine                --   100k    1mln     2mln     4mln
   mapM_ print                             --  6.8MB   6.8MB    6.8MB    6.8MB     -- !!!!!
         . take 2 . map fst                --  0.13s   2.09s    4.87s    11.25s
         . tail . iterate (splitAt 10.snd) --      n^1.21  n^1.22   n^1.21         -- !!!!
         $ ([], drop (x-10) 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
  • upload with new input
  • result: Success     time: 11.25s    memory: 6784 kB     returned value: 0

    4000000
    [67867831,67867843,67867901,67867903,67867907,67867937,67867949,67867951,67867957,67867967]
    [67867979,67867999,67868011,67868023,67868041,67868069,67868077,67868111,67868123,67868153]
    
test TMWE code from http://haskell.org/haskellwiki/Prime_numbers