Bird
0
0
DSA Cprogramming~20 mins

Sieve of Eratosthenes Find All Primes in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sieve Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Sieve for n=10
What is the output list of primes printed by this Sieve of Eratosthenes code for n=10?
DSA C
int n = 10;
int primes[10];
for(int i = 0; i < n; i++) primes[i] = 1;
primes[0] = 0; primes[1] = 0;
for(int i = 2; i*i < n; i++) {
    if(primes[i]) {
        for(int j = i*i; j < n; j += i) primes[j] = 0;
    }
}
for(int i = 2; i < n; i++) {
    if(primes[i]) printf("%d ", i);
}
A[2, 3, 5, 6, 7]
B[2, 3, 4, 5, 7]
C[2, 3, 5, 7, 9]
D[2, 3, 5, 7]
Attempts:
2 left
💡 Hint
Remember that the sieve marks multiples of primes as not prime.
Predict Output
intermediate
2:00remaining
Count of primes up to 20
How many primes does the Sieve of Eratosthenes find up to n=20?
DSA C
int n = 20;
int primes[20];
for(int i = 0; i < n; i++) primes[i] = 1;
primes[0] = 0; primes[1] = 0;
for(int i = 2; i*i < n; i++) {
    if(primes[i]) {
        for(int j = i*i; j < n; j += i) primes[j] = 0;
    }
}
int count = 0;
for(int i = 2; i < n; i++) {
    if(primes[i]) count++;
}
printf("%d", count);
A8
B9
C7
D10
Attempts:
2 left
💡 Hint
Count primes: 2,3,5,7,11,13,17,19
🔧 Debug
advanced
2:00remaining
Identify the error in Sieve implementation
What error does this Sieve of Eratosthenes code produce when run for n=15?
DSA C
int n = 15;
int primes[15];
for(int i = 0; i < n; i++) primes[i] = 1;
primes[0] = 0; primes[1] = 0;
for(int i = 2; i*i < n; i++) {
    if(primes[i]) {
        for(int j = i*i; j < n; j += i) primes[j] = 0;
    }
}
for(int i = 2; i < n; i++) {
    if(primes[i]) printf("%d ", i);
}
AArray index out of bounds error
BCompilation error due to missing semicolon
CNo error, prints primes correctly
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop that initializes the primes array.
Predict Output
advanced
2:00remaining
Output of Sieve for n=30 with print format
What is the printed output of primes for n=30 using this code?
DSA C
int n = 30;
int primes[30];
for(int i = 0; i < n; i++) primes[i] = 1;
primes[0] = 0; primes[1] = 0;
for(int i = 2; i*i < n; i++) {
    if(primes[i]) {
        for(int j = i*i; j < n; j += i) primes[j] = 0;
    }
}
for(int i = 2; i < n; i++) {
    if(primes[i]) printf("%d->", i);
}
printf("null");
A2->3->5->7->11->13->17->19->23->29->null
B2->3->5->7->9->11->13->17->19->23->29->null
C2->3->5->7->11->13->17->19->23->27->null
D2->3->5->7->11->13->17->19->23->29
Attempts:
2 left
💡 Hint
Remember 9 and 27 are not primes.
🧠 Conceptual
expert
2:00remaining
Why does the inner loop start at i*i in Sieve?
In the Sieve of Eratosthenes, why does the inner loop start marking multiples from i*i instead of 2*i?
ABecause i*i is the smallest prime number
BBecause all smaller multiples of i were already marked by smaller primes
CBecause starting at 2*i would miss some primes
DBecause starting at i*i reduces memory usage
Attempts:
2 left
💡 Hint
Think about when multiples less than i*i get marked.