📖 Scenario: You want to find all prime numbers up to a certain number, like finding all the special coins in a big pile. The Sieve of Eratosthenes is a smart way to do this quickly.
🎯 Goal: Build a program that uses the Sieve of Eratosthenes method to find and print all prime numbers up to a given number.
📋 What You'll Learn
Create a boolean list to mark numbers as prime or not
Use a loop to mark multiples of each prime as not prime
Print all numbers that remain marked as prime
💡 Why This Matters
🌍 Real World
Finding prime numbers is useful in cryptography, computer security, and solving math problems quickly.
💼 Career
Understanding the Sieve of Eratosthenes helps in algorithm design and optimization tasks in software development and data science.
Progress0 / 4 steps
1
Create the boolean list for marking primes
Create a boolean vector called is_prime of size n + 1 and set all values to true. Also create an integer variable n and set it to 30.
DSA C++
Hint
Use std::vector is_prime(n + 1, true); to create the list.
2
Initialize 0 and 1 as not prime
Set is_prime[0] and is_prime[1] to false because 0 and 1 are not prime numbers.
DSA C++
Hint
Remember to mark 0 and 1 as not prime by setting their positions to false.
3
Mark multiples of primes as not prime
Use a for loop with variable i from 2 to n. Inside it, if is_prime[i] is true, use another for loop with variable j starting from i * i to n in steps of i to set is_prime[j] to false.
DSA C++
Hint
Use nested loops: outer loop for each number, inner loop to mark multiples as not prime.
4
Print all prime numbers
Use a for loop with variable i from 2 to n. Inside it, if is_prime[i] is true, print i followed by a space.
DSA C++
Hint
Print all numbers marked true in is_prime separated by spaces.
Practice
(1/5)
1. What is the main purpose of the Sieve of Eratosthenes algorithm?
easy
A. To calculate the factorial of a number
B. To sort an array of numbers
C. To find the greatest common divisor of two numbers
D. To find all prime numbers up to a given limit
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, leaving primes unmarked up to the limit.
Final Answer:
To find all prime numbers up to a given limit -> Option D
Quick Check:
Sieve finds primes = D [OK]
Hint: Sieve marks multiples to find primes fast [OK]
Common Mistakes:
Confusing sieve with sorting algorithms
Thinking it calculates factorial or gcd
Assuming it finds primes beyond the limit
2. Which of the following C++ code snippets correctly initializes the sieve array for numbers up to n?
easy
A. std::vector<bool> sieve(n+1, true); sieve[0] = sieve[1] = false;
B. std::vector<int> sieve(n, 0); sieve[0] = sieve[1] = 1;
C. bool sieve[n]; for(int i=0; i
D. std::vector<bool> sieve(n+1, false); sieve[0] = sieve[1] = true;
Solution
Step 1: Check initialization for prime marking
The sieve should start with all true except 0 and 1, which are not prime.
Step 2: Validate code correctness
std::vector<bool> sieve(n+1, true); sieve[0] = sieve[1] = false; uses a vector<bool> with size n+1, sets all true, then marks 0 and 1 false correctly.
Hint: Start sieve with true, mark 0 and 1 false [OK]
Common Mistakes:
Using wrong data type for sieve array
Not marking 0 and 1 as non-prime
Initializing all false instead of true
3. What will be the output primes printed by the following C++ code snippet using Sieve of Eratosthenes for n = 10?
int n = 10;
std::vector<bool> sieve(n+1, true);
sieve[0] = sieve[1] = false;
for (int i = 2; i * i <= n; i++) {
if (sieve[i]) {
for (int j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
}
for (int i = 2; i <= n; i++) {
if (sieve[i]) std::cout << i << " ";
}
medium
A. 2 3 4 5 7
B. 2 3 5 7
C. 1 2 3 5 7
D. 2 3 5 7 9
Solution
Step 1: Run sieve marking multiples
Multiples of 2 (4,6,8,10) and 3 (9) are marked false.
Step 2: Identify primes left true
Numbers 2, 3, 5, 7 remain true and are printed.
Final Answer:
2 3 5 7 -> Option B
Quick Check:
Primes up to 10 = 2 3 5 7 [OK]
Hint: Mark multiples starting from i*i to avoid repeats [OK]
Common Mistakes:
Including non-primes like 4 or 9
Printing 1 as prime
Not starting inner loop from i*i
4. Identify the error in the following Sieve of Eratosthenes code snippet and select the fix:
int n = 20;
std::vector<bool> sieve(n+1, true);
sieve[0] = sieve[1] = false;
for (int i = 2; i <= n; i++) {
if (sieve[i]) {
for (int j = i * 2; j <= n; j += i) {
sieve[j] = false;
}
}
}
medium
A. Start inner loop from j = i + 1
B. Initialize sieve with false instead of true
C. Change outer loop condition to i * i <= n
D. Remove sieve[0] and sieve[1] assignments
Solution
Step 1: Analyze loop conditions
The outer loop runs up to n, which is inefficient and unnecessary.
Step 2: Correct loop to optimize marking
Changing outer loop to run while i * i <= n avoids redundant checks and improves performance.
Final Answer:
Change outer loop condition to i * i <= n -> Option C
Quick Check:
Outer loop limit = i*i <= n [OK]
Hint: Outer loop runs till i*i <= n for efficiency [OK]
Common Mistakes:
Running outer loop till n wastes time
Starting inner loop incorrectly
Not marking 0 and 1 false
5. You want to find all prime numbers up to n = 50 using the Sieve of Eratosthenes. After running the sieve, how many primes will be found?