Math

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Mark multiples of each prime as composite.

Step 1 / 5
Problem shape
Current lesson

Sieve of Eratosthenes Lab

Math Beginner

Sieve of Eratosthenes

Welcome to the Sieve of Eratosthenes algorithm lab. Mark multiples of each prime as composite. Use it when: Use to generate all primes up to a limit. The time complexity is O(n log log n), and the space complexity is O(n). The pseudocode is: isPrime[0] = isPrime[1] = false for p from 2 while p * p <= n if isPrime[p] mark multiples of p composite As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n log log n)

Space

O(n)

Pseudocode

isPrime[0] = isPrime[1] = false
for p from 2 while p * p <= n
  if isPrime[p]
    mark multiples of p composite
Open deeper structure lesson
1.0x