10000 Merge pull request #1453 from cp-algorithms/fibmatrix · cp-algorithms/cp-algorithms@79d767c · GitHub
[go: up one dir, main page]

Skip to content

Commit 79d767c

Browse files
authored
Merge pull request #1453 from cp-algorithms/fibmatrix
Fibonacci: better motivation for matrix form
2 parents 91672f0 + 375ca6b commit 79d767c

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/algebra/fibonacci-numbers.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,50 @@ In this way, we obtain a linear solution, $O(n)$ time, saving all the values pri
116116
117117
### Matrix form
118118
119-
It is easy to prove the following relation:
119+
To go from $(F_n, F_{n-1})$ to $(F_{n+1}, F_n)$, we can express the linear recurrence as a 2x2 matrix multiplication:
120120
121-
$$\begin{pmatrix} 1 & 1 \cr 1 & 0 \cr\end{pmatrix} ^ n = \begin{pmatrix} F_{n+1} & F_{n} \cr F_{n} & F_{n-1} \cr\end{pmatrix}$$
121+
$$
122+
\begin{pmatrix}
123+
1 & 1 \\
124+
1 & 0
125+
\end{pmatrix}
126+
\begin{pmatrix}
127+
F_n \\
128+
F_{n-1}
129+
\end{pmatrix}
130+
=
131+
\begin{pmatrix}
132+
F_n + F_{n-1} \\
133+
F_{n}
134+
\end{pmatrix}
135+
=
136+
\begin{pmatrix}
137+
F_{n+1} \\
138+
F_{n}
139+
\end{pmatrix}
140+
$$
141+
142+
This lets us treat iterating the recurrence as repeated matrix multiplication, which has nice properties. In particular,
143+
144+
$$
145+
\begin{pmatrix}
146+
1 & 1 \\
147+
1 & 0
148+
\end{pmatrix}^n
149+
\begin{pmatrix}
150+
F_1 \\
151+
F_0
152+
\end{pmatrix}
153+
=
154+
\begin{pmatrix}
155+
F_{n+1} \\
156+
F_{n}
157+
\end{pmatrix}
158+
$$
159+
160+
where $F_1 = 1, F_0 = 0$.
122161
123-
Thus, in order to find $F_n$ in $O(log n)$ time, we must raise the matrix to n. (See [Binary exponentiation](binary-exp.md))
162+
Thus, in order to find $F_n$ in $O(\log n)$ time, we must raise the matrix to n. (See [Binary exponentiation](binary-exp.md))
124163
125164
```{.cpp file=fibonacci_matrix}
126165
struct matrix {

0 commit comments

Comments
 (0)
0