Sieve of Eratosthenes: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Mark multiples of each prime; unmarked values are prime.

Mark multiples of surviving prime candidates, starting at p squared because smaller multiples were already handled.

Open the 3D visualization

Sieve of Eratosthenes example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Sieve of Eratosthenes

Many primality queries below one shared limit.

Choosing an alternative

Segmented sieving stores only an interval; trial division targets one number.

Worked example: input and state changes

Example input

40

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        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
      ],
      "active": []
    }
  2. Step 9: Mark 20 as a multiple of prime 2.

    State at this checkpoint
    {
      "values": [
        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
      ],
      "active": [
        18
      ],
      "kind": "sieve",
      "marked": [
        4,
        6,
        8,
        10,
        12,
        14,
        16,
        18,
        20
      ]
    }
  3. Step 18: Mark 38 as a multiple of prime 2.

    State at this checkpoint
    {
      "values": [
        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
      ],
      "active": [
        36
      ],
      "kind": "sieve",
      "marked": [
        4,
        6,
        8,
        10,
        12,
        14,
        16,
        18,
        20,
        22,
        24,
        26,
        28,
        30,
        32,
        34,
        36,
        38
      ]
    }
  4. Step 26: Mark 27 as a multiple of prime 3.

    State at this checkpoint
    {
      "values": [
        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
      ],
      "active": [
        25
      ],
      "kind": "sieve",
      "marked": [
        4,
        6,
        8,
        10,
        12,
        14,
        16,
        18,
        20,
        22,
        24,
        26,
        28,
        30,
        32,
        34,
        36,
        38,
        40,
        9,
        15,
        21,
        27
      ]
    }
  5. Step 35: Primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37.

    State at this checkpoint
    {
      "values": [
        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
      ],
      "active": [],
      "kind": "sieve",
      "marked": [
        4,
        6,
        8,
        10,
        12,
        14,
        16,
        18,
        20,
        22,
        24,
        26,
        28,
        30,
        32,
        34,
        36,
        38,
        40,
        9,
        15,
        21,
        27,
        33,
        39,
        25,
        35
      ]
    }

Expected final state

Primes: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37.

Complexity and input bounds

Family: Number theory. Time complexity: O(n log log n).

Space complexity: O(n) candidate flags.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

One upper bound from 2 to 80.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Zero and one must be excluded even if their flags remain unmarked.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// n ≤ 80. Writes primes to the output array and returns their count. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(primes, n) {
    let marked = Array(256).fill(0);
    for (let p = 2; p * p <= n; p++) {
        if (marked[p] == 0) {
            for (let j = p * p; j <= n; j += p) { marked[j] = 1; }
        }
    }
    let count = 0;
    for (let p = 2; p <= n; p++) { if (marked[p] == 0) { primes[count++] = p; } }
    return count;
}

Source

No original paper has been verified for this entry. This source is a reference text or teaching reference, not an asserted original publication.