Bird
0
0
DSA Cprogramming~10 mins

Sieve of Eratosthenes Find All Primes in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Sieve of Eratosthenes Find All Primes
Create array of size n+1, all True
Set 0 and 1 as False
Start with p=2
Is p*p <= n?
NoStop
Yes
Mark multiples of p as False
Increment p to next True
Repeat loop
Collect indices with True as primes
Start with an array marking all numbers as prime candidates. Repeatedly mark multiples of each prime as not prime, then collect remaining primes.
Execution Sample
DSA C
int n = 10;
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 finds all prime numbers up to 10 using the sieve method.
Execution Table
StepOperationCurrent pMark MultiplesArray State (index:prime)
1Initialize array-None0:F 1:F 2:T 3:T 4:T 5:T 6:T 7:T 8:T 9:T 10:T
2Start p=22Mark multiples of 20:F 1:F 2:T 3:T 4:F 5:T 6:F 7:T 8:F 9:T 10:F
3Increment p=33Mark multiples of 30:F 1:F 2:T 3:T 4:F 5:T 6:F 7:T 8:F 9:F 10:F
4Increment p=44Skip (4 is False)0:F 1:F 2:T 3:T 4:F 5:T 6:F 7:T 8:F 9:F 10:F
5Increment p=55p*p=25 > 10 stop0:F 1:F 2:T 3:T 4:F 5:T 6:F 7:T 8:F 9:F 10:F
6Collect primes-Indices with TruePrimes: 2, 3, 5, 7
💡 Stop when p*p > n (5*5=25 > 10), sieve complete.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
p-2345-
prime arrayall True except 0 and 1 Falsemultiples of 2 Falsemultiples of 3 Falseunchanged (4 False)unchanged (stop)final sieve
marked multiplesnone4,6,8,109nonenonenone
Key Moments - 3 Insights
Why do we start marking multiples from p*p, not from 2*p?
Because smaller multiples like 2*p, 3*p were already marked by smaller primes. Starting at p*p avoids redundant work, as shown in step 2 where multiples start at 4 (2*2).
Why does the loop stop when p*p > n?
Because any composite number less than or equal to n must have a factor less than or equal to sqrt(n). Once p*p > n, all composites are marked, so no need to continue (see step 5).
Why are 0 and 1 set to False at the start?
0 and 1 are not prime numbers by definition, so they are marked False before starting the sieve (step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at Step 3. Which multiples of 3 are marked False?
A6 and 9
B3 and 6
C9 and 12
D3 and 9
💡 Hint
Check the 'Mark Multiples' and 'Array State' columns at Step 3.
At which step does the algorithm stop marking multiples because p*p > n?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Operation' and 'Current p' columns where p*p > n is mentioned.
If we did not set prime[0] and prime[1] to False at the start, what would happen?
AThe algorithm would run infinitely
B0 and 1 would be incorrectly included as primes
CThe algorithm would mark all numbers False
DNo change in output
💡 Hint
Refer to the initial array state in Step 1 and the definition of primes.
Concept Snapshot
Sieve of Eratosthenes:
- Create boolean array up to n, True means prime candidate
- Set 0 and 1 to False
- For p from 2 to sqrt(n):
  - If prime[p] is True, mark multiples p*p, p*p+p, ... False
- Remaining True indices are primes
- Stops when p*p > n
- Efficiently finds all primes up to n
Full Transcript
The Sieve of Eratosthenes finds all prime numbers up to a number n by creating an array marking all numbers as prime candidates. It sets 0 and 1 as not prime. Starting from p=2, it marks all multiples of p as not prime, beginning at p squared to avoid redundant marking. This repeats for each p until p squared exceeds n. The remaining numbers marked True are primes. This method efficiently eliminates non-primes and collects primes up to n.