Sieve of Eratosthenes Find All Primes in DSA C# - Time & Space Complexity
We want to understand how the time needed to find all prime numbers grows as the input number gets bigger.
Specifically, how does the Sieve of Eratosthenes perform when finding primes up to a number n?
Analyze the time complexity of the following code snippet.
bool[] isPrime = new bool[n + 1];
for (int i = 2; i <= n; i++)
isPrime[i] = true;
for (int i = 2; i * i <= n; i++)
{
if (isPrime[i])
{
for (int j = i * i; j <= n; j += i)
isPrime[j] = false;
}
}
// isPrime array now marks primes as true
This code marks all prime numbers up to n by crossing out multiples of each prime.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Marking multiples of each prime number as not prime.
- How many times: For each prime i, we mark multiples starting from i*i up to n, stepping by i.
As n grows, the number of operations grows a bit faster than n but much slower than n squared.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~20 |
| 100 | ~300 |
| 1000 | ~5000 |
Pattern observation: The work grows roughly proportional to n times the logarithm of the logarithm of n, which is slower than n log n but faster than n.
Time Complexity: O(n log log n)
This means the time grows a bit faster than n but much slower than n squared, making it efficient for large n.
[X] Wrong: "The inner loop runs n times for each i, so total time is O(n²)."
[OK] Correct: The inner loop starts at i*i and skips by i, so it does fewer steps as i grows, reducing total work significantly.
Understanding this algorithm's time complexity shows you can analyze loops that don't run the same number of times each iteration, a valuable skill in interviews.
"What if we started marking multiples from 2*i instead of i*i? How would the time complexity change?"
Practice
Sieve of Eratosthenes algorithm?Solution
Step 1: Understand the algorithm's goal
The Sieve of Eratosthenes is designed to identify prime numbers efficiently.Step 2: Recognize the output
It marks multiples of primes as non-prime to find all primes up to a limit.Final Answer:
To find all prime numbers up to a given limit -> Option CQuick Check:
Sieve of Eratosthenes = Find primes [OK]
- Confusing it with sorting algorithms
- Thinking it finds factors instead of primes
- Assuming it works on strings
n?Solution
Step 1: Check array size and initialization
The array must be of size n+1 to include index n, and primes start from 2.Step 2: Verify initialization values
All entries from 2 to n should be set to true initially to mark potential primes.Final Answer:
bool[] isPrime = new bool[n + 1]; for(int i = 2; i <= n; i++) isPrime[i] = true; -> Option BQuick Check:
Array size n+1 and true from 2 to n [OK]
- Using array size n instead of n+1
- Initializing all values to false
- Starting initialization from 0 or 1
n = 10?Solution
Step 1: Identify primes up to 10
Primes are numbers greater than 1 with no divisors other than 1 and themselves: 2, 3, 5, 7.Step 2: Exclude non-primes
Numbers 1, 4, 6, 8, 9, 10 are not prime.Final Answer:
[2, 3, 5, 7] -> Option DQuick Check:
Primes up to 10 = [2, 3, 5, 7] [OK]
- Including 1 as prime
- Including even numbers > 2
- Including non-prime odd numbers like 9
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * 2; j <= n; j += i) {
isPrime[j] = false;
}
}
}
Which of the following improves the efficiency of this code?Solution
Step 1: Understand sieve optimization
Marking multiples starts from i squared because smaller multiples were marked by smaller primes.Step 2: Identify suboptimal start of inner loop
Starting from i*2 repeats marking multiples already handled; correct start is i*i.Final Answer:
The inner loop should start from i * i, not i * 2 -> Option AQuick Check:
Start inner loop at i*i for efficiency [OK]
- Starting inner loop at i*2 causing redundant work
- Changing outer loop condition incorrectly
- Incorrect increment steps in inner loop
n = 30 using the Sieve of Eratosthenes. After running the sieve, which of the following lists correctly shows the primes and their count?Solution
Step 1: List primes up to 30
Primes are numbers >1 with no divisors except 1 and themselves: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.Step 2: Count and verify correctness
Count is 10; numbers like 9, 25, and 1 are not prime and should be excluded.Final Answer:
Primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29], Count: 10 -> Option AQuick Check:
Correct primes and count up to 30 [OK]
- Including 1 as prime
- Including composite numbers like 9 or 25
- Miscounting the total primes
Ready to prove it?
Pass the test and this pattern moves unknown
