Fibonacci (tabulation): Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Build each value using the two preceding values.

Tabulate values from small indices upward instead of repeatedly recomputing overlapping recursive subproblems.

Open the 3D visualization

Fibonacci (tabulation) example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Fibonacci (tabulation)

Introducing recurrences and dynamic programming.

Choosing an alternative

Rolling two values reduces storage when the whole table is unnecessary.

Worked example: input and state changes

Example input

12

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": [
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": []
    }
  2. Step 3: F(3) = 1 + 1 = 2.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        2,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        3
      ]
    }
  3. Step 6: F(6) = 5 + 3 = 8.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        2,
        3,
        5,
        8,
        0,
        0,
        0,
        0,
        0,
        0
      ],
      "active": [
        6
      ]
    }
  4. Step 9: F(9) = 21 + 13 = 34.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        2,
        3,
        5,
        8,
        13,
        21,
        34,
        0,
        0,
        0
      ],
      "active": [
        9
      ]
    }
  5. Step 12: F(12) = 89 + 55 = 144.

    State at this checkpoint
    {
      "values": [
        0,
        1,
        1,
        2,
        3,
        5,
        8,
        13,
        21,
        34,
        55,
        89,
        144
      ],
      "active": [
        12
      ]
    }

Expected final state

F(12) = 89 + 55 = 144.

Complexity and input bounds

Family: Dynamic programming. Time complexity: O(n).

Space complexity: O(n) tabulation; rolling values can use O(1).

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 integer from 2 to 30.

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

Use the F(0)=0, F(1)=1 convention and respect fixed-width integer limits.

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
// Returns F(n); n ≤ 30 in this playground. Uses constant space. 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(n) {
    let a = 0; let b = 1;
    for (let i = 0; i < n; i++) { let t = a + b; a = b; b = t; }
    return a;
}

Source

Foundational dynamic-programming paper. It establishes the general method, not the first publication or exact recurrence of this particular teaching example.