Sequences · integer recurrences
Fibonacci Numbers: Recurrence and Computation
The Fibonacci numbers begin 0, 1, 1, 2, 3, 5, 8, 13 and continue by adding the two previous terms: F₀ = 0, F₁ = 1 and F_{n+1} = F_n + F_{n−1}. As a numerical example they are unusually useful, because the exact answer is known by several independent routes, so each route can check the others. They also give a small test case for loops, recursion, overflow and floating-point rounding.
This page lists the values, compares three ways of computing them, and shows where each method stops being trustworthy.
The first twenty-one values
| n | F(n) | n | F(n) | n | F(n) |
|---|---|---|---|---|---|
| 0 | 0 | 7 | 13 | 14 | 377 |
| 1 | 1 | 8 | 21 | 15 | 610 |
| 2 | 1 | 9 | 34 | 16 | 987 |
| 3 | 2 | 10 | 55 | 17 | 1597 |
| 4 | 3 | 11 | 89 | 18 | 2584 |
| 5 | 5 | 12 | 144 | 19 | 4181 |
| 6 | 8 | 13 | 233 | 20 | 6765 |
Later landmarks are F₂₅ = 75025, F₃₀ = 832040, F₄₀ = 102334155 and F₅₀ = 12586269025. Each of them can be reached by the recurrence in a loop of that many additions.
Method one: the loop
Keep two variables, a and b, start with 0 and 1, and replace the pair by (b, a + b) n times. The loop costs n additions and needs no extra memory. A recursive function that calls itself for F_{n−1} and F_{n−2} looks identical to the definition but recomputes the same values over and over; its running time grows as fast as the numbers themselves, so even n = 40 is slow. That contrast is a classic lesson in why recursion needs memory of earlier results.
Method two: Binet’s formula
The closed form is F_n = (φⁿ − ψⁿ)/√5, where φ = (1 + √5)/2 = 1.618034 and ψ = (1 − √5)/2 = −0.618034. Because |ψ|ⁿ shrinks fast, F_n is simply φⁿ/√5 rounded to the nearest integer. Check with n = 10: φ¹⁰ = 122.9919, divided by √5 = 2.236068 it is 55.0036, which rounds to 55. For n = 20 the quotient is 6765.00003.
The formula is exact in mathematics and fragile in floating point. Computing φⁿ in double precision carries a relative error near 10⁻¹⁶, which becomes an absolute error above 0.5 once F_n is large enough. In a quick double-precision test the rounded formula first disagrees with the true value at n = 71. For larger n the integer answers must come from integer arithmetic.
Method three: fast doubling
Two identities allow jumping from index k to 2k and 2k + 1:
F(2k) = F(k) · (2·F(k+1) − F(k))
F(2k+1) = F(k)² + F(k+1)²
Test with k = 10, F₁₀ = 55 and F₁₁ = 89. Then F₂₀ = 55 × (178 − 55) = 55 × 123 = 6765 and F₂₁ = 3025 + 7921 = 10946, matching the table and the recurrence. Recursing on n/2 reaches any index in about log₂ n steps, each with a couple of multiplications. With integers of unlimited size this computes F₁₀₀₀ in a blink; with fixed-size integers the limit is overflow.
Where precision runs out
Doubles represent integers exactly only up to 2⁵³ = 9007199254740992. F₇₈ = 8944394323791464 is just under that limit and is exact. F₇₉ = 14472334024676221 is above it, and a double stores it as 14472334024676220, off by one. A signed 64-bit integer lasts longer: F₉₂ = 7540113804746346429 still fits, and F₉₃ = 12200160415121876738 does not.
So the choice of arithmetic sets the largest usable n: about 78 for doubles used as integers, 92 for signed 64-bit integers.
Identities and neighbor ratios
Cassini’s identity says F_{n−1} F_{n+1} − F_n² = (−1)ⁿ. For n = 10 it reads 34 × 89 − 55² = 3026 − 3025 = 1, and for n = 9 it reads 21 × 55 − 34² = 1155 − 1156 = −1. A table that satisfies Cassini at every index contains no isolated error.
A second check concerns divisibility: the greatest common divisor of F_m and F_n is F_gcd(m, n). With m = 12 and n = 18 the divisor is F₆ = 8, and indeed gcd(144, 2584) = 8.
Dividing each Fibonacci number by its predecessor produces 1, 2, 1.5, 1.6667, 1.6, 1.625 and onward, a sequence that closes in on φ. This is the simplest example of the running quotient, and the same numbers appear as convergents in Lentz’s method. Cumulative sums and products of such sequences are covered by the cumulative product page.
For writing these functions as small reusable scripts, see scripting numerical functions. Neighboring methods are collected under interpolation and calculus.