1

I recently came across an article that claimed that it can find all primes less than n in O(n) using an efficient Sieve Of Eratosthenes. However I am unable to see how it is O(n).

https://www.geeksforgeeks.org/sieve-eratosthenes-0n-time-complexity/

Could anyone please help with that?

levio_sa
  • 123
  • 9
  • 2
    We don't go to other sites to get the information required for your question. All relevant details need to be here in the question itself so that it is available for future readers in case the link becomes invalid in the future. You can use the link as an additional reference, but all of the relevant information needs to be posted here. For more information, see the [help]. – Ken White Jun 01 '20 at 18:34
  • 1
    if it's better than *O(n log log n)* then it' not the sieve of *Eratosthenes*. and if it doesn't scale past some (fairly low by modern standards) top limit then its claim does not really hold *asymptotically*. – Will Ness Jun 02 '20 at 09:03
  • But I don't have any rights for reposting the code from that site. – levio_sa Jun 02 '20 at 15:39

3 Answers3

4

The normal Sieve of Eratosthenes is O(n log log n).

Paul Pritchard has done some work on sieves similar to the Sieve of Eratosthenes that run in O(n) and even in O(n / log log n). They are tricky to implement, and despite improved theoretical time complexity, the bookkeeping involved in running the sieves makes them slower than the normal Sieve of Eratosthenes.

I discuss a simple version of Pritchard's sieve at my blog.

user448810
  • 17,381
  • 4
  • 34
  • 59
3

It is a version of the Gries and Misra (1978) sieve, which is an O(n) sieve. A better description can be found here:

(external link) Sieve of Eratosthenes Having Linear Time Complexity.

For a more theoretical look at this type of sieve, from an expert in the field, see Pritchard's paper:

(external link) Linear Prime-Number Sieves: A Family Tree (1987, PDF).

Pritchard is well known for his sub-linear sieve algorithm and paper as well as other early contributions.

The version at GfG uses a lot of extra memory. The version at CP uses a little less. Both are huge compared to typical byte or bit implementations of the SoE. At 10^9, it is over 60x more memory used than a simple bit array monolithic SoE, and also half the speed, even when using uint32_t types.

So in practice it is slower than a simple 4-line monolithic SoE, which is usually where we start before getting into the interesting optimizations (segmented sieves, wheels, etc.). If you actually want the factor array, then that's useful. It's also useful for learning and experimentation, though the GfG article doesn't actually do much other than give the code. The CP page does go over a bit of the history and some memory/speed analysis.

DanaJ
  • 724
  • 6
  • 9
2

The algorithm at your link is a variation of Algorithm 3.3 in Paul Pritchard's paper "Linear Prime-Number Sieves: a Family Tree". The reason the algorithm is linear, i.e. O(n), is that each composite is removed once, because a composite c has a unique form p*f where p=lpf(c), and it is removed when the outer loop variable if f, and the inner loop variable j is such that p[j]=p.

Incidentally, the code is inelegant. There is no need for two arrays; SPF suffices. Also, the first test (on j) in the inner loop is unnecessary.

Many other linear sieves are presented in Pritchard's paper, one of which is due to Gries and Misra, which is an entirely different algorithm. The algorithm at your link is often mis-attributed to Gries and Misra.

oluckyman
  • 126
  • 3