Volume P2 · Concrete mathematics
Fibonacci numbers
Draft. This chapter is written and checked by the tools of the project, and a person has not reviewed it yet. Read it with care.
Read first
Contents
Identities of the Fibonacci numbers
Statement
The Fibonacci numbers satisfy a large number of identities. Most have a short proof by induction on the recurrence, and most have a second proof from the matrix form. This section proves the main ones and ends with a fast algorithm.
Definition P2.10.1 (the Fibonacci numbers, TAOCP eqs. 1.2.8(1) and 1.2.8(2)). The Fibonacci numbers are given by , and for .
Example P2.10.2. The first values are and are , , , , , , , , , . Also .
Example P2.10.3. The numbers count the compositions of into s and s. For the compositions are , , , and , so there are . The last part of a composition of is or , so the count satisfies , with and . Hence .
Theorem P2.10.4 (sums of the first terms). For every ,
Lean: not stated.
Proof. Both are proved by induction on . For the first sum, gives . If the claim holds for , then . For the second, gives . If the claim holds for , then .
Example P2.10.5. For : , and . The second identity says that a rectangle is the union of squares of the sides .
Theorem P2.10.6 (the matrix form, TAOCP eq. 1.2.8(5)). Let be the matrix with the rows and . For every ,
Lean: not stated.
Proof. For the right side is . Suppose the formula holds for . Then
Example P2.10.7. . The product is .
Theorem P2.10.8 (Cassini's identity, TAOCP eq. 1.2.8(4)). For every ,
Lean: Mathlib Int.fib_succ_mul_fib_pred_sub_fib_sq for the integer-indexed Fibonacci numbers (proved in Mathlib).
Proof. Take the determinant of . The determinant of is , and the determinant of a product is the product of the determinants. So , and by the last theorem.
Example P2.10.9. For : . For : . The identity gives a puzzle of area: cut an square along the lines of the Fibonacci numbers and put the pieces together as a rectangle. The areas are and , which differ by .
Theorem P2.10.10 (the addition law, TAOCP eq. 1.2.8(6)). For all integers and ,
Lean: Mathlib Nat.fib_add, in the equivalent form fib (m + n + 1) = fib m * fib n + fib (m + 1) * fib (n + 1) (proved in Mathlib).
Proof. For compare the entries of in the first row and the second column. The first row of is and the second column of is , so . Exchange the roles of and to get . This is the claim. For the claim reads .
Example P2.10.11. For and : . For and : .
Corollary P2.10.12 (doubling formulas). For every ,
Lean: Mathlib Nat.fib_two_mul and Nat.fib_two_mul_add_one (proved in Mathlib).
Proof. Put in the addition law: , and gives the first formula. Put : .
Example P2.10.13. For : and .
Theorem P2.10.14 (the shallow diagonals of Pascal's triangle). For every ,
Lean: Mathlib Nat.fib_succ_eq_sum_choose (proved in Mathlib).
Proof. Let be the right side. Then and . For , Pascal's rule gives . The first terms add to . In the second terms put : , which add to . So , and the sequences and have the same start and the same rule.
Example P2.10.15. For : . For : .
Method (fast doubling). Given an integer , it computes the pair with multiplications.
The idea is to read the bits of from the left and to keep the pair , where is the number that the bits read so far form. Reading one more bit turns into or , and the doubling formulas give the new pair.
fast_doubling(n):
a <- 0; b <- 1 # (F_0, F_1), k = 0
for each bit t of n, from the leading bit to the last:
c <- a * (2*b - a) # F_{2k}
d <- a*a + b*b # F_{2k+1}
if t = 0: (a, b) <- (c, d) # k <- 2k
else: (a, b) <- (d, c + d) # k <- 2k + 1
return (a, b)
Run for . The result is and .
Proof of correctness. The invariant is that, before each bit, with the number formed by the bits already read. At the start and . The values and are and by the corollary. For the bit the new number is and the new pair is . For the bit the new number is and the new pair is . After the last bit, . The loop runs times, and it stops. (The formula for also holds for , where it gives .)
Cost. Each pass has multiplications, so the cost is multiplications. For this is . Computing by the recurrence takes additions. The numbers have about bits, so on large the cost of a multiplication dominates the cost.
Proof
The identities all follow from two facts: the recurrence and the matrix with . The identities by induction use the first fact, and the identities for use the second. The shallow diagonal identity connects the sequence to the triangle of Chapter 6.
Examples
Example P2.10.16. Cassini's identity gives a fast check of a table. In the table of the first values, .
Example P2.10.17. The matrix has the first Fibonacci numbers as entries. For : , and its square is , so , and .
Exercises
- [10] Compute with the recurrence and check with the addition law.
- [M15] Prove that and that .
- [M15] Run the recurrence backwards to define , and prove that .
- [M15] Prove that for .
- [M25] Prove that for .
- [M20] Prove that with the matrix .
- [M22] Prove that for .
- [M20] Prove that the number of compositions of into s and s with exactly parts equal to is , and use it to give a second proof of the theorem on the shallow diagonals.
- [10] Run the algorithm of fast doubling for and list the pairs .
- [M20] Prove that .
Lean
Mathlib defines Nat.fib and has Nat.fib_add_two for the recurrence, Nat.fib_add for the addition law, Nat.fib_two_mul and Nat.fib_two_mul_add_one for the doubling formulas, Nat.fib_succ_eq_sum_choose for the shallow diagonals, and Int.fib_succ_mul_fib_pred_sub_fib_sq for Cassini's identity with integer indices. The matrix form and the sums of the first terms are not stated by name. The algorithm of fast doubling has no Lean definition in this volume.
Notes and history
Leonardo of Pisa, called Fibonacci, put the rabbit problem in his Liber abaci (1202), and the numbers carry his name since Edouard Lucas used it in the 1870s. Indian prosodists knew the sequence earlier, as the count of rhythms with short and long beats, which is the composition count of the second example. Robert Simson noted the identity of Cassini's type in 1753, and Giovanni Cassini found it in 1680. Lucas gave most of the identities of this section in the 1870s.
TAOCP gives the definition in eqs. 1.2.8(1) and 1.2.8(2), Cassini's identity in eq. 1.2.8(4), the matrix form in eq. 1.2.8(5) and the addition law in eq. 1.2.8(6). The proofs here are by induction and by the matrix. The method of fast doubling is a standard trick, and Section 4.6.3 of TAOCP, Volume 2, treats the related problem of fast powers.
Divisibility of the Fibonacci numbers
Statement
The index of a Fibonacci number controls its divisors. The main theorem of the section says that the gcd of two Fibonacci numbers is the Fibonacci number of the gcd of the indices. Two further results, the periodicity modulo and the theorem of Lamé on Euclid's algorithm, use the same tools.
Lemma P2.10.18 (consecutive numbers are coprime). For every , .
Lean: Mathlib Nat.fib_coprime_fib_succ (proved in Mathlib).
Proof. Cassini's identity gives for , so a common divisor of and divides . For , .
Lemma P2.10.19 (multiples of the index). For all integers and , divides .
Lean: Mathlib Int.fib_dvd for integer indices (proved in Mathlib).
Proof. The claim holds for , since , and for . Let and suppose . The addition law with and in the places of and gives . Both terms are divisible by .
Example P2.10.20. divides and , and divides and .
Theorem P2.10.21 (the gcd law, TAOCP eq. 1.2.8(7)). For all integers ,
Lean: Mathlib Nat.fib_gcd (proved in Mathlib).
Proof. If one index is , say , both sides are , because and . Let . The proof follows the steps of Euclid's algorithm and shows that does not change when is replaced by . Let with . If there is nothing to show, so let . By the addition law, . By the lemma, . So since differs from by a multiple of . Now is coprime to by the first lemma, and divides , so is coprime to . A factor that is coprime to does not change the gcd with , so . So with . By induction on the sum of the indices, which falls, this equals , where the last step uses the base case : .
Example P2.10.22. , and . Also .
Corollary P2.10.23 (divisibility). For and , if and only if . For every , . If is prime with , then is prime. So is composite for every composite with .
Lean: not stated.
Proof. If then by the lemma. If , then , so . For the sequence is strictly increasing from on, so with implies , and gives . For the second claim, is or and . For the third, let be composite, . Then has a divisor with : write with , and take , which is at least since . By the lemma , and . So is composite. Only has no such divisor, and is prime.
Example P2.10.24. The index is prime and is not. So the converse fails. The first primes among the Fibonacci numbers are , , , , and .
Theorem P2.10.25 (periodicity modulo ). Let . The sequence is purely periodic. Let be its least period. For there is a least index with , and if and only if . Also .
Lean: not stated.
Proof. Look at the pairs . There are possible pairs. The map on pairs modulo is a bijection, with inverse . A bijection on a finite set has no pre-period: the orbit of returns to . So the sequence is purely periodic with a period at most . In the period, comes back, so and exists. If and with , then by the gcd law. By the minimality of , , so . The other direction follows from the lemma on multiples. With it gives .
Example P2.10.26. The values for are For the sequence is and then it repeats, with the period and the zeros at the multiples of . For the period is , which is why the last digits of the Fibonacci numbers repeat every terms.
Theorem P2.10.27 (Lamé). Let be integers, and suppose that Euclid's algorithm on makes exactly divisions, the last one with remainder . Then and .
Lean: not stated.
Proof. Induction on . For , and , so and . Let . The first division gives with and , since means the first remainder is not . The algorithm on makes divisions. By the induction hypothesis and . Then .
Example P2.10.28. Euclid's algorithm on takes exactly divisions for , so the bound is sharp. For the divisions are , , , , , , , and . That is divisions, and gives .
Proof
The proofs use Cassini's identity for the coprimality, the addition law for the multiples, and the steps of Euclid's algorithm for the gcd law. The gcd law is Euclid's algorithm on the indices, run in parallel with Euclid's algorithm on the values. The periodicity uses that the pair map is a bijection, which kills the pre-period. Lamé's theorem is the same idea as the gcd law: Euclid's algorithm shrinks the pair as slowly as possible when all quotients equal , and then the pair is a pair of Fibonacci numbers.
Examples
Example P2.10.29. Is divisible by ? Yes, since . And .
Example P2.10.30. Every with is even, since divides it. The even Fibonacci numbers are . Likewise is divisible by exactly when , and is divisible by exactly when , since .
Exercises
- [10] Find with the gcd law, and check it by the values and .
- [M15] Prove that is even if and only if , and that is divisible by if and only if .
- [M20] Prove that implies for with a direct argument on the least positive residue of modulo , without the gcd law.
- [M20] Prove that is if and otherwise.
- [M20] Show that Euclid's algorithm on , with , makes divisions.
- [15] Find the period of for and , and show that it is a multiple of in each case.
- [M15] Prove that if and only if .
- [10] Find for all .
- [M25] Prove Catalan's identity for .
Lean
Mathlib has Nat.fib_coprime_fib_succ and Nat.fib_gcd, which is the gcd law with Nat.gcd, and Int.fib_dvd for the divisibility of the integer-indexed numbers. The periodicity modulo and Lamé's theorem are not stated in Mathlib in this form. The doc comment of Nat.fib_gcd calls the Fibonacci numbers a strong divisibility sequence.
Notes and history
Lucas proved the gcd law in 1876, and the theorem of Lamé is from 1844, the first use of the Fibonacci numbers in the analysis of an algorithm. Lagrange noted in 1774 that the Fibonacci numbers repeat modulo , and the period carries the name of Leonardo of Pisa in the later literature. The question whether infinitely many Fibonacci numbers are prime is open.
TAOCP states the gcd law as eq. 1.2.8(7), and it proves it in Theorem 1.2.8A with two lemmas, eqs. 1.2.8(8) and 1.2.8(9). The proof here follows the steps of Euclid's algorithm and does not use the steps of the book. The exercises of 1.2.8 on Lamé's theorem and on the period are in X1.
The golden ratio and Zeckendorf's theorem
Statement
The recurrence has two real roots, and every Fibonacci number is a combination of their powers. This gives a closed form, an easy estimate of and the rule that is a rounded power. The section ends with Zeckendorf's numeral system, in which each positive integer is a sum of Fibonacci numbers in exactly one way.
Definition P2.10.31 (the golden ratio and its conjugate, TAOCP eqs. 1.2.8(3) and 1.2.8(13)). Let They are the two roots of , and , and .
Lean: Mathlib Real.goldenRatio and Real.goldenConj, with Real.goldenRatio_sq : φ ^ 2 = φ + 1, Real.goldenRatio_mul_goldenConj and Real.goldenRatio_sub_goldenConj (proved in Mathlib).
Example P2.10.32. and . Also and . The check of the roots: .
Lemma P2.10.33 (powers of ). For every , and .
Lean: Mathlib Real.fib_succ_sub_goldenRatio_mul_fib, which says and is an equivalent form (proved in Mathlib).
Proof. For the claim is . If , then . The same steps hold for , since it is a root of the same equation.
Example P2.10.34. , and .
Theorem P2.10.35 (Binet's formula, TAOCP eq. 1.2.8(14)). For every ,
Lean: Mathlib Real.coe_fib_eq (proved in Mathlib).
Proof. For subtract the two equations of the lemma: . For both sides are .
Second proof. Both sides satisfy since and do, and both give at and at , since . This is the method of linear recurrences of Chapter 2.
Example P2.10.36. . With and the difference is , and division by gives .
Theorem P2.10.37 (the nearest integer, TAOCP eq. 1.2.8(15)). For every , is the integer nearest to . More exactly, , which has absolute value less than , and it tends to with alternating sign.
Lean: not stated; Binet's formula is in Mathlib.
Proof. Binet's formula gives . Since , for , and . So the difference is less than in absolute value, and the integer nearest to is .
Example P2.10.38. The table lists and the error :
Theorem P2.10.39 (the ratios). For every , . So , and the quotients are alternately above and below .
Lean: Mathlib Real.fib_succ_sub_goldenRatio_mul_fib (proved in Mathlib).
Proof. The proof is by induction. For : . Assume . Then . Since , , so this is . Dividing by gives . This tends to and has the sign .
Example P2.10.40. The quotients are . They alternate around , and the error after is .
Example P2.10.41 (the number of digits). The number of decimal digits of is , and with a tiny . With and : for the value is , so has digits, and . For the value is , so has digits, and the leading digits are given by . The first digits of are .
Definition P2.10.42 (the Lucas numbers). The Lucas numbers are given by , and .
Lean: not stated.
Example P2.10.43. are .
Theorem P2.10.44 (Lucas numbers and Fibonacci numbers). For every , (for ), and .
Lean: not stated.
Proof. The sequence has the values and at and and follows the Fibonacci rule, so it is . For the sum also follows the rule, and it is at and at , which are and . By Binet, . Finally .
Example P2.10.45. . And .
Theorem P2.10.46 (Zeckendorf's representation). Every integer is a sum of Fibonacci numbers with , , , , and this representation is unique.
Lean: Mathlib Nat.zeckendorf, Nat.zeckendorf_sum_fib and Nat.zeckendorfEquiv (proved in Mathlib).
Proof. The sum of a representation with at distance is less than . Prove this first by induction on . For the sum is if . For the sum of the last terms is less than , so the whole sum is less than .
Existence is by strong induction on . Let be the largest Fibonacci number with and . Then , so . If the representation is . Otherwise it has, by induction, a representation whose largest index has , so . Adding gives a representation of .
Uniqueness follows from the bound. A representation of with the largest index has , so is the index of the largest Fibonacci number that does not exceed . This index is determined by . Remove it: the remaining terms represent with the same rule, and by induction this representation is unique.
Method (the greedy method). To find the representation of , repeat: find the largest with , record , replace by , and stop when . The proof of the theorem shows that the recorded indices differ by at least , and the loop stops since falls.
Run for . The Fibonacci numbers up to are . The largest one at most is , and . The largest one at most is , and . Then . So . The indices differ by and .
Example P2.10.48. Written as strings of digits at the places , the numbers are No string has two adjacent s. Also , and .
Remark P2.10.49 (the generating function). The series equals , and partial fractions turn this into Binet's formula. TAOCP gives this as eqs. 1.2.8(10) to 1.2.8(12), and it gives the square of the series as eqs. 1.2.8(16) and 1.2.8(17). P3 proves them, with the rules for formal power series. The sum of Exercise 9 is the value of the series at .
Proof
Binet's formula is the lemma on powers of , subtracted. The estimate of the error uses only that . The Zeckendorf theorem uses the Fibonacci numbers as the numerals of a system in which each place is worth the sum of the two places below. The bound on the sum of a sparse representation plays the role that the bound plays for binary numerals.
Examples
Example P2.10.50. Compute by Binet's formula. gives . The nearest integer is .
Example P2.10.51. The number of binary strings of length with no two adjacent s is . For they are , , , and , which is . The Zeckendorf strings of the integers are exactly these.
Exercises
- [10] Find from to four decimal places and round it.
- [M15] Prove that for .
- [M20] Prove that and by Binet's formula, and compute .
- [M20] Prove that and .
- [15] Find the Zeckendorf representations of , and .
- [M20] Prove that the quotients tend to and that .
- [M25] Find the number of digits and the leading digit of .
- [M20] Prove that the number of binary strings of length with no two adjacent s is , and that the number of integers with is the same.
- [M20] Prove that .
Lean
Mathlib has Real.goldenRatio and Real.goldenConj with the notation and , the facts Real.goldenRatio_sq, Real.goldenRatio_mul_goldenConj, Real.goldenRatio_add_goldenConj and Real.goldenRatio_sub_goldenConj, and Binet's formula Real.coe_fib_eq : (Nat.fib n : ℝ) = (φ ^ n - ψ ^ n) / √5. The rule is Real.fib_succ_sub_goldenRatio_mul_fib. The Zeckendorf representation is Nat.zeckendorf : ℕ → List ℕ, with Nat.zeckendorf_sum_fib and the bijection Nat.zeckendorfEquiv. The Lucas numbers are not in Mathlib, and the nearest-integer rule is not stated by name.
Notes and history
Abraham de Moivre found Binet's formula in the 1720s, Euler gave it in 1765, and Jacques Binet published it in 1843. The golden ratio has been studied since the Greeks, and the numbers carry the name of Lucas, who studied them in 1878. Edouard Zeckendorf proved the representation theorem in 1939 and published it in 1972, and earlier Lekkerkerker had it in 1952.
TAOCP defines in eq. 1.2.8(3), in eq. 1.2.8(13), Binet's formula in eq. 1.2.8(14) and the nearest-integer rule in eq. 1.2.8(15). Zeckendorf's theorem is an exercise there, and the numeral system comes back in Section 6.2.1 of Volume 3 as the basis of Fibonacci search. The table maps the equations of Section 1.2.8 to this chapter.
| TAOCP 1.2.8 | Result of P2 |
|---|---|
| eqs. (1), (2) | Definition (the Fibonacci numbers), Example (the first values), Section 10.1 |
| eq. (3) | Definition (the golden ratio and its conjugate) |
| eq. (4) | Theorem (Cassini's identity), Section 10.1 |
| eq. (5) | Theorem (the matrix form), Section 10.1 |
| eq. (6) | Theorem (the addition law), Section 10.1 |
| eqs. (7) to (9) | Theorem (the gcd law) and its proof, Section 10.2 |
| eqs. (10) to (12) | Remark (the generating function), proved in P3 |
| eq. (13) | Definition (the golden ratio and its conjugate) |
| eq. (14) | Theorem (Binet's formula) |
| eq. (15) | Theorem (the nearest integer) |
| eqs. (16), (17) | Remark (the generating function), proved in P3 |
The Fibonacci numbers are the simplest sequence defined by a recurrence of order two. They appear in the worst case of Euclid's algorithm, in the analysis of search by Fibonacci numbers, in the number of ways to tile a strip and in the shape of many natural patterns. They are also the standard example of a sequence that has a recurrence, a matrix form, a closed form, a divisibility law and a numeral system.
Section 10.1 defines and proves the basic identities: the sums of the first terms, Cassini's identity, the matrix form, the addition law, the formulas for and and the sum along a shallow diagonal of Pascal's triangle. It gives an algorithm that computes with multiplications, with a run by hand. Section 10.2 proves that , derives the divisibility of and the periodicity of modulo , and proves Lamé's theorem on the worst case of Euclid's algorithm. Section 10.3 solves the recurrence by the golden ratio , proves Binet's formula and the rule that is the integer nearest to , introduces the Lucas numbers and proves Zeckendorf's theorem on the representation of integers as sums of Fibonacci numbers.
TAOCP treats the Fibonacci numbers in Section 1.2.8, in the equations 1.2.8(1) to 1.2.8(17). The table at the end of Section 10.3 maps these equations to the items of this chapter. The generating function and the square of this function (eqs. 1.2.8(10) to 1.2.8(12), 1.2.8(16) and 1.2.8(17)) belong to P3, and this chapter gives pointers only. The solution of linear recurrences in Chapter 2 gives a second proof of Binet's formula.
The chapter needs induction (P1), matrices of order two, the gcd and the Euclidean algorithm of Section 5.1, congruences of Section 5.3, and the binomial coefficients of Chapter 6. The companion section is C1 Section 1.2.8, and X1 has the solutions of the exercises of TAOCP 1.2.8. Continuants in Chapter 12 generalize the matrix form of Section 10.1.
The source of this page. Something is wrong? Report it.