The computational complexity of the Sieve of Eratosthenes
- From: Juha Nieminen <nospam@xxxxxxxxxxxxxx>
- Date: Tue, 29 Apr 2008 00:13:39 GMT
Calculating the computational complexity of the Sieve of Eratosthenes
algorithm is surprisingly difficult.
After reading the algorithm the very first intuition is that it's an
O(n^2) algorithm: For each number the table is traversed and multiples
of that number are marked as non-prime. Sounds like typical O(n^2) behavior.
However, on closer examination one discovers that's actually not true:
The inner loop can be started from the square of the number, not the
number itself. This would suggest that the algorithm is actually
O(n*sqrt(n)).
Yet, on even closer examination one discovers that the inner loop is
actually not executed for each number, but only for primes. The amount
of primes is smaller than O(n). Thus a more accurate estimation would be
O(n*sqrt(pi(n))), where pi is the prime counting function.
And still something is off: The outer loop actually doesn't go through
all the numbers: It stops after sqrt(n) numbers have been traversed.
Thus an even more accurate estimate is O(sqrt(n)*sqrt(pi(n))), or in
other words, O(sqrt(n*pi(n))).
But here's were my skills fall short. The wikipedia page gives a
complexity of O((n log n)(log log n)) for the algorithm. Where does this
come from?
.
- Follow-Ups:
- Re: The computational complexity of the Sieve of Eratosthenes
- From: Ben Bacarisse
- Re: The computational complexity of the Sieve of Eratosthenes
- From: Richard Heathfield
- Re: The computational complexity of the Sieve of Eratosthenes
- Prev by Date: Question on computing the average time of Quicksort
- Next by Date: Re: Programming to Beat the Odds in Gaming
- Previous by thread: Question on computing the average time of Quicksort
- Next by thread: Re: The computational complexity of the Sieve of Eratosthenes
- Index(es):
Relevant Pages
|