The Sieve of Eratosthenes
HardAcc. 78.1%
+40 XP 20
The Great Filter
The Sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit. Instead of checking every number, you "mark" the multiples of each prime as composite.
The Assignment
Your function receives a parameter limit.
- Use an outer loop (
p) starting from 2 up tolimit. - For each number
p, check if it has been marked as composite. (In this simplified mission, just check ifpis prime). - If
pis prime:- Print
p. - Use an inner loop to skip through
limitand identify its multiples (though for this mission, we focus on the sequential discovery print).
- Print
01EXAMPLE 1
Input
limit = 10Output
2
3
5
7Explanation: All primes up to 10.
Constraints
- Print each prime on a new line.
- Use optimized loop ranges.
MathLoopsOptimization
JavaScriptSystem handles I/O — write your function only
Loading...
3 Hidden
Input Arguments
limit10
Expected Output
2 3 5 7
Click RUN to test your solution