What is the main idea behind the Sieve of Eratosthenes?
It is a method to find all prime numbers up to a certain limit by repeatedly marking the multiples of each prime number starting from 2.
Click to reveal answer
intermediate
Why do we start marking multiples from the square of the current prime in the Sieve of Eratosthenes?
Because all smaller multiples of the prime have already been marked by smaller primes, so starting from the square avoids redundant work.
Click to reveal answer
beginner
In the Sieve of Eratosthenes, what does a 'true' value in the boolean array represent?
It means the number at that index is currently considered prime (not marked as a multiple of any smaller prime).
Click to reveal answer
intermediate
What is the time complexity of the Sieve of Eratosthenes algorithm?
The time complexity is approximately O(n log log n), which is very efficient for finding primes up to n.
Click to reveal answer
beginner
How does the Sieve of Eratosthenes differ from checking each number individually for primality?
Instead of checking each number one by one, it marks multiples of primes in bulk, reducing repeated work and improving efficiency.
Click to reveal answer
What number do we start with when using the Sieve of Eratosthenes?
A3
B1
C0
D2
✗ Incorrect
We start with 2 because it is the smallest prime number.
When marking multiples of a prime p, from which number do we start marking?
Ap + 1
B2 * p
Cp * p
D1
✗ Incorrect
We start marking from p squared to avoid marking multiples already handled by smaller primes.
What does a false value in the sieve array mean after processing?
AThe number is prime
BThe number is composite (not prime)
CThe number is zero
DThe number is negative
✗ Incorrect
False means the number was marked as a multiple of a prime, so it is not prime.
Which of these is NOT a benefit of the Sieve of Eratosthenes?
AChecks primality of a single number instantly
BUses a simple boolean array
CAvoids repeated division checks
DFinds all primes up to n efficiently
✗ Incorrect
The sieve finds all primes up to n, but does not instantly check a single number's primality without preprocessing.
What is the space complexity of the Sieve of Eratosthenes for n numbers?
AO(n)
BO(1)
CO(log n)
DO(n^2)
✗ Incorrect
The sieve uses a boolean array of size n + 1, so space complexity is O(n).
Explain step-by-step how the Sieve of Eratosthenes finds all prime numbers up to a given number n.
Think about marking multiples and skipping already marked numbers.
You got /6 concepts.
Describe why the Sieve of Eratosthenes is more efficient than checking each number's primality individually.
Focus on how the algorithm reduces repeated checks.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of the Sieve of Eratosthenes algorithm?
easy
A. To find all prime numbers up to a given limit
B. To sort an array of numbers
C. To find the greatest common divisor of two numbers
D. To reverse a string
Solution
Step 1: Understand the algorithm's goal
The Sieve of Eratosthenes is designed to identify prime numbers efficiently.
Step 2: Recognize the output
It marks multiples of primes as non-prime to find all primes up to a limit.
Final Answer:
To find all prime numbers up to a given limit -> Option A
Quick Check:
Sieve of Eratosthenes = Find primes [OK]
Hint: Remember: sieve filters out non-primes to find primes [OK]
Common Mistakes:
Confusing it with sorting algorithms
Thinking it finds factors instead of primes
Assuming it works on strings
2. Which of the following JavaScript code snippets correctly initializes the sieve array for numbers up to n?
easy
A. const sieve = Array(n).fill(true); sieve[0] = sieve[1] = true;
B. const sieve = new Array(n).fill(false); sieve[0] = sieve[1] = true;
C. const sieve = new Array(n + 1).fill(true); sieve[0] = sieve[1] = false;
D. const sieve = new Array(n + 1).fill(false); sieve[0] = sieve[1] = false;
Solution
Step 1: Understand sieve initialization
The sieve array should mark all numbers as true initially, except 0 and 1 which are not prime.
Step 2: Check each option
const sieve = new Array(n + 1).fill(true); sieve[0] = sieve[1] = false; correctly creates an array of size n+1 filled with true, then sets 0 and 1 to false.
Final Answer:
const sieve = new Array(n + 1).fill(true); sieve[0] = sieve[1] = false; -> Option C
Quick Check:
Initialize sieve with true, except 0 and 1 false [OK]
Hint: Sieve size is n+1; 0 and 1 are not prime [OK]
Common Mistakes:
Using size n instead of n+1
Filling with false instead of true
Marking 0 and 1 as true
3. What will be the output of the following code snippet?
function sieve(n) {
const sieve = new Array(n + 1).fill(true);
sieve[0] = sieve[1] = false;
for (let i = 2; i * i <= n; i++) {
if (sieve[i]) {
for (let j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
}
return sieve.map((val, idx) => val ? idx : null).filter(x => x);
}
console.log(sieve(10));
medium
A. [1, 2, 3, 5, 7]
B. [2, 3, 5, 7]
C. [2, 3, 4, 5, 7]
D. [2, 3, 5, 7, 9]
Solution
Step 1: Understand sieve marking
The code marks multiples of primes as false, starting from i*i, up to n=10.
Step 2: Identify primes up to 10
Primes are 2, 3, 5, 7. Numbers 1, 4, 6, 8, 9, 10 are marked false.
Final Answer:
[2, 3, 5, 7] -> Option B
Quick Check:
Primes up to 10 = [2, 3, 5, 7] [OK]
Hint: Primes under 10 are 2,3,5,7 only [OK]
Common Mistakes:
Including 1 as prime
Including non-primes like 4 or 9
Off-by-one errors in loops
4. Identify the error in the following Sieve of Eratosthenes implementation:
function sieve(n) {
const sieve = new Array(n).fill(true);
sieve[0] = sieve[1] = false;
for (let i = 2; i <= n; i++) {
if (sieve[i]) {
for (let j = i * 2; j < n; j += i) {
sieve[j] = false;
}
}
}
return sieve.map((val, idx) => val ? idx : null).filter(x => x);
}
medium
A. Array size should be n+1 to include number n
B. Inner loop should start from i*3 instead of i*2
C. Outer loop should run only up to sqrt(n)
D. sieve[0] and sieve[1] should be true
Solution
Step 1: Check array size
The array is created with size n, so index n is out of range and not included.
Step 2: Understand loop bounds
To include number n, array size must be n+1; otherwise, sieve[n] is undefined.
Final Answer:
Array size should be n+1 to include number n -> Option A
Quick Check:
Array size = n+1 to cover all numbers [OK]
Hint: Array size must cover all indices up to n [OK]
Common Mistakes:
Using array size n instead of n+1
Looping outer loop beyond sqrt(n)
Starting inner loop from i*3 incorrectly
5. You want to find all prime numbers between 50 and 100 using the Sieve of Eratosthenes. Which approach is correct?
hard
A. Run sieve up to 150 and ignore primes below 50
B. Run sieve only from 50 to 100 directly
C. Run sieve up to 50, then add primes from 51 to 100 manually
D. Run sieve up to 100, then filter primes >= 50 from the result
Solution
Step 1: Understand sieve range requirement
The sieve finds primes from 2 up to the limit; partial ranges are not supported directly.
Step 2: Filter primes after full sieve
Run sieve up to 100, then select primes >= 50 from the result for correct output.
Final Answer:
Run sieve up to 100, then filter primes >= 50 from the result -> Option D
Quick Check:
Sieve full range, then filter needed primes [OK]
Hint: Sieve full range, then filter start point [OK]
Common Mistakes:
Trying to sieve partial ranges directly
Ignoring primes below 50 without full sieve
Manually adding primes without verification
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.