After getting through some of the SO posts, i found Sieve of Eratosthenes is the best & fastest way of generating prime numbers.
I want to generate the prime num
There are two basic choices here: sieve the range [a..b]
by primes below sqrt(b)
(the "offset" sieve of Eratosthenes), or by odd numbers. That's right; just eliminate the multiples of each odd as you would of each prime. Sieve the range in one chunk, or in several "segments" if the range is too wide (but efficiency can deteriorate if the chunks are too narrow).
In Haskell executable pseudocode,
primesRange_by_Odds a b = foldl (\r x-> r `minus` [q x, q x+2*x..b])
[o,o+2..b]
[3,5..floor(sqrt(fromIntegral b))]
where
o = 1 + 2*div a 2 -- odd start of range
q x = x*x - 2*x*min 0 (div (x*x-o) (2*x)) -- 1st odd multiple of x >= x*x in range
Sieving by odds will have the additional space complexity of O(1) (on top of the output / range). That is because we can enumerate the odds just by iteratively adding 2 — unlike primes of sieve of Eratosthenes, below sqrt(b)
, for which we have to reserve additional space of O(pi(sqrt(b))) = ~ 2*sqrt(b)/log(b)
(where pi()
is a prime-counting function).