Is there a faster way to compute the nth Fibonacci number than by fib2 (page 13 )? One idea involves matrices.
We start by writing the equations $F_{1}=F_{1}$ and $F_{2}=F_{0}+F_{1}$ in matrix notation:
\[
\left(\begin{array}{l}
F_{1} \\
F_{2}
\end{array}\right)=\left(\begin{array}{ll}
0 & 1 \\
1 & 1
\end{array}\right) \cdot\left(\begin{array}{l}
F_{0} \\
F_{1}
\end{array}\right)
\]
Similarly,
\[
\left(\begin{array}{l}
F_{2} \\
F_{3}
\end{array}\right)=\left(\begin{array}{ll}
0 & 1 \\
1 & 1
\end{array}\right) \cdot\left(\begin{array}{l}
F_{1} \\
F_{2}
\end{array}\right)=\left(\begin{array}{ll}
0 & 1 \\
1 & 1
\end{array}\right)^{2} \cdot\left(\begin{array}{l}
F_{0} \\
F_{1}
\end{array}\right)
\]
and in general
\[
\left(\begin{array}{c}
F_{n} \\
F_{n+1}
\end{array}\right)=\left(\begin{array}{cc}
0 & 1 \\
1 & 1
\end{array}\right)^{n} \cdot\left(\begin{array}{c}
F_{0} \\
F_{1}
\end{array}\right)
\]
So, in order to compute $F_{n},$ it suffices to raise this $2 \times 2$ matrix, call it $X,$ to the $n$ th power.
(a) Show that two $2 \times 2$ matrices can be multiplied using 4 additions and 8 multiplications.
But how many matrix multiplications does it take to compute $X^{n} ?$
(b) Show that $O(\log n)$ matrix multiplications suffice for computing $X^{n}$. (Hint:Think about computing $X^{8}$, )
Thus the number of arithmetic operations needed by our matrix-based algorithm, call it $\underline{f}$ ib a is just $O(\log n),$ as compared to $O(n)$ for $f$ ib 2 . Have we broken another exponential barrier?
The catch is that our new algorithm involves multiplication, not just addition; and multiplications of large numbers are slower than additions. We have already seen that, when the complexity of arithmetic operations is taken into account, the running time of $\mathrm{fib} 2$ becomes $O\left(n^{2}\right)$
(c) Show that all intermediate results of $f$ ib 3 are $O(n)$ bits long.
(d) Let $M(n)$ be the running time of an algorithm for multiplying $n$ -bit numbers, and assume that $M(n)=O\left(n^{2}\right)$ (the school method for multiplication, recalled in Chapter 1 , achieves this). Prove that the running time of $f$ ib 3 is $O(M(n) \log n$ ).
(e) Can you prove that the running time of $\mathrm{fib} 3$ is $\mathrm{O}(\mathrm{M}(n)) ?$ (Hint: The lengths of the numbers being multiplied get doubled with every squaring.)
In conclusion, whether fib3 is faster than $\mathrm{fib} 2$ depends on whether we can multiply $n$ -bit integers faster than $O\left(n^{2}\right) .$ Do you think this is possible? (The answer is in Chapter $2 .$ ) Finally, there is a formula for the Fibonacci numbers:
\[
F_{n}=\frac{1}{\sqrt{5}}\left(\frac{1+\sqrt{5}}{2}\right)^{n}-\frac{1}{\sqrt{5}}\left(\frac{1-\sqrt{5}}{2}\right)^{n}
\]
So, it would appear that we only need to raise a couple of numbers to the $n$ th power in order to compute $F_{n} .$ The problem is that these numbers are irrational, and computing them to sufficient accuracy is nontrivial. In fact, our matrix method fib3 can be seen as a roundabout way of raising these irrational numbers to the $n$ th power. If you know your linear algebra, you should see why. (Hint: What are the eigenvalues of the matrix $X ?$ )