Math Tools

The Fibonacci sequence: from rabbits to the golden ratio, and why slow code loves it

Add the last two numbers to get the next: 0, 1, 1, 2, 3, 5, 8, 13. That one rule hides a growth pattern, a famous ratio of about 1.618, a stair-climbing puzzle and the classic example of slow recursive code. This guide walks through all four with pictures and exact numbers.

By the Toolcloak team8 October 20265 min read
In this article
  1. The rule and the first terms
  2. The rabbit puzzle
  3. It grows faster than it looks
  4. The golden ratio appears
  5. Climbing stairs and counting ways
  6. Why programmers use it as a warning
  7. Quick practice

In 1202, Leonardo of Pisa, known as Fibonacci, posed a puzzle about rabbits: start with one pair, let every pair that is at least a month old produce a new pair each month, and count the pairs. The answer was a sequence that mathematicians, biologists, traders and programmers still talk about. The rule is so simple that a child can follow it, and the consequences are surprisingly deep.

The rule and the first terms

Start with 0 and 1. Every next number is the sum of the previous two: 0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5, and so on. Some books start at 1, 1 instead of 0, 1. The numbers after the start are identical.

n0123456789101112
F(n)01123581321345589144

F(n) is the n-th term. F(10) = 55 and F(12) = 144.

How each new term is made
  1. 1Take the last twoFor example 21 and 34.
  2. 2Add them21 + 34 = 55.
  3. 3Slide alongNow the last two are 34 and 55. Repeat.
Choose how many terms you want and list them, or jump straight to the n-th number.Open Fibonacci Sequence Generator

The rabbit puzzle

Fibonacci’s puzzle assumes immortal rabbits that mate perfectly, so it is a model rather than zoology. Month 1: one pair, too young to breed. Month 2: still one pair, now mature. Month 3: the pair has a new pair, so two. Month 4: the old pair breeds again, the newer pair is maturing, so three. The pairs per month go 1, 1, 2, 3, 5, 8: exactly the sequence.

Pairs of rabbits at the start of each month
Month 1
1
Month 2
1
Month 3
2
Month 4
3
Month 5
5
Month 6
8
Month 7
13
Month 8
21

Each month the population equals last month’s pairs (who survive) plus the pairs born to everyone who was already mature two months ago.

It grows faster than it looks

The early terms are gentle, which hides how quickly the sequence takes off. Roughly every step multiplies the value by 1.618, so it is exponential growth, and the terms soon become huge.

Some larger terms
6,765F(20)Still small enough to read at a glance
832,040F(30)Over eight lakh
12,586,269,025F(50)More than 12 billion

F(90) is already 2,880,067,194,370,816,120, about 2.9 × 10¹⁸.

The first 16 terms (values)
F(n)
01252503755006250123456789101112131415F(15) = 610

The curve is flat for the first few terms and then bends steeply upward, the signature of exponential growth.

If that shape looks familiar, it is the same curve you meet in compound growth and the rule of 72. Fibonacci growth is like earning about 61.8% per step, with the nice twist that the “interest” comes from the sequence’s own past.

The golden ratio appears

Divide each term by the one before it and watch what happens. The first ratios jump around (1, 2, 1.5, 1.67), then quickly calm down and approach 1.6180339…, known as the golden ratio and written with the Greek letter phi (φ).

Ratio of each term to the one before it: F(n+1) ÷ F(n)
F(n+1) ÷ F(n)Golden ratio φ
00.511.521234567891011121.6181

The blue line oscillates around the amber line, getting closer each time. By n = 10 the ratio is correct to three decimals.

Because of this, there is even a direct formula, Binet’s formula: F(n) = (φⁿ − (1 − φ)ⁿ) ÷ √5. Since (1 − φ) is about −0.618, its powers shrink to nothing, so F(n) is simply φⁿ ÷ √5 rounded to the nearest whole number. For n = 10: 1.618¹⁰ ÷ 2.236 ≈ 55.0.

Climbing stairs and counting ways

Here is a puzzle that has nothing to do with rabbits. You can climb a staircase by taking either 1 or 2 steps at a time. How many different routes are there for n steps? The last move was either a single step (leaving n − 1 steps before it) or a double step (leaving n − 2). So ways(n) = ways(n − 1) + ways(n − 2): the Fibonacci rule again.

Number of ways to climb n stairs with steps of 1 or 2
5 stairs
8
10 stairs
89
15 stairs
987
20 stairs
10,946

The count for n stairs is F(n + 1). Twenty stairs already have 10,946 different routes.

Another identity: the sum of the first n terms (from F(1)) is F(n + 2) − 1. Check with the first ten: 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34 + 55 = 143, and F(12) − 1 = 144 − 1 = 143.

Why programmers use it as a warning

Fibonacci is the standard example of recursion, a function that calls itself: fib(n) = fib(n − 1) + fib(n − 2). It is elegant, and it is slow, because the same values are recomputed again and again. fib(5) calls fib(4) and fib(3), and fib(4) calls fib(3) again, and so on.

Two ways to compute F(30)
Naive recursion2,692,537 calls
  • Recomputes the same terms repeatedly
  • Time roughly doubles with every extra step
  • F(40) needs about 331 million calls
A simple loop (or memoisation)30 steps
  • Keeps only the last two values
  • Time grows in a straight line with n
  • Easily handles F(10,000) with big integers

The number of calls for naive recursion is 2 × F(n + 1) − 1, so the slowness is itself a Fibonacci sequence.

The fast way, step by step (for F(6))
  1. 1Starta = 0, b = 1.
  2. 2Repeat 6 timesReplace (a, b) with (b, a + b): (1,1), (1,2), (2,3), (3,5), (5,8), (8,13).
  3. 3Read the answerAfter 6 rounds, a = 8, which is F(6).

Quick practice

  1. What comes after 55, 89? 144 (55 + 89).
  2. How many ways to climb 6 stairs with steps of 1 or 2? F(7) = 13.
  3. Estimate F(12) with φⁿ ÷ √5: 1.618¹² ≈ 321.99, divided by 2.236 gives 144.0. Rounded: 144.
  4. Is 1,000,000 a Fibonacci term? No. The terms near it are 832,040 (F(30)) and 1,346,269 (F(31)).

A rule you can say in one breath turns out to describe breeding rabbits, climbing stairs and a ratio that appears all over geometry, while also teaching every new programmer why smart algorithms matter. Try the generator above to see 50 terms in a flash, and notice how fast the numbers grow after the first few. For another kind of number pattern, read about prime numbers.

Questions people ask

What is the Fibonacci sequence?

A sequence in which each number is the sum of the two before it, starting 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

What is the golden ratio?

About 1.6180339887, the value that the ratio of consecutive Fibonacci numbers approaches. It equals (1 + √5) ÷ 2.

What is the 10th Fibonacci number?

55, if you count F(0) = 0, F(1) = 1. Counting from F(1) = 1, F(2) = 1, the tenth term is also 55.

Is there a formula for the n-th Fibonacci number?

Yes, Binet’s formula: F(n) = (φⁿ − (1 − φ)ⁿ) ÷ √5. In practice F(n) is φⁿ ÷ √5 rounded to the nearest integer.

Why is recursive Fibonacci slow?

It recomputes the same values many times, so the number of calls grows exponentially. F(30) takes about 2.7 million calls, while a loop needs 30 steps.

Do all flowers follow Fibonacci numbers?

No. Many sunflowers, pinecones and pineapples show Fibonacci counts in their spirals, but it is a common pattern, not a rule.