Challenge - 5 Problems
Sieve Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sieve for n=10
What is the output of the following Rust code that uses the Sieve of Eratosthenes to find all primes up to 10?
DSA Rust
fn sieve(n: usize) -> Vec<usize> {
let mut primes = vec![true; n + 1];
primes[0] = false;
primes[1] = false;
let mut p = 2;
while p * p <= n {
if primes[p] {
let mut i = p * p;
while i <= n {
primes[i] = false;
i += p;
}
}
p += 1;
}
primes.iter().enumerate().filter_map(|(i, &is_prime)| if is_prime { Some(i) } else { None }).collect()
}
fn main() {
let result = sieve(10);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember that primes are numbers greater than 1 that have no divisors other than 1 and themselves.
✗ Incorrect
The sieve marks multiples of each prime starting from its square as not prime. For n=10, primes are 2, 3, 5, and 7. Numbers like 9 are not prime because 3 divides 9.
🧠 Conceptual
intermediateNumber of Primes up to 20
Using the Sieve of Eratosthenes, how many prime numbers are there up to 20 (inclusive)?
Attempts:
2 left
💡 Hint
List primes: 2, 3, 5, 7, 11, 13, 17, 19
✗ Incorrect
The primes up to 20 are 2, 3, 5, 7, 11, 13, 17, and 19, totaling 8 primes.
🔧 Debug
advancedIdentify the Error in Sieve Implementation
What error does the following Rust code produce when trying to find primes up to 15?
DSA Rust
fn sieve(n: usize) -> Vec<usize> {
let mut primes = vec![true; n];
primes[0] = false;
primes[1] = false;
let mut p = 2;
while p * p <= n {
if primes[p] {
let mut i = p * p;
while i <= n {
primes[i] = false;
i += p;
}
}
p += 1;
}
primes.iter().enumerate().filter_map(|(i, &is_prime)| if is_prime { Some(i) } else { None }).collect()
}
fn main() {
let result = sieve(15);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check the size of the vector and the indices accessed.
✗ Incorrect
The vector primes has length n, but the code accesses primes[n], which is out of bounds causing a panic.
🚀 Application
advancedFind Sum of Primes up to 30
What is the sum of all prime numbers up to 30 using the Sieve of Eratosthenes?
Attempts:
2 left
💡 Hint
List primes up to 30 and add them: 2,3,5,7,11,13,17,19,23,29
✗ Incorrect
Sum is 2+3+5+7+11+13+17+19+23+29 = 129. Wait, check carefully: 2+3=5, +5=10, +7=17, +11=28, +13=41, +17=58, +19=77, +23=100, +29=129. So correct sum is 129.
❓ Predict Output
expertOutput of Modified Sieve with Off-by-One
What is the output of this Rust code that attempts to find primes up to 12 but has an off-by-one error in the loop?
DSA Rust
fn sieve(n: usize) -> Vec<usize> {
let mut primes = vec![true; n + 1];
primes[0] = false;
primes[1] = false;
let mut p = 2;
while p * p < n {
if primes[p] {
let mut i = p * p;
while i <= n {
primes[i] = false;
i += p;
}
}
p += 1;
}
primes.iter().enumerate().filter_map(|(i, &is_prime)| if is_prime { Some(i) } else { None }).collect()
}
fn main() {
let result = sieve(12);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check the condition in the while loop: p * p < n instead of p * p <= n.
✗ Incorrect
Because the loop stops when p*p is not less than n, it misses marking multiples of 3 (since 3*3=9 < 12, but 4*4=16 > 12). This causes 11 to remain marked prime correctly, but the loop ends early, so 11 is included. Wait, check carefully: The loop condition is p*p < n, so for p=3, 9 < 12 true, loop runs. For p=4, 16 < 12 false, loop ends. So multiples of 3 are marked. So primes are correct. So output is [2,3,5,7,11]. So option A is correct. But option A misses 11. So correct answer is A.
