Solving Recurrence Relations
win-vector.com
win-vector.com
My personal preference for understanding linear recurrence relations (and understanding where/why sqrt(5) shows up for the Fibonacci sequence) is via eigenvalues and eigenvectors of a corresponding linear transformation. For example:
[F_n ] = [1 1] [F_n-1]
[F_n-1] [1 0] [F_n-2]
and therefore [F_n ] = [1 1]^n-2 [F_2] = [1 1]^n-2 [1]
[F_n-1] [1 0] [F_1] [1 0] [1]
and therefore the nth Fibonacci number can be computed by computing the (n-2)th power of the matrix [[1, 1], [1, 0]], and then multiplying that matrix by the column vector [1, 1] (or any other desired first terms of the sequence). The matrix power can be computed by diagonalizing, and that's where the powers of the golden ratio will come up (as eigenvalues of that matrix).This is completely equivalent to the method in TFA, but to my mind is better motivated.
It was always fun to hit the interviewer with this, it’s exponentially faster than the memoization they were usually expecting.
First thoughts: The memoization algorithm is linear in n, and the repeated squaring algorithm is logarithmic in n, so the second one is faster.
Second thoughts: Fibonacci numbers grow exponentially, so we should consider the speed of actually adding and multiplying them. Adding is linear in the number of digits, so linear in n (because F(n) grows exponentially). Multiplication is quadratic in n! The memoization algorithm is repeated adding, so we get an extra linear factor, total complexity O(n^2). The repeated squaring uses multiplication, so we get an extra quadratic factor, total complexity O(n^2 log n). The first is faster!
Third thoughts: the fastest known algorithm for multiplying numbers is actually O(n log n) in the number of digits. We're down to O(n log n log n). The second is faster! That's probably terribly impractical, but there are apparently multiplication algorithms that are less impractical, with a smaller exponent than 2, so still an improvement over the first method.
But without specialized multiplication routines, the first is asymptotically faster.
Tangentially: look up the master theorem if you're interested in at least estimating growth rates for recurrence relations that crop up in computer science.
You can solve Fibonacci by writing it as
a0 + a1 n + a2 n^2 + ...
= a0 + a1 (n-1) + a2 (n-1)^2 + ...
+ a0 + a1 (n-2) + a2 (n-2)^2 + ...
Expand terms, then match the n, n^2, n^3 etc terms and solve for a0, a1, a2, etc. This works in solving differential equations too - it's called "Method of Frobenius" (easier in diffeq because you don't have to do annoying binomial coefficient math).
[1] Siebert, William McC.. Circuits, Signals, and Systems., McGraw-Hill, 1986.