Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 09 · DSA · Four patterns the ladders lean on · about 17 min to read

Play it as a journeyTutor comes with a plan

Fibonacci in forty steps: matrix exponentiation

Thinking in Bits
Thinking in Bits · Open board

The board shows a number as a row of switches. This lesson reads the switches of an exponent, and uses them to decide which squares to multiply.

A coding test gives you this task. The Fibonacci numbers start 0, 1, 1, 2, 3, 5, 8. Each number is the sum of the two before it. Print the number at position 1,000,000,000,000,000,000 (that is 10^18), and print only the remainder after dividing by 1,000,000,007, because the true number has far too many digits to print.

The loop you already know adds two numbers, one position at a time. It needs 10^18 additions. This lesson gives a method that needs about 84 matrix multiplications, and the same method solves any recurrence where each number is a fixed sum of the few numbers before it.

By the end you will be able to turn such a recurrence into a small grid of numbers, raise that grid to a power with the doubling idea from Clinic #31: Pow(x, n), and read off the answer for position 10^18.

The history (optional)

Leonardo of Pisa, known as Fibonacci, put a rabbit problem in his book Liber Abaci, which he completed in 1202. One pair of rabbits produces a new pair every month, and the monthly counts give the sequence 1, 1, 2, 3, 5, 8 and on. The French mathematician Edouard Lucas gave the sequence the name Fibonacci numbers in 1876.

Before you start. Clinic #31: Pow(x, n) gave you fast exponentiation: raise a number to the power n by squaring it again and again and multiplying in the squares that the binary digits of n ask for, about log n steps instead of n. The clock that wraps around gave you the modulus: keep only the remainder after every multiplication and the final remainder stays correct, so numbers never grow. Climbing a tree in powers of two used the same jump sizes of 1, 2, 4, 8 to climb a tree. Read the code in this track in twenty minutes solved LeetCode 509, Fibonacci Number, with a plain loop.

Lesson 82 of 96 in DSA

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • One step of the loop, written as a multiplication
  • Multiplying two tables: matrix multiplication
  • Raising a table to a power: the doubling you already know
  • What 84 buys: forty squarings and a timing on one machine
  • Why the pattern Q^n = [[F(n+1), F(n)], [F(n), F(n-1)]] holds
  • Why squaring is allowed
  • The code, run on 2026-11-28 with Python 3.12
  • Why the modulus matters here
  • Free LeetCode problem 1: N-th Tribonacci Number (LeetCode 1137)
  • Free LeetCode problem 2: Climbing Stairs (LeetCode 70)
  • Limits and trade-offs

Terms it explains: matrix, transition matrix, matrix multiplication, identity matrix, Common mistakes, 7) == fib_mod(10.

Review DSA