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.

  1. Use an outer loop (p) starting from 2 up to limit.
  2. For each number p, check if it has been marked as composite. (In this simplified mission, just check if p is prime).
  3. If p is prime:
    • Print p.
    • Use an inner loop to skip through limit and identify its multiples (though for this mission, we focus on the sequential discovery print).

01EXAMPLE 1

Inputlimit = 10
Output2 3 5 7

Explanation: All primes up to 10.

Constraints

  • Print each prime on a new line.
  • Use optimized loop ranges.
MathLoopsOptimization
JavaScript
Loading...
3 Hidden

Input Arguments

limit10

Expected Output

2
3
5
7

Click RUN to test your solution