📖 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 n. You will use the Sieve of Eratosthenes method, which is a simple and efficient way to find primes.
🎯 Goal: Build a Go program that uses the Sieve of Eratosthenes algorithm to find and print all prime numbers up to a given number n.
📋 What You'll Learn
Create a boolean slice to represent numbers from 0 to n
Initialize the slice to mark all numbers as potentially prime except 0 and 1
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 number theory research.
💼 Career
Understanding prime number algorithms helps in roles involving algorithm design, security engineering, and software development.
Progress0 / 4 steps
1
Create the boolean slice for numbers up to n
Create an integer variable called n and set it to 30. Then create a boolean slice called isPrime with length n+1.
DSA Go
Hint
Use make([]bool, n+1) to create the boolean slice for numbers from 0 to n.
2
Initialize the slice to mark all numbers as prime except 0 and 1
Use a for loop with variable i from 2 to n inclusive to set isPrime[i] to true.
DSA Go
Hint
Start from 2 because 0 and 1 are not prime numbers.
3
Mark multiples of each prime as not prime
Use a for loop with variable p from 2 to int(float64(n).Sqrt()) (use p * p <= n as condition). Inside it, check if isPrime[p] is true. If yes, use another for loop with variable multiple starting from p * p to n incrementing by p to set isPrime[multiple] to false.
DSA Go
Hint
Start marking multiples from p * p because smaller multiples were already marked by smaller primes.
4
Print all prime numbers up to n
Use a for loop with variable i from 2 to n inclusive. Inside the loop, if isPrime[i] is true, print i followed by a space. After the loop, print a newline.
DSA Go
Hint
Print all numbers marked as prime 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 a list of numbers in ascending order
B. To find all prime numbers up to a given limit
C. To find the greatest common divisor of two numbers
D. To calculate the factorial of a number
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 a limit.
Final Answer:
To find all prime numbers up to a given limit -> Option B
Quick Check:
Sieve of Eratosthenes = Find primes [OK]
Hint: Sieve marks multiples to find primes fast [OK]
Common Mistakes:
Confusing it with sorting algorithms
Thinking it calculates factorials
Assuming it finds divisors instead of primes
2. Which of the following Go code snippets correctly initializes a boolean slice for the sieve up to n?
easy
A. primes := make([]bool, n-1)
B. primes := make([]int, n)
C. primes := []bool{n}
D. primes := make([]bool, n+1)
Solution
Step 1: Identify correct slice type and size
We need a boolean slice of size n+1 to include index n.
Step 2: Check Go syntax for slice creation
make([]bool, n+1) correctly creates a boolean slice of length n+1.