Resource · Guide or article
Sieve of Eratosthenes
Step-by-step description of the Sieve of Eratosthenes for listing every prime up to n, a frequent easy-to-medium coding question.
Reviewed
Open the original: Sieve of Eratosthenes (opens the original site in a new tab)Add to a roadmap
Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.
Source content: English
The Sieve of Eratosthenes is an algorithm for finding all prime numbers up to some limit n. It is attributed to Eratosthenes of Cyrene, an ancient Greek mathematician. When the algorithm terminates, the numbers remaining true in the array are all the primes below n.
PrepKit overview
PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.
- Suited to
- The source does not state a level
- Format
- Reference
What it covers
- How it works: five numbered steps using a boolean array of n + 1 slots.
- The optimization of starting to cross out multiples at p * p instead of 2 * p.
- Complexity stated as O(n log(log n)).
How to use it
PrepKit study advice, not a claim made by the source.
Implement the sieve for n = 100 from the numbered steps, then add the p * p start and explain why it skips no primes.
Scope and limitations
Brief; the example is an animation and the only reference is Wikipedia. No space complexity or practice problems.
Access and format
English README, free on GitHub in the MIT-licensed trekhleb/javascript-algorithms repo.
Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88
Evidence behind this overview
- src/algorithms/math/sieve-of-eratosthenes/README.md
Pages that include this
Get the PrepKit newsletter
Optional. You don't need it to use PrepKit.