Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a vector of booleans initialized to true for marking primes.
DSA Rust
let mut is_prime = vec![[1]; n + 1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using false will mark all numbers as non-prime initially.
Using 0 or 1 are not boolean values.
✗ Incorrect
We initialize all numbers as true (potential primes) before marking non-primes false.
2fill in blank
mediumComplete the for loop to start crossing out multiples from the square of the current prime candidate.
DSA Rust
for multiple in (i * i..=n).step_by([1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 as step skips some multiples.
Using i + 1 or 1 steps incorrectly.
✗ Incorrect
We start crossing out multiples from i squared, stepping by i each time.
3fill in blank
hardFix the error in the loop condition to only check up to the square root of n.
DSA Rust
for i in 2..=[1] {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking all numbers up to n is inefficient.
Using integer division n / 2 is not precise.
✗ Incorrect
We only need to check primes up to the square root of n for efficiency.
4fill in blank
hardFill both blanks to mark multiples as non-prime inside the loop.
DSA Rust
if is_prime[i] { for multiple in ([1]..=n).step_by([2]) { is_prime[multiple] = false; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from i + 1 misses some multiples.
Using step size 2 skips multiples.
✗ Incorrect
Start crossing out from i squared and step by i to mark multiples.
5fill in blank
hardFill all three blanks to collect all primes into a vector.
DSA Rust
let primes: Vec<usize> = (2..=n) .filter(|&[1]| is_prime[[2]]) .map(|[3]| [3]) .collect();
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names causes errors.
Using x or i inconsistently breaks the code.
✗ Incorrect
Use the same variable name num to filter and map primes correctly.
