Challenge - 5 Problems
Sieve Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Sieve for n=10
What is the output list of primes generated by the Sieve of Eratosthenes for n=10?
DSA Python
def sieve(n): primes = [True] * (n+1) primes[0] = primes[1] = False p = 2 while p * p <= n: if primes[p]: for i in range(p * p, n+1, p): primes[i] = False p += 1 return [x for x in range(n+1) if primes[x]] print(sieve(10))
Attempts:
2 left
💡 Hint
Remember primes are numbers greater than 1 with no divisors other than 1 and themselves.
✗ Incorrect
The sieve marks multiples of primes as not prime. For n=10, primes are 2, 3, 5, and 7 only.
❓ Predict Output
intermediate2:00remaining
Number of primes up to 20
How many prime numbers are there up to 20 using the Sieve of Eratosthenes?
DSA Python
def sieve_count(n): primes = [True] * (n+1) primes[0] = primes[1] = False p = 2 while p * p <= n: if primes[p]: for i in range(p * p, n+1, p): primes[i] = False p += 1 return sum(primes) print(sieve_count(20))
Attempts:
2 left
💡 Hint
Count primes: 2,3,5,7,11,13,17,19
✗ Incorrect
There are 8 primes up to 20: 2,3,5,7,11,13,17,19.
🔧 Debug
advanced2:00remaining
Identify the error in this sieve implementation
What error will this code produce when run with n=10?
DSA Python
def sieve_bug(n): primes = [True] * n primes[0] = primes[1] = False p = 2 while p * p <= n: if primes[p]: for i in range(p * p, n+1, p): primes[i] = False p += 1 return [x for x in range(n) if primes[x]] print(sieve_bug(10))
Attempts:
2 left
💡 Hint
Check the list size and the indices accessed in the loop.
✗ Incorrect
The list primes has length n=10, but the loop tries to access primes[10], causing IndexError.
❓ Predict Output
advanced2:00remaining
Output of primes for n=1
What is the output of the sieve function when n=1?
DSA Python
def sieve(n): primes = [True] * (n+1) primes[0] = primes[1] = False p = 2 while p * p <= n: if primes[p]: for i in range(p * p, n+1, p): primes[i] = False p += 1 return [x for x in range(n+1) if primes[x]] print(sieve(1))
Attempts:
2 left
💡 Hint
Primes start from 2, so no primes exist for n=1.
✗ Incorrect
Since 0 and 1 are not prime, and n=1, the output list is empty.
🧠 Conceptual
expert2:00remaining
Why does the sieve start marking multiples from p*p?
In the Sieve of Eratosthenes, why do we start marking multiples of a prime p from p*p instead of 2*p?
Attempts:
2 left
💡 Hint
Think about when smaller multiples get marked.
✗ Incorrect
Multiples less than p*p are already marked by smaller primes, so starting at p*p avoids redundant work.