Using the Fibonacci sequence's relation to the golden ratio to created a closed-form solution.
A Different Approach
The classic lesson in recursion for new programmers is finding the nth value of the Fibonacci sequence. It is a nice way to teach the basics of recursion since the Fibonacci sequence should be familiar to most people and the idea of summing the two previous elements in the sequence is simple enough to grasp. However, if you want to calculate Fn for some large n, or you have some value m and want to find n such that Fn=m, there is a more efficient closed-form solution.
Relation to the Golden Ratio
Consider the first few terms of the Fibonacci sequence: 0,1,1,2,3,5,8,13,…. Skipping the base cases F1=0,F2=1 and taking the ratio between consecutive terms we get the ratios (Fn−1Fn)n=3∞=(1,2,1.5,35,1.6,1.65,…). At a glance there seems to be a pattern here, in fact this sequence will quickly converge upon the golden ratio.
Viewed graphically, the red dots are values of the Fibonacci sequence, blue dots are the values in our sequence of ratios, and the dashed orange line is y=21+5 — the true golden ratio. Visually it appears that this ratio converges very quickly to the golden ratio. Now that we have motivated the connection between these two sequences, we will use linear algebra to derive a closed-form solution to the value of any Fn.
Deriving the Closed-Form Solution
Representing the Sequence as a Linear Operator
We can treat the Fibonacci sequence as linear operator T on R2. We will define this operator as T(a,b)=(b,a+b). First we will use induction to show that Tn(0,1)=(Fn,Fn+1) for any n∈N. For our base case, n=1, by definition T(0,1)=(1,1)=(F1,F2)=(Fn,Fn+1). Now assume that this holds for all n<k.
Given some eigenvalue λ and its respective eigenvector v, we know Tv−λv=0⟹Tv=λv⟹(b,a+b)=(λa,λb). This gives us the following equations:
b=λaa+b=λb
Substituting (1) into (2) gives a+λa=λ2a⟹(λ2−λ−1)a=0. Clearly a cannot be zero since that would imply b is also zero, but our eigenvector (a,b) cannot be zero. Thus the quadratic given by λ2−λ−1 must be zero. Apply the quadratic equation to solve for the roots:
λ=21±12−4(1)(−1)=21±5
Recall that the formula for the golden ratio is ϕ=21+5, thus the golden ratio is one of the eigenvalues for T. Our second eigenvalue, 21−5, is the conjugate of the golden ratio and we will denote it as ψ.
Creating a Basis
Now we will find a basis for R2 consisting of eigenvectors of T. We know we have the eigenvalues ϕ and ψ. Let v=(a,b) be an eigenvector of R2 corresponding to the eigenvalue ϕ such that Tv=(b,a+b)=ϕv=(ϕa,ϕb). This implies the following relations:
b=ϕaa+b=ϕb
Substituting (3) into (4) we get a+ϕa=ϕ2a. However, ϕ2=1+ϕ. Using this to simplify, a+ϕa=(1+ϕ)a=a+ϕa. Thus a solution is a=1 from which (3) gives us b=ϕ, thus our eigenvector is (1,ϕ). A similar process gives us the eigenvector (1,ψ) for the eigenvalue ψ. In combination, (1,ϕ),(1,ψ) is a basis for R2 consisting of eigenvectors of T.
Using This Basis to Compute Tn(0,1)
Let c1,c2∈R such that (0,1)=c1v1+c2v2 where v1=(1,ϕ) and v2=(1,ψ).
Recall that earlier we proved that Tn(0,1)=(Fn,Fn+1). Comparing this to the equation above we arrive at Fn=51(ϕn−ψn)=51[(21+5)n−(21−5)n]. This is called Binet’s formula. Note that we are only interested in values where n≥0 and for these values 5ψn<21. In fact, this error is practically nonexistent by the time we reach n=8 (<0.01). Armed with this, we can further simplify the formula by simply rounding the ϕ portion to the nearest integer. In its final form, we achieve the following closed-form expression for the Fibonacci sequence for n≥0:
Fn=⌊5ϕn⌉
Bonus: Getting the Index of a Fibonacci Value
Our closed-form solution can be easily reversed (with some slight hand-waving where we temporarily drop the rounding function):