Search⌘ K

Complexity Analysis

Explore how to analyze the time complexity of the Sieve of Eratosthenes algorithm. Understand the behavior of nested loops dependent on prime numbers and grasp why the overall complexity is O(N log log N). This lesson prepares you to optimize implementations for competitive programming contests.

We'll cover the following...

The outer loop

for (int i = 2; i * i <= N; i++)

Runs for N\sqrt{N} values but the number of iterations of the inner loop is dependent on ii.

for (int j = i + i; j <= N; j += i)

Also, note that the inner loop only runs for values when ii is prime.

ii Inner loop iterations
2 N2\frac{N}{2}
3 N3
...