Fibonacci Sequence
The Fibonacci sequence (OEIS A000045) is defined as follows:
The first few terms of the sequence are:
Lucas Sequence
The Lucas sequence (OEIS A000032) is defined as follows:
The first few terms of the sequence are:
Studying the Fibonacci sequence often requires using the Lucas sequence as a tool.
Closed-form Expression for Fibonacci Numbers
The \(n\)-th Fibonacci number can be computed in \(\Theta (n)\) time using the recurrence relation. However, there are faster methods.
Closed-form Solution
The closed-form solution is given by Binet's Formula:
This formula can be easily proven by induction, derived using generating functions, or obtained by solving an equation.
You may notice that the second term in the numerator is always less than \(1\) and decreases exponentially. Therefore, the formula can be written as:
Here, the brackets denote rounding to the nearest integer.
Both formulas require extremely high precision in computation, so they are rarely used in practice. However, do not overlook them! Combined with the concepts of quadratic residues and modular inverses, this formula can still be useful in OI.
Closed-form Expression for Lucas Numbers
We have the closed-form formula for Lucas numbers:
This is very similar to the Fibonacci sequence. In fact:
That is, \(L_n\) and \(F_n\) exactly correspond to the coefficients of the binomial expansion of \(\left(\frac{1 + \sqrt{5}}{2}\right)^n\) after combining like terms. In other words, all solutions to the Pell equation
are exactly
which are precisely the Lucas and Fibonacci sequences. Therefore:
Matrix Form
The recurrence of the Fibonacci sequence can be expressed in matrix multiplication form:
Let \(P = \begin{bmatrix}0 & 1 \cr 1 & 1 \cr\end{bmatrix}\). Then we have:
Thus, we can compute the Fibonacci sequence in \(\Theta(\log n)\) time using matrix exponentiation. Additionally, the formulas discussed in the previous section can also be derived using matrix diagonalization techniques.
Fast Doubling Method
Using the above method, we can derive the following identities:
Thus, we can quickly compute two adjacent Fibonacci numbers using this method (with a smaller constant than matrix exponentiation). The code below returns a pair \((F_n,F_{n+1})\).
1 2 3 4 5 6 7 8 9 10 | |
Properties
The Fibonacci sequence has many interesting properties. Here are some simple ones:
- Cassini's identity: \(F_{n-1} F_{n+1} - F_n^2 = (-1)^n\).
- Addition property: \(F_{n+k} = F_k F_{n+1} + F_{k-1} F_n\).
- Setting \(k = n\) in the above property, we get \(F_{2n} = F_n (F_{n+1} + F_{n-1})\).
- From the above property, it can be proven by induction that \(\forall k\in \mathbb{N},F_n|F_{nk}\).
- The above property is reversible: \(\forall F_a|F_b,a|b\).
- GCD property: \((F_m, F_n) = F_{(m, n)}\).
- Using two consecutive Fibonacci numbers as input to the Euclidean algorithm achieves the worst-case complexity (see Wikipedia - Gabriel Lamé).
Relationship Between Fibonacci and Lucas Sequences
It is not difficult to see that the equations relating Lucas and Fibonacci numbers bear a strong resemblance to trigonometric identities. For example:
resembles
and
resembles
Thus, the Lucas sequence resembles the cosine function, while the Fibonacci sequence resembles the sine function. For instance, from
we can derive identities for the sum of indices:
From this, we can deduce identities for doubling the index:
This is also a method for fast doubling of indices. Similarly, by imitating trigonometric formulas such as parity, sum-to-product, product-to-sum, half-angle, and universal substitution, we can derive more corresponding equations for Lucas and Fibonacci sequences.
Fibonacci Coding
We can use the Fibonacci sequence to encode positive integers. According to Zeckendorf's theorem, any natural number \(n\) can be uniquely expressed as a sum of distinct Fibonacci numbers:
with \(k_1 \ge k_2 + 2,\ k_2 \ge k_3 + 2,\ \ldots,\ k_r \ge 2\) (i.e., no two consecutive Fibonacci numbers are used).
Thus, we can represent a positive integer using the encoding \(d_0 d_1 d_2 \dots d_s 1\), where \(d_i=1\) indicates that \(F_{i+2}\) is used. We forcibly append a \(1\) at the end of the encoding (which will create two adjacent \(1\)s) to indicate the end of the encoding. Here are some examples:
The encoding process for \(n\) can be solved using a greedy algorithm:
- Enumerate Fibonacci numbers \(F_i\) from largest to smallest until \(F_i\le n\).
- Subtract \(F_i\) from \(n\), and place a \(1\) at position \(i-2\) in the encoding (positions are indexed from left to right starting at \(0\)).
- If \(n\) is positive, return to step 1.
- Finally, append a \(1\) to the end of the encoding to indicate the end.
The decoding process is similar: first remove the trailing \(1\), then for each position \(i\) (indexed from left to right starting at \(0\)) where the encoding is \(1\), add \(F_{i+2}\) to the answer. The final sum is the original number.
Periodicity Under Modulo
Consider the Fibonacci sequence modulo \(p\). Using the pigeonhole principle, it can be easily shown that the sequence is periodic. Consider the first \(p^2+1\) pairs of consecutive Fibonacci numbers modulo \(p\):
The residue system modulo \(p\) has size \(p\), so among the first \(p^2+1\) pairs, there must be two identical pairs. These two pairs will generate the same Fibonacci sequence thereafter, indicating periodicity.
Pisano Period
The smallest positive period of the Fibonacci sequence modulo \(m\) is called the Pisano period (OEIS A001175).
The Pisano period never exceeds \(6m\), and equality holds only when \(m=2\times 5^k\).
When computing the \(n\)-th Fibonacci number modulo \(m\) for very large \(n\), it is necessary to compute the period of the Fibonacci sequence modulo \(m\). Note that we only need to compute a period, not necessarily the minimal positive period.
It is easy to verify that the minimal positive period of Fibonacci numbers modulo \(2\) is \(3\), and modulo \(5\) is \(20\).
Clearly, if \(a\) and \(b\) are coprime, the Pisano period of \(ab\) is the least common multiple of the Pisano periods of \(a\) and \(b\).
Computing the period also requires the following conclusions:
Conclusion 1: For odd primes \(p\equiv 1,4 \pmod 5\), \(p-1\) is a period of Fibonacci numbers modulo \(p\). That is, the Pisano period of an odd prime \(p\) divides \(p-1\).
Proof:
In this case, \(5^\frac{p-1}{2} \equiv 1\pmod p\).
By binomial expansion:
Since both \(F_p\) and \(F_{p+1}\) are congruent to \(1\), matching \(F_1\) and \(F_2\), \(p-1\) is a period.
Conclusion 2: For odd primes \(p\equiv 2,3 \pmod 5\), \(2p+2\) is a period of Fibonacci numbers modulo \(p\). That is, the Pisano period of an odd prime \(p\) divides \(2p+2\).
Proof:
In this case, \(5^\frac{p-1}{2} \equiv -1\pmod p\).
By binomial expansion:
After modulo \(p\), in \(F_{2p}\), only the term \(\dbinom{2p}{p}\equiv 2 \pmod p\) remains; in \(F_{2p+1}\), the terms \(\dbinom{2p+1}{1}\equiv 1 \pmod p\), \(\dbinom{2p+1}{p}\equiv 2 \pmod p\), and \(\dbinom{2p+1}{2p+1}\equiv 1 \pmod p\) remain.
Thus, \(F_{2p}\) and \(F_{2p+1}\) match \(F_{-2}\) and \(F_{-1}\), so \(2p+2\) is a period.
Conclusion 3: For a prime \(p\), if \(M\) is a period of Fibonacci numbers modulo \(p^{k-1}\), then \(Mp\) is a period of Fibonacci numbers modulo \(p^k\). In particular, if \(M\) is the Pisano period modulo \(p^{k-1}\), then \(Mp\) is the Pisano period modulo \(p^k\).
Proof:
This proof treats \(\frac{1+\sqrt{5}}{2}\) as a whole.
Since:
Therefore:
Since the converse can also be derived, \(M\) is a period of Fibonacci numbers modulo \(p^{k-1}\) if and only if:
When \(p\) is an odd prime, by the Lifting the Exponent Lemma, we have:
When \(p=2\), by the Lifting the Exponent Lemma, we have:
Substituting \(a\) with \(\left(\frac{1+\sqrt{5}}{2}\right)\) and \(\left(\frac{1-\sqrt{5}}{2}\right)\), and \(t\) with \(M\) and \(Mp\), the above condition is equivalent to:
Thus, it is also equivalent to \(Mp\) being a period of Fibonacci numbers modulo \(p^k\).
Since the periods are equivalent, the minimal positive periods are also equivalent.
With these three conclusions proven, we can write the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
Exercises
- SPOJ - Euclid Algorithm Revisited
- SPOJ - Fibonacci Sum
- HackerRank - Is Fibo
- Project Euler - Even Fibonacci numbers
-
Luogu P4000 Fibonacci Sequence
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 is licensed under CC-BY-SA 4.0.
本页面最近更新:,更新历史
发现错误?想一起完善? 在 GitHub 上编辑此页!
本页面贡献者:OI-wiki
本页面的全部内容在 CC BY-SA 4.0 和 SATA 协议之条款下提供,附加条款亦可能应用