The Sieve of Eratosthenes is one of the oldest and most elegant algorithms in mathematics. It finds every prime up to N by crossing out multiples rather than testing each candidate individually. This visualizer runs the sieve step by step on a colour-coded number grid, so you can watch exactly which composites each prime eliminates before the next one takes over.
Enter any limit from 2 to 500, press Start, and the grid animates at your chosen speed. Each orange cell is the prime currently doing the eliminating; red cells are being crossed out in real time; green cells are confirmed primes. Press Step to advance one elimination at a time, or drag the speed slider before pressing Start for a slower walk-through. Load Sample picks a random limit between 2 and 500 and resets the grid so you can run a fresh sieve immediately.
Getting a Sieve Running
- Type a limit N between 2 and 500 and press Enter (or click away) to apply it. The grid renders immediately.
- Press Start to begin the animation, Pause to freeze it, or Step to advance exactly one elimination at a time.
- Drag the Speed slider before or after pressing Start. Dragging it while running automatically pauses, so you can set the new pace before resuming.
- Once the animation finishes, the prime list and key statistics appear below the grid. Click Copy to grab all primes as a comma-separated string for use in code or a document.
How the Algorithm Works
- Start with all integers from 2 to N marked as potentially prime. 0 and 1 are neither prime nor composite and are shown as grey from the start.
- The first unmarked number (2) is prime. Cross out every multiple of 2 starting at 2 squared = 4, because smaller multiples do not exist yet.
- Move to the next unmarked number (3) and cross out its multiples from 9 onward. Repeat until the current prime exceeds the square root of N.
- Every remaining unconfirmed number is prime. No further crossing is needed because any composite above the square root of N must have a prime factor at or below it, already processed.
Accuracy and Complexity Limits
- The implementation is exact for all integers in the 2 to 500 range: no probabilistic tests, no rounding. Every number is definitively prime or composite.
- Time complexity is O(N log log N), practically linear up to very large N. At N=500 the browser completes all steps in under a millisecond. The animation delay is entirely artificial and purely for visualisation.
- Elimination starts at p squared rather than 2p because all multiples below p squared have a smaller prime factor already crossed out. If you step through slowly, notice that 5 starts at 25, not 10. That is this optimisation in action.
- The grid caps at N=500 to keep every cell readable. Above that a small cell fits fine numerically but loses the educational value of seeing which specific number is being eliminated.