Sieve of Eratosthenes Find All Primes in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to find all prime numbers grows as the input number gets bigger.
How does the work increase when we check more numbers?
Analyze the time complexity of the following code snippet.
function sieveOfEratosthenes(n) {
const primes = new Array(n + 1).fill(true);
primes[0] = false;
primes[1] = false;
for (let i = 2; i * i <= n; i++) {
if (primes[i]) {
for (let j = i * i; j <= n; j += i) {
primes[j] = false;
}
}
}
return primes;
}
This code marks non-prime numbers as false to find all primes up to n.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Nested loops where the outer loop runs from 2 to √n, and the inner loop marks multiples of i as false.
- How many times: Outer loop runs about √n times; inner loop runs fewer times as i grows, marking multiples.
As n grows, the number of operations grows a bit faster than n but much slower than n squared.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20-30 operations |
| 100 | About 500-600 operations |
| 1000 | About 8,000-10,000 operations |
Pattern observation: The operations grow roughly proportional to n times the logarithm of logarithm of n, which is slower than n log n but faster than n.
Time Complexity: O(n log log n)
This means the time grows a bit faster than n but much slower than n squared, making it efficient for large n.
[X] Wrong: "The algorithm checks every number against all smaller numbers, so it must be O(n²)."
[OK] Correct: The inner loop skips many numbers by jumping in steps of i, and the outer loop only goes up to √n, so it does much less work than n squared.
Understanding this time complexity shows you can analyze nested loops carefully and recognize efficient number checking methods, a useful skill in many coding challenges.
"What if we started the inner loop from 2*i instead of i*i? How would the time complexity change?"
Practice
Sieve of Eratosthenes algorithm?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 AQuick Check:
Sieve of Eratosthenes = Find primes [OK]
- Confusing it with sorting algorithms
- Thinking it finds factors instead of primes
- Assuming it works on strings
n?Solution
Step 1: Understand sieve initialization
The sieve array should mark all numbers as true initially, except 0 and 1 which are not prime.Step 2: Check each option
const sieve = new Array(n + 1).fill(true); sieve[0] = sieve[1] = false; correctly creates an array of size n+1 filled with true, then sets 0 and 1 to false.Final Answer:
const sieve = new Array(n + 1).fill(true); sieve[0] = sieve[1] = false; -> Option CQuick Check:
Initialize sieve with true, except 0 and 1 false [OK]
- Using size n instead of n+1
- Filling with false instead of true
- Marking 0 and 1 as true
function sieve(n) {
const sieve = new Array(n + 1).fill(true);
sieve[0] = sieve[1] = false;
for (let i = 2; i * i <= n; i++) {
if (sieve[i]) {
for (let j = i * i; j <= n; j += i) {
sieve[j] = false;
}
}
}
return sieve.map((val, idx) => val ? idx : null).filter(x => x);
}
console.log(sieve(10));Solution
Step 1: Understand sieve marking
The code marks multiples of primes as false, starting from i*i, up to n=10.Step 2: Identify primes up to 10
Primes are 2, 3, 5, 7. Numbers 1, 4, 6, 8, 9, 10 are marked false.Final Answer:
[2, 3, 5, 7] -> Option BQuick Check:
Primes up to 10 = [2, 3, 5, 7] [OK]
- Including 1 as prime
- Including non-primes like 4 or 9
- Off-by-one errors in loops
function sieve(n) {
const sieve = new Array(n).fill(true);
sieve[0] = sieve[1] = false;
for (let i = 2; i <= n; i++) {
if (sieve[i]) {
for (let j = i * 2; j < n; j += i) {
sieve[j] = false;
}
}
}
return sieve.map((val, idx) => val ? idx : null).filter(x => x);
}Solution
Step 1: Check array size
The array is created with size n, so index n is out of range and not included.Step 2: Understand loop bounds
To include number n, array size must be n+1; otherwise, sieve[n] is undefined.Final Answer:
Array size should be n+1 to include number n -> Option AQuick Check:
Array size = n+1 to cover all numbers [OK]
- Using array size n instead of n+1
- Looping outer loop beyond sqrt(n)
- Starting inner loop from i*3 incorrectly
Solution
Step 1: Understand sieve range requirement
The sieve finds primes from 2 up to the limit; partial ranges are not supported directly.Step 2: Filter primes after full sieve
Run sieve up to 100, then select primes >= 50 from the result for correct output.Final Answer:
Run sieve up to 100, then filter primes >= 50 from the result -> Option DQuick Check:
Sieve full range, then filter needed primes [OK]
- Trying to sieve partial ranges directly
- Ignoring primes below 50 without full sieve
- Manually adding primes without verification
Ready to prove it?
Pass the test and this pattern moves unknown
