📖 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 TypeScript program that uses the Sieve of Eratosthenes to find and print all prime numbers up to a given number.
📋 What You'll Learn
Create an array to represent numbers from 0 to the given limit
Use a boolean array to mark numbers as prime or not
Implement the Sieve of Eratosthenes logic to mark multiples as not prime
Print the list of prime numbers found
💡 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 software development roles involving security, algorithms, and performance optimization.
Progress0 / 4 steps
1
Create the number limit and initialize the sieve array
Create a variable called limit and set it to 30. Then create a boolean array called isPrime with size limit + 1 where all values are initially true except for indices 0 and 1 which should be false.
DSA Typescript
Hint
Use new Array(limit + 1).fill(true) to create the boolean array. Then set isPrime[0] and isPrime[1] to false because 0 and 1 are not prime.
2
Set the starting point for the sieve loop
Create a variable called start and set it to 2. This will be the first number to check for marking multiples.
DSA Typescript
Hint
The sieve starts checking from 2, the smallest prime number.
3
Implement the Sieve of Eratosthenes logic
Use a for loop with variable num starting from start up to and including Math.sqrt(limit). Inside it, use an if statement to check if isPrime[num] is true. If yes, use another for loop with variable multiple starting from num * num up to limit, incrementing by num. Inside this inner loop, set isPrime[multiple] to false.
DSA Typescript
Hint
Mark all multiples of each prime number as not prime starting from its square.
4
Print all prime numbers found
Use a for loop with variable i from 2 to limit. Inside the loop, use an if statement to check if isPrime[i] is true. If yes, print i followed by a space on the same line. After the loop, print a newline.
DSA Typescript
Hint
Collect all prime numbers in a string separated by spaces and print it once after the loop.
Practice
(1/5)
1. What is the main purpose of the Sieve of Eratosthenes algorithm?
easy
A. To find the greatest common divisor of two numbers
B. To sort an array of 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, leaving primes unmarked up to the limit.
Final Answer:
To find all prime numbers up to a given limit -> Option C
Quick Check:
Prime finding = To find all prime numbers up to a given limit [OK]
Hint: Sieve marks multiples to find primes fast [OK]
Common Mistakes:
Confusing with sorting algorithms
Thinking it finds divisors or reverses strings
Assuming it works for negative numbers
2. Which of the following is the correct TypeScript syntax to initialize a boolean array named isPrime of size n+1 with all values set to true?
easy
A. const isPrime = Array(n + 1).fill(false);
B. const isPrime = new Array(n + 1).fill(true);
C. const isPrime = new Array(n).fill(true);
D. const isPrime = new Boolean[n + 1];
Solution
Step 1: Check array size and initialization
We need an array of size n+1 to include index n, filled with true values.
Step 2: Verify TypeScript syntax
Using new Array(n + 1).fill(true) correctly creates and fills the array.
Final Answer:
const isPrime = new Array(n + 1).fill(true); -> Option B