fork download
  1. {-# OPTIONS_GHC -O2 #-}
  2. module Main where
  3. import qualified Data.IntSet as I
  4.  
  5. -- sieve of Eratosthenes - all primes up to n
  6. primes n = go 3 (I.fromDistinctAscList (2:[3,5..n]))
  7. where
  8. go x is
  9. | (x*x) > n = is
  10. | I.notMember x is = go (x+2) is
  11. | otherwise = go (x+2) (is I.\\
  12. (I.fromDistinctAscList [x*x,x*x+x+x..n]))
  13.  
  14. main = do
  15. let topval = 4256233
  16. let ps = primes topval
  17. -- print ps
  18. putStrLn $ "There are " ++ show(I.size ps)
  19. ++ " primes upto " ++ show topval
  20. ++ ", and the topmost prime is "
  21. ++ show (I.findMax ps) ++ "."
  22.  
  23. {- INTSET/all INTSET/Odds SEGfilt SEGTREE ARR/Odds
  24.   1,000: 7919 0.01s 4.6MB 0.00 3.6 0.00 3.7 0.00 3.7
  25.   30,000: 350377 0.65s 29.2MB 0.29 15.9 0.09 4.7 0.02 4.8
  26.   100,000: 1299709 2.90s 110.1MB 1.38 52.7 0.51 5.8 0.23 6.8 0.06 7.8
  27.   300,000: 4256233 OUT_OF_MEM 5.32 190.0 2.49 12.9 0.83 12.9 0.27 17.1
  28. 1,000,000: 15485863 OUT_OF_MEM 14.09 38.5 3.73 57.0 1.18 52.9
  29. -}
stdin
Standard input is empty
compilation info
[1 of 1] Compiling Main             ( prog.hs, prog.o )
Linking prog ...
stdout
There are 300000 primes upto 4256233, and the topmost prime is 4256233.