Segmented prime sieve: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Sieve only a bounded interval using primes up to the square root of its upper bound.

Find small primes through sqrt(R), then mark only their multiples inside the requested interval.

Open the 3D visualization

Segmented prime sieve example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Segmented prime sieve

Prime queries in a narrow interval.

Choosing an alternative

An ordinary sieve stores all candidates below one limit.

Worked example: input and state changes

Example input

100,130

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: 0 and 1 are not prime.

    State at this checkpoint
    {
      "values": [
        100,
        101,
        102,
        103,
        104,
        105,
        106,
        107,
        108,
        109,
        110,
        111,
        112,
        113,
        114,
        115,
        116,
        117,
        118,
        119,
        120,
        121,
        122,
        123,
        124,
        125,
        126,
        127,
        128,
        129,
        130
      ],
      "active": [],
      "kind": "sieve",
      "marked": []
    }
  2. Step 10: Prime 2: mark 118 in the segment.

    State at this checkpoint
    {
      "values": [
        100,
        101,
        102,
        103,
        104,
        105,
        106,
        107,
        108,
        109,
        110,
        111,
        112,
        113,
        114,
        115,
        116,
        117,
        118,
        119,
        120,
        121,
        122,
        123,
        124,
        125,
        126,
        127,
        128,
        129,
        130
      ],
      "active": [
        18
      ],
      "kind": "sieve",
      "marked": [
        100,
        102,
        104,
        106,
        108,
        110,
        112,
        114,
        116,
        118
      ]
    }
  3. Step 20: Prime 3: mark 111 in the segment.

    State at this checkpoint
    {
      "values": [
        100,
        101,
        102,
        103,
        104,
        105,
        106,
        107,
        108,
        109,
        110,
        111,
        112,
        113,
        114,
        115,
        116,
        117,
        118,
        119,
        120,
        121,
        122,
        123,
        124,
        125,
        126,
        127,
        128,
        129,
        130
      ],
      "active": [
        11
      ],
      "kind": "sieve",
      "marked": [
        100,
        102,
        104,
        106,
        108,
        110,
        112,
        114,
        116,
        118,
        120,
        122,
        124,
        126,
        128,
        130,
        105,
        111
      ]
    }
  4. Step 29: Prime 5: mark 110 in the segment.

    State at this checkpoint
    {
      "values": [
        100,
        101,
        102,
        103,
        104,
        105,
        106,
        107,
        108,
        109,
        110,
        111,
        112,
        113,
        114,
        115,
        116,
        117,
        118,
        119,
        120,
        121,
        122,
        123,
        124,
        125,
        126,
        127,
        128,
        129,
        130
      ],
      "active": [
        10
      ],
      "kind": "sieve",
      "marked": [
        100,
        102,
        104,
        106,
        108,
        110,
        112,
        114,
        116,
        118,
        120,
        122,
        124,
        126,
        128,
        130,
        105,
        111,
        117,
        123,
        129
      ]
    }
  5. Step 39: Primes: 101, 103, 107, 109, 113, 127.

    State at this checkpoint
    {
      "values": [
        100,
        101,
        102,
        103,
        104,
        105,
        106,
        107,
        108,
        109,
        110,
        111,
        112,
        113,
        114,
        115,
        116,
        117,
        118,
        119,
        120,
        121,
        122,
        123,
        124,
        125,
        126,
        127,
        128,
        129,
        130
      ],
      "active": [],
      "kind": "sieve",
      "marked": [
        100,
        102,
        104,
        106,
        108,
        110,
        112,
        114,
        116,
        118,
        120,
        122,
        124,
        126,
        128,
        130,
        105,
        111,
        117,
        123,
        129,
        115,
        125,
        119,
        121
      ],
      "primes": [
        101,
        103,
        107,
        109,
        113,
        127
      ]
    }

Expected final state

Primes: 101, 103, 107, 109, 113, 127.

Complexity and input bounds

Family: Number theory. Time complexity: O(√R log log R + range log log R).

Space complexity: O(sqrt(R) + interval length) 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.

L,R with 0 ≤ L ≤ R ≤ 9999 and at most 100 numbers.

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

Begin at max(p squared, first interval multiple), and exclude zero and one.

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
13
// 0 ≤ left ≤ right ≤ 9999, interval size ≤ 100. Writes ascending primes and returns 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(left, right, output) {
    let base = Array(256).fill(0); let marked = Array(256).fill(0);
    for (let p = 2; p * p <= right; p++) {
        if (base[p] != 0) { continue; }
        for (let j = p * p; j < 100; j += p) { base[j] = 1; }
        let start = (Math.floor((left + p - 1) / p)) * p; if (start < p * p) { start = p * p; }
        for (let j = start; j <= right; j += p) { marked[j - left] = 1; }
    }
    let count = 0;
    for (let value = left; value <= right; value++) { if (value >= 2 && marked[value - left] == 0) { output[count++] = value; } }
    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.