Pascal triangle: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Each entry is the sum of the two entries above it; the outer edges are one.

Each interior value adds two adjacent values from the previous row, with ones on both boundaries.

Open the 3D visualization

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

When to use Pascal triangle

Visualizing binomial recurrences.

Choosing an alternative

A single binomial calculation can retain only one row.

Worked example: input and state changes

Example input

6

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: Pascal triangle starts empty.

    State at this checkpoint
    {
      "values": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [],
      "kind": "matrix",
      "columns": 6
    }
  2. Step 6: Triangle entry [2,2] = 1.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1,
        2,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        14
      ],
      "kind": "matrix",
      "columns": 6
    }
  3. Step 11: Triangle entry [4,0] = 1.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1,
        2,
        1,
        0,
        0,
        0,
        1,
        3,
        3,
        1,
        0,
        0,
        1,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        24
      ],
      "kind": "matrix",
      "columns": 6
    }
  4. Step 17: Triangle entry [5,1] = 5.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1,
        2,
        1,
        0,
        0,
        0,
        1,
        3,
        3,
        1,
        0,
        0,
        1,
        4,
        6,
        4,
        1,
        0,
        1,
        5,
        0,
        0,
        0,
        0
      ],
      "active": [
        31
      ],
      "kind": "matrix",
      "columns": 6
    }
  5. Step 22: Pascal triangle complete: 6 rows.

    State at this checkpoint
    {
      "values": [
        1,
        0,
        0,
        0,
        0,
        0,
        1,
        1,
        0,
        0,
        0,
        0,
        1,
        2,
        1,
        0,
        0,
        0,
        1,
        3,
        3,
        1,
        0,
        0,
        1,
        4,
        6,
        4,
        1,
        0,
        1,
        5,
        10,
        10,
        5,
        1
      ],
      "active": [],
      "kind": "matrix",
      "columns": 6
    }

Expected final state

Pascal triangle complete: 6 rows.

Complexity and input bounds

Family: Number theory. Time complexity: O(rows²).

Space complexity: O(rows squared) full-triangle storage.

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 row count from 1 to 12. Cells outside the triangle remain zero.

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

The number of displayed rows differs from the highest zero-based row index.

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
// rows from 1 to 12. Writes a square row-major Pascal triangle matrix, with zeros above the diagonal. 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(rows, output) {
    for (let i = 0; i < rows * rows; i++) { output[i] = 0; }
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c <= r; c++) {
            let i = r * rows + c;
            if (c == 0 || c == r) { output[i] = 1; } else { output[i] = output[i - rows - 1] + output[i - rows]; }
        }
    }
    return 0;
}

Source

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