UtilityToolsLab

© 2026 UtilityToolsLab. Built and maintained by the UtilityToolsLab Team.

Free eBooks·About·Changelog·Privacy Policy·Terms of Service·Report a bug
HomeCompetitive ProgrammingSieve Visualizer

Related Tools

Code FormatterMatrix GeneratorComplexity CalcBit VisualizerBitmask PlannerPrime FactorsMEX CalcInterval MergerMatrix RotationPBDS GeneratorSegment TreeGraph VisualizerStress TesterModulo CalcConvex HullPath FinderOffline JudgeBig-O AnalyzerCombinatoricsDP Table BuilderExtended GCDBinary SearchSorting VisualizerSparse Table RMQUnion-Find DSU

Sieve of Eratosthenes Visualizer

Animate the Sieve of Eratosthenes step by step on a colour-coded grid. Enter any limit up to 500, step through each elimination, and copy the prime list.

You Might Also Like

All Competitive Programming

Code Formatter

Format and beautify C++, Java, and Python code with consistent indentation. 100% client-side — no code leaves your browser.

Matrix Generator

Generate grid/matrix inputs for competitive programming. Random, zeros, identity, or sequential fill. Outputs in multiple formats.

Complexity Calc

Estimate Big-O operations and runtime for any N. Interactive reference table for all complexity classes from O(1) to O(N!).

Bit Visualizer

Toggle 32/64-bit grids. Click bits to flip them live and see decimal recalculate. Shows popcountll, clzll, ctzll, and MSB instantly.

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

  1. Type a limit N between 2 and 500 and press Enter (or click away) to apply it. The grid renders immediately.
  2. Press Start to begin the animation, Pause to freeze it, or Step to advance exactly one elimination at a time.
  3. Drag the Speed slider before or after pressing Start. Dragging it while running automatically pauses, so you can set the new pace before resuming.
  4. 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.

Range: 2–500. Press Enter or click away to apply.

medium
Grid (2 – 50)
prime eliminating current prime composite
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

How it works

Start with all integers 2–N marked as potentially prime. Find the first unmarked number (2). Cross out every multiple of 2 starting from 2² = 4. Move to the next unmarked number (3) and cross out its multiples from 3² = 9. Continue until the current prime exceeds √N — all remaining unmarked numbers are prime. Time complexity: O(N log log N).