Fast Fibonacci Transform | Brilliant Math & Science Wiki (2024)

Sign up with Facebook or Sign up manually

Already have an account? Log in here.

Agnishom Chattopadhyay, Satyabrata Dash, Pranjal Jain, and

  • Calvin Lin
  • Jimin Khim

contributed

Fibonacci series is a sequence of numbers where \(F(n)\) is computed by the summation of the previous two terms. In this wiki, we will be exploring various ways to compute Fibonacci numbers.

For those who do not remember what they are,

\[ F(n) = \left\{\begin{matrix}0 & n=0\\ 1 & n=1\\ F(n-2) + F(n-1) & \text{otherwise}.\end{matrix}\right. \]

Contents

  • Recursive Algorithm
  • Dynamic Programming
  • Matrix Exponentiation
  • Double Fibonacci Identities
  • Generalized Fast Fibonacci Transform

Recursive Algorithm

The most natural way to calculate the \(n^\text{th}\) Fibonacci number is to use the recursive definition itself.

1234567
def fib(n): if n==0: return 0 elif n==1: return 1 else: return fib(n-1)+fib(n-2)

When this algorithm is run, the program goes all the way down the Fibonacci tree to calculate \(F(n-1)\) and then again all the way down to calculate \(F(n-2)\). Clearly, there is a lot of re-computation going on.

Space Complexity
Note that to calculate \(F(n)\), we have to first calculate \(F(n-1)\) and to calculate \(F(n-1)\), we have to calculate \(F(n-2)\) and so on, all the way to \(F(1)\) and \(F(0)\). So, there are essentially \(n\) levels of recursion. The stack space complexity is hence \(O(n)\)

Time Complexity
Let \(T(n)\) be the time required to compute the \(n^\text{th}\) Fibonacci number.Clearly, \[T(n) = T(n-1) + T(n-2) ,\]which is just the Fibonacci recurrence! Hence, calculating the \(n\text{th}\) Fibonacci number takes time of the order \(F(n)\) or simply \(O(\phi^n).\)

Dynamic Programming

Is there a way to avoid recomputing the previous Fibonacci numbers over and over and to not overload the recursion stack as in the recursion algorithm? One way to do this is to use a bottom-up approach, i.e., to compute the Fibonacci numbers all the way from \(F(0)\) and \(F(1)\) to \(F(n-1)\) and \(F(n-2),\) and hence \(F(n).\)

12345
def fib(n): new, old = 1, 0 for i in xrange(n): new, old = old, new + old return old

Clearly, we are only storing at most \(2\) numbers at a time. So, we have a constant space complexity.

The time complexity is \(O(n)\), since we need to run the loop through \(n\) times.

Fast Fibonacci Transform | Brilliant Math & Science Wiki (1)

Consider the Fibonacci sequence, defined as follows:

Fibonacci(1) = 1Fibonacci(2) = 1Fibonacci(n) = Fibonacci(n - 2) + Fibonacci(n - 1)

The first two Fibonacci numbers are 1, 1. The following elements are computed by adding the prior two.

The first 6 Fibonacci numbers are: 1, 1, 2, 3, 5, 8.

Let F be the \(46^\text{th}\) Fibonacci number. What are the last 3 digits of F?

Note that the above problem is going to be very expensive with recursion.

Matrix Exponentiation

From this point onwards, we will use the sole properties of matrices to discover the fast computations of Fibonacci numbers.

\[ \begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^n = \begin{bmatrix}F_{n+1} & F_{n}\\ F_{n} & F_{n-1}\end{bmatrix} .\]

This theorem is trivial but nevertheless, we will prove it.

This statement is obviously true for \(n=1\) since

\[ \begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^1 = \begin{bmatrix}F_2 & F_1\\ F_1 & F_0\end{bmatrix}. \]

Assume that it also holds for some \(k\), then we have

\[ \begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^k = \begin{bmatrix}F_{k+1} & F_k\\ F_k & F_{k-1}\end{bmatrix}. \]

Now, multiplying both sides with \( \begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}, \) we have

\[ \begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^{k+1} = \begin{bmatrix}F_{k} + F_{k+1} &F_{k+1}\\ F_{k+1} & F_{k}\end{bmatrix}. \]

This implies

\[\begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^{k+1} = \begin{bmatrix}F_{k+2} &F_{k+1}\\ F_{k+1} & F_{k}\end{bmatrix} ,\]

which follows from the definition.

Hence, the identity holds for any \(n\). \(_\square\)

This algorithm is only as smart as the dynamic programming until it is implemented with exponentiation by squaring. To do this, we need to realize that \[ A^n = \left\{\begin{matrix}A\left(A^2\right)^\frac{n-1}{2} & \text{for odd } n\\ \left(A^2\right)^\frac{n}{2} & \text{for even } n.\end{matrix}\right. \]

Implemented that way, it takes \(O(\lg n)\) time to calculate \(F(n).\)

Double Fibonacci Identities

The following is a direct consequence of the matrix exponentiation algorithm that enables us to do the same thing with some lesser computations.

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

We have

\[ \begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^n = \begin{bmatrix}F_{n+1} & F_{n}\\ F_{n} & F_{n-1}\end{bmatrix}. \]

Squaring both sides,

\[\begin{align}\begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^{n} \begin{bmatrix}1 & 1\\ 1 & 0\end{bmatrix}^{n} &= \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 & 1\\ 1 & 0\end{bmatrix}^{2n} &= \begin{bmatrix} F_{n+1}^2 + F_{n}^2 & F_{n}F_{n+1} + F_{n}F_{n-1}\\F_{n}F_{n+1} + F_{n}F_{n-1} & F_{n}^2 + F_{n-1}^2\end{bmatrix} \\\begin{bmatrix}F_{2n+1} & F_{2n}\\ F_{2n} & F_{2n-1}\end{bmatrix} &= \begin{bmatrix} F_{n+1}^2 + F_{n}^2 & F_{n}F_{n+1} + F_{n}F_{n-1}\\F_{n}F_{n+1} + F_{n}F_{n-1} & F_{n}^2 + F_{n-1}^2\end{bmatrix} .\end{align}\]

Hence, we have

\[\begin{align}F_{2n} &= F_{n}F_{n+1} + F_{n}F_{n-1} \\F_{2n+1} &= F_{n+1}^2 + F_{n}^2.\end{align} \]

The first identity can be simplified a bit:

\[\begin{align}F_{2n} &= F_{n}F_{n+1} + F_{n}F_{n-1} \\ &= F_n ( F_{n+1} + F_{n-1} ) \\&= F_n ( F_{n+1} + F_{n+1} - F_{n} ) \\ &= F_n (2 F_{n+1} - F_{n} ) \\ \\\implies F_{2n} &= F_n (2 F_{n+1} - F_{n} ).\ _\square\end{align} \]

Generalized Fast Fibonacci Transform

This section requires a better explanation. If you have one, please add it and remove this flag.

We could extrapolate the above idea to recurrences of the form \[ f_{n} = x f_{n-1} + y f_{n-2} \] by using the following matrix form:

\[\begin{pmatrix} x & y \\ 1 & 0\end{pmatrix} \begin{pmatrix} f_{n-1}\\ f_{n-2}\end{pmatrix} = \begin{pmatrix} f_{n}\\ f_{n-1}\end{pmatrix}.\]

Cite as: Fast Fibonacci Transform. Brilliant.org. Retrieved from https://brilliant.org/wiki/fast-fibonacci-transform/

Fast Fibonacci Transform | Brilliant Math & Science Wiki (2024)

FAQs

How do you solve Fibonacci sequences fast? ›

Add the first term (1) and 0. This will give you the second number in the sequence. Remember, to find any given number in the Fibonacci sequence, you simply add the two previous numbers in the sequence. To create the sequence, you should think of 0 coming before 1 (the first term), so 1 + 0 = 1.

What is the Fibonacci sequence answer? ›

The Fibonacci sequence is a type series where each number is the sum of the two that precede it. It starts from 0 and 1 usually. The Fibonacci sequence is given by 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, and so on. The numbers in the Fibonacci sequence are also called Fibonacci numbers.

Whose real name is the first few Fibonacci numbers 0 1 2 3 5 8 these numbers are named after Fibonacci? ›

They are named after the Italian mathematician Leonardo of Pisa, also known as Fibonacci, who introduced the sequence to Western European mathematics in his 1202 book Liber Abaci.

How is Fibonacci used in real life? ›

These numbers are used in various fields such as architecture, art, space exploration, engineering, technology, and computing. The Fibonacci sequence, also known as the golden ratio, is utilized in architectural designs, creating aesthetically pleasing structures 1.

How do you solve for Fibonacci sequence? ›

What is the Formula for Generating the Fibonacci Sequence? The Fibonacci sequence formula deals with the Fibonacci sequence, finding its missing terms. The Fibonacci formula is given as, Fn = Fn-1 + Fn-2, where n > 1. It is used to generate a term of the sequence by adding its previous two terms.

What is the golden rule of the Fibonacci numbers? ›

The golden ratio, also known as the golden number, golden proportion, or the divine proportion, is a ratio between two numbers that equals approximately 1.618. Usually written as the Greek letter phi, it is strongly associated with the Fibonacci sequence, a series of numbers wherein each number is added to the last.

What is so special about Fibonacci numbers? ›

The sequence of numbers, starting with zero and one, is a steadily increasing series where each number is equal to the sum of the preceding two numbers. Some traders believe that the Fibonacci numbers and ratios created by the sequence play an important role in finance that traders can apply using technical analysis.

What is the math behind the Fibonacci sequence? ›

The Fibonacci sequence is the series of numbers where each number is the sum of the two preceding numbers. For example, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, …

What is Fibonacci's real name? ›

Leonardo Fibonacci, also known as Leonardo of Pisa or Leonardo Pisano, is better known as Fibonacci today. Fibonacci was an Italian-born mathematician. He was born around 1175 CE in Pisa, Italy. However, he grew up in Algeria.

What is a famous Fibonacci quote? ›

If by chance I have omitted anything more or less proper or necessary, I beg forgiveness, since there is no one who is without fault and circ*mspect in all matters.

What is the Rabbit problem Fibonacci? ›

Each pair is comprised of 1 male and 1 female and no rabbits die or leave the field. This is the classic rabbit problem Fibonacci used to generate the sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144… Ask students to work together in pairs and estimate the number of pairs they would have at the end of one year.

What is the golden ratio of DNA? ›

In addition, recently scientists have concluded that the height of one unit of the DNA helix shows the Golden ratio. The DNA molecule measures 34 angstroms long by 21 angstroms wide for each full cycle of its double helix spiral. These numbers, 34 and 21, are numbers in the Fibonacci series, and their ratio 1.6190.

What is the Fibonacci sequence in music? ›

The Fibonacci Sequence in Music

An octave on the piano consists of 13 notes: 8 white keys and 5 black keys. A scale consists of 8 notes, of which the 3rd and 5th notes make up a basic chord. In a scale, the dominant note is the 5th note, which is also the 8th note of all 13 notes that make up the octave.

Can math explain the universe? ›

These mathematical explorations are interesting and potentially relevant for physics: they may hold clues as to what secrets the Universe might have in store beyond what's presently known. But mathematics alone cannot teach us how the Universe works.

Why is recursive Fibonacci so slow? ›

Note the pattern: the number of recursive calls almost doubles each time n increases by one. So computing fibonacci(11) this way requires almost twice as much work as fibonacci(10) , and so on.

Which algorithm is best for the Fibonacci series? ›

Recursive Algorithm

Our first solution will implement recursion. This is probably the most intuitive approach, since the Fibonacci Sequence is, by definition, a recursive relation.

What is the best time complexity for the Fibonacci series? ›

The first term in Binet's Formula is also known as the golden ratio, typically denoted with the Greek letter ϕ. Thus, the complexity of fibonacci is O(Fn) = O(ϕn). This is approximately O(1.618n). Still awful, but a little better than the initial assumption of O(2n).

Are the Fibonacci numbers in fact 1 1 2 3 5 8 13 21 34? ›

Understanding the Fibonacci Sequence

The numbers in the Fibonacci Sequence don't equate to a specific formula, however, the numbers tend to have certain relationships with each other. Each number is equal to the sum of the preceding two numbers. For example, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377.

Top Articles
Latest Posts
Article information

Author: Roderick King

Last Updated:

Views: 6033

Rating: 4 / 5 (51 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Roderick King

Birthday: 1997-10-09

Address: 3782 Madge Knoll, East Dudley, MA 63913

Phone: +2521695290067

Job: Customer Sales Coordinator

Hobby: Gunsmithing, Embroidery, Parkour, Kitesurfing, Rock climbing, Sand art, Beekeeping

Introduction: My name is Roderick King, I am a cute, splendid, excited, perfect, gentle, funny, vivacious person who loves writing and wants to share my knowledge and understanding with you.