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.
How does the number of steps change when we increase the limit for primes?
Analyze the time complexity of the following code snippet.
void sieve(int n) {
bool prime[n+1];
for (int i = 0; i <= n; i++)
prime[i] = true;
prime[0] = prime[1] = false;
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
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: The inner loop that marks multiples of each prime as not prime.
- How many times: For each prime p, it runs roughly n/p times starting from p*p up to n.
As n grows, the number of operations grows a bit faster than n but much less than n squared.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 to 30 steps |
| 100 | About 300 to 400 steps |
| 1000 | About 4000 to 5000 steps |
Pattern observation: The operations grow 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 number, so the time is O(n²)."
[OK] Correct: The inner loop runs fewer times for larger primes because it starts at p squared and skips multiples, so total work is much less than n squared.
Understanding this time complexity shows you can analyze nested loops with decreasing work, a useful skill for many algorithm problems.
"What if we started marking multiples from 2*p instead of p*p? 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 BQuick Check:
Prime finding = B [OK]
- Confusing prime finding with sorting
- Thinking it finds divisors or reverses strings
- Assuming it works for negative numbers
n?Solution
Step 1: Understand sieve initialization
The sieve array uses 1 to mark potential primes initially.Step 2: Check array size and initialization
Array size should be n+1 to include number n, and all set to 1 (true) initially.Final Answer:
int sieve[n+1]; for(int i=0; i<=n; i++) sieve[i] = 1; -> Option AQuick Check:
Initialize all to 1 for primes [OK]
- Using size n instead of n+1
- Initializing with zeros instead of ones
- Marking 0 and 1 as prime initially
int n = 10;
int sieve[11];
for(int i=0; i<=n; i++) sieve[i] = 1;
sieve[0] = sieve[1] = 0;
for(int i=2; i*i<=n; i++) {
if(sieve[i]) {
for(int j=i*i; j<=n; j+=i) sieve[j] = 0;
}
}
for(int i=2; i<=n; i++) {
if(sieve[i]) printf("%d ", i);
}Solution
Step 1: Initialize sieve and mark 0,1 as non-prime
Array sieve marks 0 and 1 as 0 (not prime), others as 1 (prime candidate).Step 2: Mark multiples of primes as non-prime
For i=2, mark 4,6,8,10 as 0; for i=3, mark 9 as 0.Step 3: Print all indices with sieve[i] = 1
Primes are 2, 3, 5, 7 only.Final Answer:
2 3 5 7 -> Option DQuick Check:
Primes up to 10 = 2 3 5 7 [OK]
- Printing 1 as prime
- Including non-primes like 4 or 9
- Starting marking multiples from i instead of i*i
int n = 20;
int sieve[21];
for(int i=0; i<=n; i++) sieve[i] = 1;
sieve[0] = sieve[1] = 0;
for(int i=2; i<=n; i++) {
if(sieve[i]) {
for(int j=2*i; j<=n; j+=i) sieve[j] = 0;
}
}Solution
Step 1: Analyze outer loop condition
Outer loop runs from 2 to n, but it should run only up to sqrt(n) for efficiency.Step 2: Understand inner loop start
Inner loop correctly starts from 2*i, but starting from i*i is more optimal.Step 3: Correct the outer loop condition
Change outer loop to for(i=2; i*i<=n; i++) to avoid unnecessary iterations.Final Answer:
Change outer loop condition to i*i <= n -> Option CQuick Check:
Outer loop till sqrt(n) improves sieve [OK]
- Running outer loop till n wastes time
- Starting inner loop from 2*i instead of i*i (less efficient)
- Not marking 0 and 1 as non-prime
Solution
Step 1: List primes up to 50
Primes are: 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47 (total 15).Step 2: Count primes carefully
Counting again: 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47 = 15 primes.Step 3: Check if 1 is counted
1 is not prime, so total primes = 15.Final Answer:
15 -> Option AQuick Check:
Count primes up to 50 = 15 [OK]
- Including 1 as prime
- Miscounting primes between 40 and 50
- Forgetting to exclude non-primes
Ready to prove it?
Pass the test and this pattern moves unknown
