5

From this website, http://www.nayuki.io/page/fast-fibonacci-algorithms (fast doubling proof close to the bottom of the page).

I have understood the proof for the most part but I am struggling to see how this part of the proof works especially when the the F(n) function is squared.

\begin{align} F(2n+1) &= F(n+1)^2 + F(n)^2. \\ F(2n) &= F(n) \left[ F(n+1) + F(n-1) \right] \\ &= F(n) \left[ F(n+1) + (F(n+1) - F(n)) \right] \\ &= F(n) \left[ 2F(n+1) - F(n) \right]. \\ F(2n-1) &= F(n)^2 + F(n-1)^2. \end{align}

3 Answers3

5

Start with:

$$F(n+1) = F(n) + F(n-1)$$

Rewrite as:

$$\begin{align} F(n + 1) &= F(n) + F(n-1) \\ F(n) &= F(n) \end{align}$$

which is:

$$\begin{align} \begin{bmatrix} F(n+1) \\ F(n)\end{bmatrix} &= \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix} \begin{bmatrix} F(n) \\ F(n-1)\end{bmatrix} \\ &= \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^2 \begin{bmatrix} F(n-1) \\ F(n-2)\end{bmatrix} \\ &= \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^3 \begin{bmatrix} F(n-1) \\ F(n-3)\end{bmatrix} \\ & \dots \\ &= \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^n \begin{bmatrix} F(1) \\ F(0)\end{bmatrix} \end{align}$$

Taking $F(0) = 0$ and $F(1) = 1$, you get:

$$\begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^n = \begin{bmatrix} F(n+1) & F(n) \\ F(n) & F(n-1) \end{bmatrix}$$

So to use "doubling", we just plug $2n$ into the formula:

$$\begin{align} \begin{bmatrix} F(2n+1) \\ F(2n)\end{bmatrix} &= \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^{2n} \begin{bmatrix} F(1) \\ F(0)\end{bmatrix} \\ \\ &= \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^{n} \begin{bmatrix} 1 & 1 \\ 1 & 0 \end{bmatrix}^{n} \begin{bmatrix} F(1) \\ F(0)\end{bmatrix} \\ \\ &= \begin{bmatrix} F(n+1) & F(n) \\ F(n) & F(n-1) \end{bmatrix} \begin{bmatrix} F(n+1) & F(n) \\ F(n) & F(n-1) \end{bmatrix} \begin{bmatrix} 1 \\ 0 \end{bmatrix} \\ \\ &= \begin{bmatrix} F(n+1)^2 + F(n)^2 \\ F(n)F(n+1) + F(n-1)F(n) \end{bmatrix} \\ \\ ~ \\ & \text{And if you want...} \\ ~ \\ &= \begin{bmatrix} F(n+1)^2 + F(n)^2 \\ F(n)F(n+1)+ \bigg(F(n+1) - F(n)\bigg)F(n) \end{bmatrix}\\ \\&= \begin{bmatrix} F(n+1)^2 + F(n)^2 \\ 2F(n+1)F(n) - F(n)^2 \end{bmatrix} \end{align}$$

Which isn't actually better than matrix exponentiation asymptotically. And regardless, since the Fibonacci sequence grows exponentially, it will always require exponential time to compute just due to the size of the output. The matrix or "doubling" approach takes you from $O({\rm exp}~x^2)$ to $O({\rm exp}~x)$ asymptotic calculation time, which isn't nothing, but it still isn't exactly tractable either.

DanielV
  • 23,556
  • Your answer is largely excellent. I have a problem with the last paragraph - "exponential time to compute due to the size of the output". This is not true, because the bit length of the output is linearly proportional to the value of the input. In other words, $\log_2 F(n) \in O(n)$. – Nayuki Apr 24 '16 at 18:19
  • Thank you, but I can't agree. Runtime is usually (admittedly loosely) defined in terms the radix-B encoding length of the data structure. It is that way for trees, graphs, lists, whatever. For natural numbers, that would be $F(\sum_{k=0}^n c_kB^k) \in O(\exp n)$. Do you know a reason to make an exception for numbers? – DanielV Apr 24 '16 at 18:40
  • I see where you're coming from. There are some algorithms whose runtime is described in terms of the value instead of the bit length. For example: Ford-Fulkerson, subset sum (dynamic programming solution). But which convention to use is a matter of opinion and style, as long as the assumptions are stated clearly – Nayuki Apr 24 '16 at 19:56
  • @DanielV Can you please explain how did you get the expression immediately following the sentence: Taking F(0)=0 and F(1)=1, you get:? – Autonomous May 29 '17 at 21:17
  • @ParagS.Chandakkar Using the same approach above the sentence, you can derive $$\begin{bmatrix} f(n) \ f(n - 1) \end{bmatrix} = \begin{bmatrix} 1 & 1 \ 1 & 0 \end{bmatrix}^n \begin{bmatrix} f(0) \ f(-1) \end{bmatrix}$$ The initial conditions tell you $f(-1) = 1$. Then just use the property of matrices $$a = M~b \text{ and } c = M~d \text{ iff } \begin{bmatrix}a & c\end{bmatrix} = M \begin{bmatrix} c & d \end{bmatrix}$$ – DanielV May 30 '17 at 13:27
  • 1
    I think you mean $[a \ c] = M \ [b \ d]$, but I understood your point. Thanks. – Autonomous May 30 '17 at 18:00
1

This follows immediately by looking at the preceding lines. Since the two square matrices are equal, each corresponding entry must be equal. By looking at the $(1,1)$-entry we get $F(2n+1)=F(n+1)^2+F(n)^2$; by looking at either the $(1,2)$- or $(2,1)$-entry we get $F(2n)=F(n)F(n+1)+F(n)F(n-1)$, from which the other lines are just basic manipulations.

Jason
  • 15,438
  • I understand how the final matrix was computed but I do not understand for instance how F(n+1)^2 +F(n)^2 is computed to be F(2n+1). Sorry I am a novice in this area. – user3458913 Jan 29 '15 at 06:37
0

While playing around with the Fibonacci series. I found a way to compute nth Fibonacci number in Log(N) complexity without using matrices.

My method is simpler and intuitive and could be used for self derivation.

I wrote a medium blog regarding this. Thought it would be helpful.

https://medium.com/@ani1998ket/deriving-the-fast-fibonacci-identities-without-matrices-o-log-n-4cd9ce69d9d4

  • The coeff pairs that you found is nothing but the coeff matrix raised to some power. So, in essence, you are using the matrix - may be unknowingly. Nonetheless, its always amazing feeling to discover something! – Ravi Tiwari Jan 03 '23 at 20:25