The Fibonacci sequence (OEIS A000045) is defined as follows:
F0=0,F1=1,Fn=Fn−1+Fn−2
Its first few terms are:
0,1,1,2,3,5,8,13,21,34,55,89,…
The Lucas Sequence
The Lucas sequence (OEIS A000032) is defined as follows:
L0=2,L1=1,Ln=Ln−1+Ln−2
Its first few terms are:
2,1,3,4,7,11,18,29,47,76,123,199,…
The Lucas sequence is often used as a tool when studying the Fibonacci sequence.
Closed Form of the Fibonacci Sequence
The n-th Fibonacci number can be computed from the recurrence in \Theta(n) time. However, faster methods are available. Θ(n)
Closed-Form Solution
The closed-form solution is a formula-based solution. The general formula for the Fibonacci sequence, known as Binet's formula, is
Fn=5(21+5)n−(21−5)n
This formula is easy to prove by induction. It can also be derived using generating functions or by solving an equation.
The second term in the numerator always has absolute value less than 1 and decreases exponentially. Therefore, the formula can be written as
Fn=5(21+5)n
Here the square brackets mean the nearest integer.
Both formulas require extremely high precision in computation, so they are rarely used in practice. Nevertheless, together with quadratic residues and inverses modulo an integer, they are still useful in OI.
Closed Form of the Lucas Sequence
The Lucas sequence has the closed form
Ln=(21+5)n+(21−5)n
which is very similar to the Fibonacci formula. In fact,
2Ln+Fn5=(21+5)n
That is, Ln and Fn are precisely the numerator coefficients obtained after expanding (21+5)n binomially and collecting like terms. Thus, all solutions of the Pell equation
x2−5y2=−4
are exactly
2xn+yn5=2Ln+Fn5
namely the Lucas and Fibonacci sequences. Consequently,
Ln2−5Fn2=−4
Matrix Form
The Fibonacci recurrence can be expressed through matrix multiplication:
[Fn−1Fn]=[Fn−2Fn−1][0111]
Let P=[0111]. Then
[FnFn+1]=[F0F1]Pn
Thus, matrix multiplication computes Fibonacci numbers in Θ(logn) time. The formula from the preceding section can also be obtained by diagonalizing the matrix.
Fast Doubling
The method above yields
F2kF2k+1=Fk(2Fk+1−Fk)=Fk+12+Fk2
Therefore, two adjacent Fibonacci numbers can be computed quickly, with a smaller constant than matrix exponentiation. The following code returns the pair (Fn,Fn+1).
pair<int, int> fib(int n){
if (n == 0) return {0, 1};
auto p = fib(n >> 1);
int c = p.first * (2 * p.second - p.first);
int d = p.first * p.first + p.second * p.second;
if (n & 1)
return {d, c + d};
elsereturn {c, d};
}
Properties
The Fibonacci sequence has many interesting properties. Some simple ones are listed below:
Cassini's identity: Fn−1Fn+1−Fn2=(−1)n.
Addition identity: Fn+k=FkFn+1+Fk−1Fn.
Setting k=n in the preceding identity gives F2n=Fn(Fn+1+Fn−1).
By induction from the preceding identity, ∀k∈N,Fn∣Fnk.
The preceding property has a converse: ∀Fa∣Fb,a∣b.
GCD identity: (Fm,Fn)=F(m,n).
Taking two adjacent Fibonacci numbers as input makes the Euclidean algorithm attain its worst-case complexity (see Wikipedia - Lamé for details).
Relationship between the Fibonacci and Lucas Sequences
Identities for the Lucas and Fibonacci sequences are strikingly similar to trigonometric identities. For example,
2Ln+Fn5=(21+5)n
resembles
cosnx+isinnx=(cosx+isinx)n
and
Ln2−5Fn2=−4
resembles
cos2x+sin2x=1
Thus, the Lucas sequence is similar to the cosine function, while the Fibonacci sequence is similar to the sine function. From
(21+5)m(21+5)n=(21+5)m+n
we obtain the identities for the sum of two indices:
2Lm+n=5FmFn+LmLn2Fm+n=FmLn+LmFn
and hence the doubling identities:
L2n=Ln2−2(−1)nF2n=FnLn
These also provide a way to double indices quickly. In the same way, by imitating trigonometric identities such as parity, sum-to-product, product-to-sum, half-angle formulas, and universal substitutions, we can derive more corresponding identities for the Lucas and Fibonacci sequences.
Fibonacci Coding
The Fibonacci sequence can encode positive integers. By Zeckendorf's theorem, every natural number n has a unique representation as a sum of Fibonacci numbers:
N=Fk1+Fk2+…+Fkr
where k1≥k2+2,k2≥k3+2,…,kr≥2 (that is, two adjacent Fibonacci numbers cannot both be used).
We can therefore encode a positive integer as d0d1d2…ds1, where di=1 means that Fi+2 is used. A final 1 is forced at the end of the code, producing two adjacent 1s, to mark the end of the code. For example:
Enumerate Fibonacci numbers from largest to smallest until reaching Fi≤n.
Subtract Fi from n and put a 1 at position i−2 in the code, numbered from left to right starting at 0.
If n is positive, return to step 1.
Append a 1 at the end of the code to mark its end. Fi
Decoding is analogous: first remove the final 1; for every position i containing a 1, numbered from left to right starting at 0, add Fi+2 to the answer. The final answer is the original number.
Periodicity Modulo m
By the pigeonhole principle, the Fibonacci sequence modulo m is periodic. Since each Fibonacci number depends on the preceding two values, the state is described by a pair of adjacent Fibonacci numbers. Consider the first m2+1 pairs modulo m:
(F0,F1),(F1,F2),…,(Fm2,Fm2+1)
There are m residue classes modulo m, so at most m2 distinct pairs are possible. Hence two of the first m2+1 pairs must be equal. The same Fibonacci sequence can then be generated from either pair, so the sequence is periodic and its smallest positive period is at most m2.
Pisano Period
The smallest positive period of the Fibonacci sequence modulo m is called the Pisano period (Pisano period, OEIS A001175). We denote it by π(m). m
This observation can be used to compute the n-th Fibonacci number modulo m. If n is very large, we need only compute a period modulo m, not necessarily the smallest positive period.
The following facts hold:
For coprime moduli m1,m2, π(m1m2)=lcm(π(m1),π(m2)).
For a prime p and positive integer e, π(pe)∣pe−1π(p).
For m=2e(e∈N+), π(m)=3⋅2e−1.
For m=5e(e∈N+), π(m)=4⋅5e.
For a prime p≡±1(mod10), π(p)∣(p−1); for a prime p≡±3(mod10), π(p)∣2(p+1).
Together these facts show that the Pisano period modulo m is at most 6m, with equality if and only if m=2×5e(e∈N+).
Using these facts and prime factorization, we obtain the following fast method for computing a Pisano period:
Reference Code
// Get a period of Fibonacci sequence mod m.// Not necessarily be the exact Pisano period.uint32_tcalc_cycle_from_mod(uint32_t m){
uint32_t res = 1;
for (auto pe : factorize(m)) {
auto p = pe.first;
auto e = pe.second;
uint64_t cur = pow(p, e - 1);
if (p == 2) {
cur *= 3;
} elseif (p == 5) {
cur *= 20;
} elseif (p % 5 == 1 || p % 5 == 4) {
cur *= p - 1;
} else {
cur *= 2 * (p + 1);
}
res = lcm(res, cur);
}
return res;
}
The period obtained this way may only be a multiple of the Pisano period. To obtain the exact period, we can further examine its divisors, or use the BSGS algorithm in O(m) time.
Proof
We briefly prove the facts about the Pisano period above. The method below also generalizes to general second-order homogeneous linear recurrences with constant coefficients. Although the constants differ, their periods modulo m are all O(m).
First, by the Chinese remainder theorem, it suffices to consider prime-power moduli. Let m1,m2 be coprime. The periods modulo m1 and m2 are respectively multiples of π(m1) and π(m2), so the smallest positive period modulo m1m2 is \operatorname{lcm}(\pi(m_1),\pi(m_2)). This proves fact 1. π(m1)π(m2)
The Pisano period modulo m is also the smallest positive integer k such that
Thus, by the properties of orders, π(pe+1)∣kp=pπ(pe). Induction on e gives π(pe)∣pe−1π(p).
For a prime modulus p, we give two proofs.
Using the Closed Form
Use
Fn=51(21+5)n−51(21−5)n.
Expanding by the binomial theorem and eliminating radicals gives
Fn=2n−11i=0∑⌊(n−1)/2⌋(2i+1n)5i.
For p=2, this expression cannot be reduced modulo p directly, but π(2)=3 can be verified. For p=5, Fn≡n⋅3n−1(modp), and π(5)=20 follows directly. For the remaining odd primes:
Here we use Lucas's theorem, Fermat's little theorem, and quadratic reciprocity: (kp)≡0(modp) for 0<k<p, (kp+1)≡0(modp) for 1<k<p, 2p−1≡5p−1≡1(modp), and 5(p−1)/2≡1(modp). Therefore (Fp,Fp+1)≡(F1,F2)(modp), so (p−1) is a period and π(p)∣(p−1).
Lucas's theorem gives the required vanishing binomial coefficients and \dbinom{2p}{p}\equiv\dbinom{2p+1}{p}\equiv2\pmod{p}. Fermat's little theorem gives 2p−1≡5p−1≡1(modp), while quadratic reciprocity gives 5(p−1)/2≡−1(modp). Hence (F2p,F2p+1)≡(F−2,F−1)(modp), so 2(p+1) is a period and π(p)∣2(p+1). 0<k<pp<k<2p(kp)≡0(modp)(p2p)≡2(modp)1<k<pp+1<k<2p(kp)≡0(modp)(p2p+1)≡2(modp)p≡2,3(mod5)p55pp
This completes the proof. The limitation of this method is its heavy reliance on the closed form, which makes direct generalization difficult.
Using a Field Extension
Alternatively, compute the order of A=(1110) directly. Its characteristic polynomial is f(x)=x2−x−1, with discriminant Δ=5. For p=5, Δ≡0(mod5); A has the repeated eigenvalue λ=3 and is not diagonalizable, so this case must be handled separately. For p≡1,4(mod5), quadratic reciprocity says that Δ=5 is a quadratic residue modulo p. Thus A has two distinct eigenvalues in Fp, and its order divides ∣Fp×∣=p−1. For p≡2,3(mod5), Δ=5 is a quadratic nonresidue modulo p. The two distinct eigenvalues lie in the field extensionFp[5]. Frobenius swaps them, so λ2=λ1p and λ1p+1=λ2p+1=λ1λ2=−1. Hence both eigenvalues have order dividing 2(p+1), so the order of A divides 2(p+1). λ1=λ2lcm(ord(λ1),ord(λ2))AFpλ1=λ2x↦xpλ12(p+1)=λ22(p+1)=1Alcm(ord(λ1),ord(λ2))
Thus:
π(2e)=23⋅2e,41π(5e)=5e.
If p≡±1(mod10), then π(pe)∣(p−1)pe−1, so π(pe)≤pe.
If p≡±3(mod10), then 41π(pe)∣2p+1pe−1, so 41π(pe)≤pe.
For m=∏ipiei, fact 1 gives
π(m)=lcm{π(piei):pi∈P}≤lcm{π(piei):pi=2 or pi≡±1(mod10)}⋅4⋅lcm{π(piei)/4:pi=5 or pi≡±3(mod10)}≤∏{π(piei):pi=2 or pi≡±1(mod10)}⋅4⋅∏{π(piei)/4:pi=5 or pi≡±3(mod10)}≤23⋅∏{piei:pi=2 or pi≡±1(mod10)}⋅4⋅∏{piei:pi=5 or pi≡±3(mod10)}=6m.
Therefore the Pisano period modulo m is always at most 6m, with equality exactly when m=2⋅5e.
This page is mainly translated from the blog post Числа Фибоначчи and its English translation Fibonacci Numbers. The Russian version is licensed under Public Domain + Leave a Link; the English version under CC-BY-SA 4.0. The content has been modified.
Footnotes
Strictly speaking, it is the order of matrix A in the general linear group GL2(Zm). ↩