Time complexity of Sieve of Eratosthenes algorithm
Posted
by eSKay
on Stack Overflow
See other posts from Stack Overflow
or by eSKay
Published on 2010-04-06T05:06:23Z
Indexed on
2010/04/06
5:13 UTC
Read the original article
Hit count: 589
From Wikipedia:
The complexity of the algorithm is
O(n(logn)(loglogn))
bit operations.
How do you arrive at that?
That the complexity includes the loglogn term tells me that there is a sqrt(n)
somewhere.
Suppose I am running the sieve on the first 100 numbers (n = 100
), assuming that marking the numbers as composite takes constant time (array implementation), the number of times we use mark_composite()
would be something like
n/2 + n/3 + n/5 + n/7 + ... + n/97 = O(n)
And to find the next prime number (for example to jump to 7
after crossing out all the numbers that are multiples of 5
), the number of operations would be O(n)
.
So, the complexity would be O(n^2)
. Do you agree?
© Stack Overflow or respective owner