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 efficient for finding primes up to large numbers.
Click to reveal answer
beginner
How does the Sieve of Eratosthenes help in real-life scenarios?
It helps quickly find prime numbers which are important in cryptography, hashing, and number theory applications.
Click to reveal answer
What is the first prime number used in the Sieve of Eratosthenes?
A1
B0
C2
D3
✗ Incorrect
The sieve starts with 2, the smallest prime number.
When marking multiples of a prime p, from which number do we start marking?
Ap * p
B2 * p
Cp + 1
D1
✗ Incorrect
We start marking from p squared to avoid marking multiples already handled by smaller primes.
What data structure is commonly used to keep track of prime candidates in the sieve?
ABoolean array
BQueue
CStack
DLinked list
✗ Incorrect
A boolean array marks whether each number is prime (true) or not (false).
Which of these numbers will be marked as not prime when processing prime 3?
A6
B9
C12
DAll of the above
✗ Incorrect
All multiples of 3 (6, 9, 12, etc.) are marked as not prime.
What is the stopping condition for the outer loop in the sieve?
AWhen current prime > n
BWhen current prime > sqrt(n)
CWhen current prime > n/2
DWhen current prime > 1
✗ Incorrect
We only need to check primes up to the square root of n.
Explain how the Sieve of Eratosthenes finds all prime numbers up to a given number n.
Think about crossing out multiples step by step.
You got /7 concepts.
Describe why the Sieve of Eratosthenes is more efficient than checking each number individually for primality.
Focus on how it reduces repeated checks.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Sieve of Eratosthenes algorithm?
easy
A. To sort an array of numbers
B. To find the greatest common divisor of two numbers
C. To find all prime numbers up to a given limit
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 C
Quick Check:
Sieve of Eratosthenes = Find primes [OK]
Hint: Remember: Sieve filters out non-primes to list 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 C# code snippets correctly initializes a boolean array for the sieve to find primes up to n?
easy
A. bool[] isPrime = new bool[n + 1]; for(int i = 0; i <= n; i++) isPrime[i] = false;
B. bool[] isPrime = new bool[n + 1]; for(int i = 2; i <= n; i++) isPrime[i] = true;
C. bool[] isPrime = new bool[n]; for(int i = 0; i < n; i++) isPrime[i] = false;
D. bool[] isPrime = new bool[n]; for(int i = 2; i < n; i++) isPrime[i] = true;
Solution
Step 1: Check array size and initialization
The array must be of size n+1 to include index n, and primes start from 2.
Step 2: Verify initialization values
All entries from 2 to n should be set to true initially to mark potential primes.
Final Answer:
bool[] isPrime = new bool[n + 1]; for(int i = 2; i <= n; i++) isPrime[i] = true; -> Option B
Quick Check:
Array size n+1 and true from 2 to n [OK]
Hint: Array size n+1 and true from 2 to n for primes [OK]
Common Mistakes:
Using array size n instead of n+1
Initializing all values to false
Starting initialization from 0 or 1
3. What will be the output list of primes when running the Sieve of Eratosthenes for n = 10?
medium
A. [2, 3, 5, 7, 9]
B. [1, 2, 3, 5, 7]
C. [2, 3, 4, 5, 7]
D. [2, 3, 5, 7]
Solution
Step 1: Identify primes up to 10
Primes are numbers greater than 1 with no divisors other than 1 and themselves: 2, 3, 5, 7.
Step 2: Exclude non-primes
Numbers 1, 4, 6, 8, 9, 10 are not prime.
Final Answer:
[2, 3, 5, 7] -> Option D
Quick Check:
Primes up to 10 = [2, 3, 5, 7] [OK]
Hint: Remember primes > 1 and exclude multiples [OK]
Common Mistakes:
Including 1 as prime
Including even numbers > 2
Including non-prime odd numbers like 9
4. Consider this code snippet for the sieve:
for (int i = 2; i * i <= n; i++) {
if (isPrime[i]) {
for (int j = i * 2; j <= n; j += i) {
isPrime[j] = false;
}
}
}
Which of the following improves the efficiency of this code?
medium
A. The inner loop should start from i * i, not i * 2
B. The outer loop condition should be i <= n, not i * i <= n
C. The inner loop increments should be j += 1, not j += i
D. The isPrime array should be initialized with false, not true
Solution
Step 1: Understand sieve optimization
Marking multiples starts from i squared because smaller multiples were marked by smaller primes.
Step 2: Identify suboptimal start of inner loop
Starting from i*2 repeats marking multiples already handled; correct start is i*i.
Final Answer:
The inner loop should start from i * i, not i * 2 -> Option A
Quick Check:
Start inner loop at i*i for efficiency [OK]
Hint: Start marking multiples from i squared, not double i [OK]
Common Mistakes:
Starting inner loop at i*2 causing redundant work
Changing outer loop condition incorrectly
Incorrect increment steps in inner loop
5. You want to find all prime numbers up to n = 30 using the Sieve of Eratosthenes. After running the sieve, which of the following lists correctly shows the primes and their count?