Final Complexity
-
Time Complexity:
-
Auxiliary Space Complexity:
Step 1: What does the algorithm do?
The Sieve of Eratosthenes finds all prime numbers up to n by repeatedly marking the multiples of every prime.
For example, if n = 30:
-
Multiples of 2 → 4, 6, 8, 10, 12, …
-
Multiples of 3 → 6, 9, 12, 15, …
-
Multiples of 5 → 10, 15, 20, …
-
Skip 4 because it has already been marked as non-prime.
So, the work done by the algorithm is simply the work of marking multiples.
Step 2: How much work does each prime do?
Suppose the current prime is p.
The algorithm starts marking from p² (because smaller multiples have already been handled by smaller primes), but asymptotically, the number of multiples marked is approximately
Examples:
| Prime | Approximate Multiples Marked |
|---|---|
| 2 | |
| 3 | |
| 5 | |
| 7 |
Step 3: Total Work Done
Adding the work done for every prime gives
Notice something very important:
We are only summing over prime numbers, not over every integer.
Step 4: Why isn’t it ?
If we summed over every integer, we’d get
This is called the harmonic series, whose value grows as
Therefore,
$$
n \times O(\log n)
O(n\log n)
However, the sieve **doesn't process every integer**. It only processes **prime numbers**, making the sum much smaller. --- # Step 5: The Important Mathematical Result A famous result from number theory states that the sum of the reciprocals of all prime numbers up to `n` is # $$ \frac12+\frac13+\frac15+\frac17+\cdots O(\log\log n)Therefore,
Hence,
Intuition
Think about what happens as the primes become larger:
-
Prime 2 marks about half of all numbers.
-
Prime 3 marks about one-third.
-
Prime 5 marks about one-fifth.
-
Larger primes mark fewer and fewer numbers.
Moreover, prime numbers themselves become less frequent as numbers grow larger.
So, even though we’re visiting many primes, each successive prime contributes less work, causing the total work to grow much slower than .
This is why the overall complexity becomes
Interview Answer (30 Seconds)
For every prime
p, the sieve marks approximately multiples. Therefore, the total work isA well-known mathematical result states that the sum of the reciprocals of all prime numbers up to
nis . Therefore, the overall time complexity isThe auxiliary space complexity is because we maintain a boolean array of size
n + 1.
Key Takeaways
-
Each prime
pmarks approximately multiples. -
Total work is
-
The reciprocal-prime sum is (a standard mathematical result).
-
Therefore,
-
Auxiliary Space Complexity: