📖 Scenario: You are helping a teacher prepare a list of prime numbers for a math class. The teacher wants to find all prime numbers up to a certain number using a fast method called the Sieve of Eratosthenes.
🎯 Goal: Build a program that finds and prints all prime numbers up to a given number using the Sieve of Eratosthenes method.
📋 What You'll Learn
Create an array to mark numbers as prime or not
Use a variable to store the maximum number to check
Implement the sieve algorithm to mark non-prime numbers
Print all prime numbers found
💡 Why This Matters
🌍 Real World
Finding prime numbers quickly is useful in cryptography, computer security, and math research.
💼 Career
Understanding prime number algorithms helps in software development roles involving security, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create the array to mark primes
Create an integer array called is_prime of size 21 to mark numbers from 0 to 20. Initialize all elements to 1 except for index 0 and 1, which should be set to 0 because 0 and 1 are not prime.
DSA C
Hint
Use a for loop from 0 to 20. Set is_prime[0] and is_prime[1] to 0, others to 1.
2
Set the maximum number to check
Create an integer variable called max_num and set it to 20. This will be the highest number we want to check for primes.
DSA C
Hint
Just create int max_num = 20;
3
Implement the sieve algorithm
Use a for loop with variable i from 2 while i * i <= max_num. Inside it, if is_prime[i] is 1, use another for loop with variable j starting from i * i to max_num, incrementing by i. Set is_prime[j] to 0 inside the inner loop to mark multiples as not prime.
DSA C
Hint
Use nested loops: outer from 2 to sqrt(max_num), inner from i*i to max_num stepping by i.
4
Print all prime numbers
Use a for loop with variable i from 2 to max_num. Inside the loop, if is_prime[i] is 1, print i followed by a space. After the loop, print a newline character.
DSA C
Hint
Print all numbers where is_prime[i] is 1, separated by spaces, then print a newline.
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 all prime numbers up to a given limit
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 B
Quick Check:
Prime finding = B [OK]
Hint: Sieve marks multiples to find primes fast [OK]
Common Mistakes:
Confusing prime finding with sorting
Thinking it finds divisors or reverses strings
Assuming it works for negative numbers
2. Which of the following is the correct way to initialize the sieve array in C for finding primes up to n?
easy
A. int sieve[n+1]; for(int i=0; i<=n; i++) sieve[i] = 1;
B. int sieve[n]; for(int i=1; i
C. int sieve[n+1]; for(int i=0; i<=n; i++) sieve[i] = 0; sieve[0] = sieve[1] = 1;
D. int sieve[n+1]; for(int i=0; i<=n; i++) sieve[i] = i;
Solution
Step 1: Understand sieve initialization
The sieve array uses 1 to mark potential primes initially.
Step 2: Check array size and initialization
Array size should be n+1 to include number n, and all set to 1 (true) initially.
Final Answer:
int sieve[n+1]; for(int i=0; i<=n; i++) sieve[i] = 1; -> Option A
Quick Check:
Initialize all to 1 for primes [OK]
Hint: Initialize all to 1 (true) for prime candidates [OK]
Common Mistakes:
Using size n instead of n+1
Initializing with zeros instead of ones
Marking 0 and 1 as prime initially
3. What will be the output of the following C code snippet using Sieve of Eratosthenes to find primes up to 10?