The Fibonacci Sequence

The Fibonacci sequence (OEIS A000045) is defined as follows:

F0=0,F1=1,Fn=Fn−1+Fn−2F_0 = 0, F_1 = 1, F_n = F_{n-1} + F_{n-2}

Its first few terms are:

0,1,1,2,3,5,8,13,21,34,55,89,…0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, \dots

The Lucas Sequence

The Lucas sequence (OEIS A000032) is defined as follows:

L0=2,L1=1,Ln=Ln−1+Ln−2L_0 = 2, L_1 = 1, L_n = L_{n-1} + L_{n-2}

Its first few terms are:

2,1,3,4,7,11,18,29,47,76,123,199,…2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123, 199, \dots

The Lucas sequence is often used as a tool when studying the Fibonacci sequence.

Closed Form of the Fibonacci Sequence

The nn-th Fibonacci number can be computed from the recurrence in \Theta(n) time. However, faster methods are available. Θ(n)\Theta (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=(1+52)n−(1−52)n5F_n = \frac{\left(\frac{1 + \sqrt{5}}{2}\right)^n - \left(\frac{1 - \sqrt{5}}{2}\right)^n}{\sqrt{5}}

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 11 and decreases exponentially. Therefore, the formula can be written as

Fn=[(1+52)n5]F_n = \left[\frac{\left(\frac{1 + \sqrt{5}}{2}\right)^n}{\sqrt{5}}\right]

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=(1+52)n+(1−52)nL_n = \left(\frac{1 + \sqrt{5}}{2}\right)^n + \left(\frac{1 - \sqrt{5}}{2}\right)^n

which is very similar to the Fibonacci formula. In fact,

Ln+Fn52=(1+52)n\frac{L_n + F_n\sqrt{5}}{2} = \left(\frac{1 + \sqrt{5}}{2}\right)^n

That is, LnL_n and FnF_n are precisely the numerator coefficients obtained after expanding (1+52)n\left(\frac{1 + \sqrt{5}}{2}\right)^n binomially and collecting like terms. Thus, all solutions of the Pell equation

x2−5y2=−4x^2-5y^2=-4

are exactly

xn+yn52=Ln+Fn52\frac{x_n + y_n\sqrt{5}}{2} = \frac{L_n + F_n\sqrt{5}}{2}

namely the Lucas and Fibonacci sequences. Consequently,

Ln2−5Fn2=−4{L_n}^2-5{F_n}^2=-4

Matrix Form

The Fibonacci recurrence can be expressed through matrix multiplication:

[Fn−1Fn]=[Fn−2Fn−1][0111]\begin{bmatrix}F_{n-1} & F_{n} \cr\end{bmatrix} = \begin{bmatrix}F_{n-2} & F_{n-1} \cr\end{bmatrix} \begin{bmatrix}0 & 1 \cr 1 & 1 \cr\end{bmatrix}

Let P=[0111]P = \begin{bmatrix}0 & 1 \cr 1 & 1 \cr\end{bmatrix}. Then

[FnFn+1]=[F0F1]Pn\begin{bmatrix}F_n & F_{n+1} \cr\end{bmatrix} = \begin{bmatrix}F_0 & F_1 \cr\end{bmatrix} P^n

Thus, matrix multiplication computes Fibonacci numbers in Θ(log⁡n)\Theta(\log n) time. The formula from the preceding section can also be obtained by diagonalizing the matrix.

Fast Doubling

The method above yields

F2k=Fk(2Fk+1−Fk)F2k+1=Fk+12+Fk2\begin{aligned} F_{2k} &= F_k (2 F_{k+1} - F_{k}) \\ F_{2k+1} &= F_{k+1}^2 + F_{k}^2 \end{aligned}

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)(F_n,F_{n+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};
  else
    return {c, d};
}

Properties

The Fibonacci sequence has many interesting properties. Some simple ones are listed below:

  1. Cassini's identity: Fn−1Fn+1−Fn2=(−1)nF_{n-1} F_{n+1} - F_n^2 = (-1)^n.
  2. Addition identity: Fn+k=FkFn+1+Fk−1FnF_{n+k} = F_k F_{n+1} + F_{k-1} F_n.
  3. Setting k=nk = n in the preceding identity gives F2n=Fn(Fn+1+Fn−1)F_{2n} = F_n (F_{n+1} + F_{n-1}).
  4. By induction from the preceding identity, ∀k∈N,Fn∣Fnk\forall k\in \mathbb{N},F_n|F_{nk}.
  5. The preceding property has a converse: ∀Fa∣Fb,a∣b\forall F_a|F_b,a|b.
  6. GCD identity: (Fm,Fn)=F(m,n)(F_m, F_n) = F_{(m, n)}.
  7. 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,

Ln+Fn52=(1+52)n\frac{L_n + F_n\sqrt{5}}{2} = \left(\frac{1 + \sqrt{5}}{2}\right)^n

resembles

cos⁡nx+isin⁡nx=(cos⁡x+isin⁡x)n\cos nx + i\sin nx = \left(\cos x + i\sin x\right)^n

and

Ln2−5Fn2=−4{L_n}^2-5{F_n}^2=-4

resembles

cos⁡2x+sin⁡2x=1\cos^2 x + \sin^2 x = 1

Thus, the Lucas sequence is similar to the cosine function, while the Fibonacci sequence is similar to the sine function. From

(1+52)m(1+52)n=(1+52)m+n\left(\frac{1 + \sqrt{5}}{2}\right)^m\left(\frac{1 + \sqrt{5}}{2}\right)^n = \left(\frac{1 + \sqrt{5}}{2}\right)^{m+n}

we obtain the identities for the sum of two indices:

2Lm+n=5FmFn+LmLn2L_{m+n}=5F_mF_n+L_mL_n 2Fm+n=FmLn+LmFn2F_{m+n}=F_mL_n+L_mF_n

and hence the doubling identities:

L2n=Ln2−2(−1)nL_{2n}={L_n}^2-2{\left(-1\right)}^n F2n=FnLnF_{2n}=F_nL_n

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 nn has a unique representation as a sum of Fibonacci numbers:

N=Fk1+Fk2+…+FkrN = F_{k_1} + F_{k_2} + \ldots + F_{k_r}

where k1≥k2+2, k2≥k3+2, …, kr≥2k_1 \ge k_2 + 2,\ k_2 \ge k_3 + 2,\ \ldots,\ k_r \ge 2 (that is, two adjacent Fibonacci numbers cannot both be used).

We can therefore encode a positive integer as d0d1d2…ds1d_0 d_1 d_2 \dots d_s 1, where di=1d_i=1 means that Fi+2F_{i+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:

1=1=F2=(11)F2=2=F3=(011)F6=5+1=F5+F2=(10011)F8=8=F6=(000011)F9=8+1=F6+F2=(100011)F19=13+5+1=F7+F5+F2=(1001011)F\begin{aligned} 1 &=& 1 &=& F_2 &=& (11)_F \\ 2 &=& 2 &=& F_3 &=& (011)_F \\ 6 &=& 5 + 1 &=& F_5 + F_2 &=& (10011)_F \\ 8 &=& 8 &=& F_6 &=& (000011)_F \\ 9 &=& 8 + 1 &=& F_6 + F_2 &=& (100011)_F \\ 19 &=& 13 + 5 + 1 &=& F_7 + F_5 + F_2 &=& (1001011)_F \end{aligned}

The encoding of nn can be found greedily:

  1. Enumerate Fibonacci numbers from largest to smallest until reaching Fi≤nF_i\le n.
  2. Subtract FiF_i from nn and put a 1 at position i−2i-2 in the code, numbered from left to right starting at 0.
  3. If nn is positive, return to step 1.
  4. Append a 1 at the end of the code to mark its end. FiF_i

Decoding is analogous: first remove the final 1; for every position ii containing a 1, numbered from left to right starting at 0, add Fi+2F_{i+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+1m^2+1 pairs modulo mm:

(F0, F1), (F1, F2), …, (Fm2, Fm2+1)(F_0,\ F_1),\ (F_1,\ F_2),\ \ldots,\ (F_{m^2},\ F_{m^2 + 1})

There are mm residue classes modulo mm, so at most m2m^2 distinct pairs are possible. Hence two of the first m2+1m^2+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 m2m^2.

Pisano Period

The smallest positive period of the Fibonacci sequence modulo mm is called the Pisano period (Pisano period, OEIS A001175). We denote it by π(m)\pi(m). mm

This observation can be used to compute the nn-th Fibonacci number modulo mm. If nn is very large, we need only compute a period modulo mm, not necessarily the smallest positive period.

The following facts hold:

  1. For coprime moduli m1,m2m_1,m_2, π(m1m2)=lcm⁡(π(m1),π(m2))\pi(m_1m_2)=\operatorname{lcm}(\pi(m_1),\pi(m_2)).
  2. For a prime pp and positive integer ee, π(pe)∣pe−1π(p)\pi(p^{e})\mid p^{e-1}\pi(p).
  3. For m=2e (e∈N+)m=2^e~(e\in\mathbf N_+), π(m)=3⋅2e−1\pi(m)=3\cdot 2^{e-1}.
  4. For m=5e (e∈N+)m=5^e~(e\in\mathbf N_+), π(m)=4⋅5e\pi(m)=4\cdot 5^e.
  5. For a prime p≡±1(mod10)p\equiv\pm1\pmod{10}, π(p)∣(p−1)\pi(p)\mid(p-1); for a prime p≡±3(mod10)p\equiv\pm3\pmod{10}, π(p)∣2(p+1)\pi(p)\mid 2(p+1).

Together these facts show that the Pisano period modulo mm is at most 6m6m, with equality if and only if m=2×5e (e∈N+)m = 2\times 5^e~(e\in\mathbf 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_t calc_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;
    } else if (p == 5) {
      cur *= 20;
    } else if (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)O(\sqrt{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 mm are all O(m)O(m).

First, by the Chinese remainder theorem, it suffices to consider prime-power moduli. Let m1,m2m_1,m_2 be coprime. The periods modulo m1m_1 and m2m_2 are respectively multiples of π(m1)\pi(m_1) and π(m2)\pi(m_2), so the smallest positive period modulo m1m2m_1m_2 is \operatorname{lcm}(\pi(m_1),\pi(m_2)). This proves fact 1. π(m1)\pi(m_1) π(m2)\pi(m_2)

The Pisano period modulo mm is also the smallest positive integer kk such that

Ak=(1110)k≡I(modm).A^k = \begin{pmatrix} 1&1\\1&0 \end{pmatrix}^k \equiv I \pmod{m}.

In other words, it is the order of AA modulo mm1.

For m=pem=p^e, let k=π(pe)k=\pi(p^e). There is a matrix Λ\Lambda such that

Ak=peΛ+IA^k = p^e\Lambda + I

By the binomial theorem,

Akp=(peΛ+I)p=I+∑i=1p(pi)(peΛ)i≡I(modpe+1).A^{kp} = (p^e\Lambda + I)^p = I + \sum_{i=1}^p\binom{p}{i}(p^e\Lambda)^i \equiv I\pmod{p^{e+1}}.

Thus, by the properties of orders, π(pe+1)∣kp=pπ(pe)\pi(p^{e+1})\mid kp = p\pi(p^e). Induction on ee gives π(pe)∣pe−1π(p)\pi(p^e)\mid p^{e-1}\pi(p).

For a prime modulus pp, we give two proofs.

Using the Closed Form

Use

Fn=15(1+52)n−15(1−52)n.F_n = \dfrac{1}{\sqrt{5}}\left(\dfrac{1+\sqrt{5}}{2}\right)^n - \dfrac{1}{\sqrt{5}}\left(\dfrac{1-\sqrt{5}}{2}\right)^n.

Expanding by the binomial theorem and eliminating radicals gives

Fn=12n−1∑i=0⌊(n−1)/2⌋(n2i+1)5i.F_n = \dfrac{1}{2^{n-1}}\sum_{i=0}^{\lfloor(n-1)/2\rfloor}\binom{n}{2i+1}5^i.

For p=2p=2, this expression cannot be reduced modulo p directly, but π(2)=3\pi(2)=3 can be verified. For p=5p=5, Fn≡n⋅3n−1(modp)F_n\equiv n\cdot 3^{n-1}\pmod{p}, and π(5)=20\pi(5)=20 follows directly. For the remaining odd primes:

  • If p≡1,4(mod5)p\equiv 1,4\pmod{5}, then

    Fp≡12p−1(pp)5(p−1)/2≡1(modp),Fp+1≡12p((p+11)+(p+1p)5(p−1)/2)≡1(modp).\begin{aligned} F_{p} &\equiv \dfrac{1}{2^{p-1}}\binom{p}{p}5^{(p-1)/2} \equiv 1 \pmod{p},\\ F_{p+1} &\equiv \dfrac{1}{2^p}\left(\binom{p+1}{1} + \binom{p+1}{p}5^{(p-1)/2}\right) \equiv 1 \pmod{p}. \end{aligned}

    Here we use Lucas's theorem, Fermat's little theorem, and quadratic reciprocity: (pk)≡0(modp)\dbinom{p}{k}\equiv 0\pmod{p} for 0<k<p0 < k < p, (p+1k)≡0(modp)\dbinom{p+1}{k}\equiv 0\pmod{p} for 1<k<p1 < k < p, 2p−1≡5p−1≡1(modp)2^{p-1}\equiv 5^{p-1}\equiv 1\pmod{p}, and 5(p−1)/2≡1(modp)5^{(p-1)/2} \equiv 1\pmod{p}. Therefore (Fp,Fp+1)≡(F1,F2)(modp)(F_p,F_{p+1}) \equiv (F_1,F_2) \pmod{p}, so (p−1)(p-1) is a period and π(p)∣(p−1)\pi(p)\mid(p-1).

  • If p≡2,3(mod5)p\equiv 2,3\pmod{5}, then p≡1,4(mod5)p\equiv 1,4\pmod{5} pp 55 55 pp pp

    F2p≡122p−1(2pp)5(p−1)/2≡−1(modp),F2p+1≡122p((2p+11)+(2p+1p)5(p−1)/2+(2p+12p+1)5p)≡−1(modp).\begin{aligned} F_{2p} &\equiv \dfrac{1}{2^{2p-1}}\binom{2p}{p}5^{(p-1)/2} \equiv -1 \pmod{p},\\ F_{2p+1} &\equiv \dfrac{1}{2^{2p}}\left(\binom{2p+1}{1} + \binom{2p+1}{p}5^{(p-1)/2} + \binom{2p+1}{2p+1}5^p\right) \equiv -1\pmod{p}. \end{aligned}

    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)2^{p-1}\equiv 5^{p-1}\equiv 1\pmod{p}, while quadratic reciprocity gives 5(p−1)/2≡−1(modp)5^{(p-1)/2} \equiv -1\pmod{p}. Hence (F2p,F2p+1)≡(F−2,F−1)(modp)(F_{2p},F_{2p+1}) \equiv (F_{-2},F_{-1}) \pmod{p}, so 2(p+1)2(p+1) is a period and π(p)∣2(p+1)\pi(p)\mid 2(p+1). 0<k<p0 < k < p p<k<2pp < k < 2p (pk)≡0(modp)\dbinom{p}{k}\equiv 0\pmod{p} (2pp)≡2(modp)\dbinom{2p}{p}\equiv 2\pmod{p} 1<k<p1 < k < p p+1<k<2pp + 1 < k < 2p (pk)≡0(modp)\dbinom{p}{k}\equiv 0\pmod{p} (2p+1p)≡2(modp)\dbinom{2p+1}{p}\equiv 2\pmod{p} p≡2,3(mod5)p\equiv 2,3\pmod{5} pp 55 55 pp pp

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)A=\begin{pmatrix}1&1\\1&0\end{pmatrix} directly. Its characteristic polynomial is f(x)=x2−x−1f(x) = x^2-x-1, with discriminant Δ=5\Delta = 5. For p=5p=5, Δ≡0(mod5)\Delta\equiv 0\pmod{5}; AA has the repeated eigenvalue λ=3\lambda=3 and is not diagonalizable, so this case must be handled separately. For p≡1,4(mod5)p\equiv 1,4\pmod{5}, quadratic reciprocity says that Δ=5\Delta=5 is a quadratic residue modulo pp. Thus AA has two distinct eigenvalues in Fp\mathbf F_p, and its order divides ∣Fp×∣=p−1|\mathbf F_p^\times|=p-1. For p≡2,3(mod5)p\equiv 2,3\pmod{5}, Δ=5\Delta=5 is a quadratic nonresidue modulo pp. The two distinct eigenvalues lie in the field extension Fp[5]\mathbf F_p[\sqrt{5}]. Frobenius swaps them, so λ2=λ1p\lambda_2=\lambda_1^p and λ1p+1=λ2p+1=λ1λ2=−1\lambda_1^{p+1}=\lambda_2^{p+1}=\lambda_1\lambda_2=-1. Hence both eigenvalues have order dividing 2(p+1), so the order of AA divides 2(p+1)2(p+1). λ1≠λ2\lambda_1\neq\lambda_2 lcm⁡(ord⁡(λ1),ord⁡(λ2))\operatorname{lcm}(\operatorname{ord}(\lambda_1),\operatorname{ord}(\lambda_2)) AA Fp\mathbf F_p λ1≠λ2\lambda_1\neq\lambda_2 x↦xpx\mapsto x^p λ12(p+1)=λ22(p+1)=1\lambda_1^{2(p+1)}=\lambda_2^{2(p+1)}=1 AA lcm⁡(ord⁡(λ1),ord⁡(λ2))\operatorname{lcm}(\operatorname{ord}(\lambda_1),\operatorname{ord}(\lambda_2))

Thus:

  • π(2e)=32⋅2e, 14π(5e)=5e\pi(2^e)=\dfrac{3}{2}\cdot 2^e,~\dfrac{1}{4}\pi(5^e)=5^e.
  • If p≡±1(mod10)p\equiv\pm1\pmod{10}, then π(pe)∣(p−1)pe−1\pi(p^e) \mid (p-1)p^{e-1}, so π(pe)≤pe\pi(p^e)\le p^e.
  • If p≡±3(mod10)p\equiv\pm3\pmod{10}, then 14π(pe)∣p+12pe−1\dfrac{1}{4}\pi(p^e) \mid \dfrac{p+1}{2}p^{e-1}, so 14π(pe)≤pe\dfrac{1}{4}\pi(p^e)\le p^e.

For m=∏ipieim=\prod_i p_i^{e_i}, fact 1 gives

π(m)=lcm⁡{π(piei):pi∈P}≤lcm⁡{π(piei):pi=2 or pi≡±1 (mod⁡10)}⋅4⋅lcm⁡{π(piei)/4:pi=5 or pi≡±3 (mod⁡10)}≤∏{π(piei):pi=2 or pi≡±1 (mod⁡10)}⋅4⋅∏{π(piei)/4:pi=5 or pi≡±3 (mod⁡10)}≤32⋅∏{piei:pi=2 or pi≡±1 (mod⁡10)}⋅4⋅∏{piei:pi=5 or pi≡±3 (mod⁡10)}=6m.\begin{aligned} \pi(m)&=\operatorname{lcm}\{\pi(p_i^{e_i}):p_i\in\mathbf P\} \\ &\le \operatorname{lcm}\{\pi(p_i^{e_i}):p_i=2\text{ or }p_i\equiv\pm1~(\operatorname{mod}{10})\}\\ &\quad \cdot 4\cdot\operatorname{lcm}\{\pi(p_i^{e_i})/4:p_i=5\text{ or }p_i\equiv\pm3~(\operatorname{mod}{10})\}\\ &\le \prod\{\pi(p_i^{e_i}):p_i=2\text{ or }p_i\equiv\pm1~(\operatorname{mod}{10})\}\\ &\quad \cdot 4\cdot\prod\{\pi(p_i^{e_i})/4:p_i=5\text{ or }p_i\equiv\pm3~(\operatorname{mod}{10})\}\\ &\le \dfrac{3}{2}\cdot\prod\{p_i^{e_i}:p_i=2\text{ or }p_i\equiv\pm1~(\operatorname{mod}{10})\}\\ &\quad \cdot 4\cdot\prod\{p_i^{e_i}:p_i=5\text{ or }p_i\equiv\pm3~(\operatorname{mod}{10})\}\\ &= 6m. \end{aligned}

Therefore the Pisano period modulo mm is always at most 6m6m, with equality exactly when m=2⋅5em=2\cdot 5^e.

Exercises

References and Notes

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

  1. Strictly speaking, it is the order of matrix AA in the general linear group GL2(Zm)GL_2(\mathbf Z_m). ↩

This content comes from OI Wiki: The Fibonacci Sequence. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.