04 Time Complexity of Sieve of Eratosthenes

Each prime number p will have to mark N/p numbers as prime. So total markings are:

N2+N3+N5+N7++NP\frac{N}{2}+\frac{N}{3}+\frac{N}{5}+\frac{N}{7}+\dots+\frac{N}{P}

where P is the largest prime smaller than N.

Taking N common

N(12+13+15+17++1P)N\left(\frac{1}{2}+\frac{1}{3}+\frac{1}{5}+\frac{1}{7}+\dots+\frac{1}{P}\right)

We know

(12+13+15+17++1P)=HP of Primes=loglogP\left(\frac{1}{2}+\frac{1}{3}+\frac{1}{5}+\frac{1}{7}+\dots+\frac{1}{P}\right)=\text{HP of Primes}=\log{\log{P}}

Assuming worst case where P = N

The time compelxity of Sieve of Eratosthenes is

O(NloglogN)O(N\log{\log{N}})

Last updated