Home

 › 

Articles

 › 

Program for Fibonacci Numbers With Examples

program for fibonacci numbers

Program for Fibonacci Numbers With Examples

The Fibonacci sequence is undoubtedly one of the most famous mathematical sequences of all time. Mathematicians and scientists have been using this sequence for centuries in mathematics and science, naming it after the famed Middle Ages Italian mathematician, Fibonacci.

The Fibonacci sequence is a series of numbers where each subsequent number is the sum of the two preceding ones, starting from 0 and 1. The sequence is often denoted by Fn and looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, and so on. 

But what’s the big deal? Why should we care about the Fibonacci sequence? Well, it turns out that this sequence is not just some arbitrary series of numbers.

The Fibonacci sequence is found all over nature and in many areas of science, and it has important applications in computer science and programming. The series’ significance in computer science stems from the various algorithms that can generate its numbers.

Programming languages like Python have in-built functions to generate the Fibonacci sequence. If you’re interested in understanding the algorithms behind these functions, stick around, as this article will explain some 7 main algorithms and methods used to generate the Fibonacci numbers.

Different Methods of Creating Programs for Fibonacci Numbers

Multiple methods/approaches exist for generating Fibonacci numbers, each with varying time and space complexities. The most straightforward way to find the nth Fibonacci number is to use recursion.

However, this approach is not very efficient and can lead to a lot of redundant calculations. We’ll, therefore, explore recursion alongside other methods to see which one(s) can be considered the most efficient.

Recursion

Recursion is a method of solving a problem where the solution depends on the solutions to smaller instances of the same problem. In the context of the Fibonacci sequence, the recursive solution works by breaking down the problem of finding the nth Fibonacci number into two smaller problems: finding the (n-1)th Fibonacci number and finding the (n-2)th Fibonacci number.

This process continues recursively until the base case is reached, where n is either 0 or 1. The base case represents the condition that the function must satisfy to stop calling itself. Once met, the function simply returns n. For all other values of n, the function recursively calls itself, passing in n-1 and n-2 as arguments/parameters.

The following example shows a simple Python program that implements recursion to generate Fibonacci numbers:

def fibonacci(n):

    if n <= 1:  # base case

        return n

    else:

        return fibonacci(n-1) + fibonacci(n-2)

print(fibonacci(7)) # Output: 13

The function first checks for the base case upon which it returns n. If n is greater than 1, the function recursively calls itself with n-1 and n-2 as arguments and adds their return values together. This is because the n-th number in a Fibonacci sequence is equal to the sum of the (n-1)-th and (n-2)-th numbers in the sequence.

The recursive calls continue until the base case is reached and the function returns the n-th number in the sequence. We can obtain the time complexity of the recursive solution — it’s exponential being O(2n), as the function calls itself twice for each value of n.

This can quickly become inefficient as n grows larger, making this method impractical for larger values of n. The space complexity is O(n), which is linear, as the maximum depth of the recursion tree is n.

Iteration

Iteration is a more efficient approach to solving the Fibonacci sequence problem. The iterative approach involves solving a problem by repeatedly performing a set of instructions until a specific condition is met.

In the context of the Fibonacci sequence, the iterative solution works by starting with the first two Fibonacci numbers and computing the next Fibonacci number using the previous two. This process continues until the nth Fibonacci number is computed.

Virtualization software digital technology computer PC
The Fibonacci sequence first appeared in the Latin manuscript “Liber Abaci” (1202), where it was used to calculate the growth of rabbit populations.

©TippaPatt/Shutterstock.com

We can implement a simple program using iteration to create Fibonacci numbers in the following way:

def fibonacci(n):

    if n == 0:

        return 0

    elif n == 1:

        return 1

    else:

        fib1, fib2 = 0, 1

        for i in range(2, n+1):

            fib = fib1 + fib2

            fib1, fib2 = fib2, fib

        return fib2

Here, we are using a loop to compute the n-th Fibonacci number in linear time. We achieve this by starting with the first two numbers in the sequence (0 and 1) and iteratively computing the next number as the sum of the previous two.

We keep track of the previous two numbers in variables and update them at each iteration. After n iterations, we return the n-th number.

The time complexity of the iterative solution is O(n), which is linear. This is because the for loop runs n-1 times to compute the nth Fibonacci number. Since the function uses only a constant number of variables to compute the nth Fibonacci number, its space complexity is O(1), which is constant.

Memoization

Memoization is a method of solving a problem by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

In the context of the Fibonacci sequence, the memoization solution works by storing the solutions to subproblems and returning the cached result, if available.

In this way, the program calculates the solution to each subproblem only once and reuses it when needed.

Here’s an example of implementing memoization:

def fibonacci(n, memo={}):

    if n in memo:

        return memo[n]

    elif n <= 1:

        return n

    else:

        memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)

        return memo[n]

This example uses a top-down approach to compute the n-th Fibonacci number by caching previously computed values to avoid redundant computations. The function first checks if it has already computed and cached the value of the n-th Fibonacci number in a Python dictionary. If it has, it returns the cached value. 

If it hasn’t, it computes the value recursively as the sum of the (n-1)-th and (n-2)-th Fibonacci numbers, caches the computed value in the dictionary, and returns it.

The time complexity of the memoization solution is O(n), which is linear. This is because the program computes each Fibonacci number only once and stores it in the memo dictionary for future use. The space complexity is also O(n), which is linear, as the memo dictionary stores solutions to subproblems up to n.

Dynamic Programming

Dynamic programming is a method of solving a problem by breaking it down into smaller subproblems and storing the solutions to these subproblems to avoid redundant computations.

In the context of the Fibonacci sequence, the dynamic programming solution works by computing the Fibonacci numbers in a bottom-up manner and storing the solutions in an array. This way, the solution to each subproblem is only computed once and reused when needed.

Consider the following example:

def fibonacci(n):

    fib = [0, 1]

    for i in range(2, n+1):

        fib.append(fib[i-1] + fib[i-2])

    return fib[n]

The function uses a Python list to store the computed values of the sequence, starting with the first two values. Then, it iterates from 2 to n, computing the i-th value as the sum of the (i-1)-th and (i-2)-th values and storing it in the list. Finally, it returns the n-th value in the list.

The time complexity of the dynamic programming solution is O(n), which is linear. The time complexity of the algorithm is O(n) because the for loop computes the nth Fibonacci number by running n-1 times, and each computation takes constant time.

Moreover, the space complexity of the algorithm is O(n) because it uses a list of size n to store the solutions to subproblems.

Matrix Exponentiation

Matrix exponentiation is a method of solving a problem by raising a matrix to a power. In the context of the Fibonacci sequence, the matrix exponentiation solution works by using a matrix with Fibonacci numbers to raise it to the power of n-1 to get the nth Fibonacci number.

e can use Matrix Exponentiation to create a Python program for Fibonacci numbers, as shown:

import numpy as np

def fibonacci(n):

    F = np.array([[1, 1], [1, 0]])

    return np.linalg.matrix_power(F, n-1)[0, 0]

In this example, we’re using a matrix exponentiation technique to compute the n-th Fibonacci number in logarithmic time. To accomplish this, we raise a 2×2 matrix to the power of n-1 and multiply it with the initial state vector [0, 1]. The resulting matrix contains the n-th and (n+1)-th Fibonacci numbers, and we return the n-th number.

The time complexity of the matrix exponentiation solution is O(log n), which is, of course, logarithmic. The divide-and-conquer algorithm used in matrix multiplication reduces the number of multiplications required. Therefore, the space complexity is O(1), which is constant, as only a constant amount of memory is used.

Binet’s Formula

Binet’s formula is a direct formula to compute the nth Fibonacci number without having to compute all the previous Fibonacci numbers. 

The formula is often written as: Fn = round((Φ^n – (1-Φ)^n) / √5), where Φ (phi) is the golden ratio.

What is dynamic programming?
Binet’s formula suggests that the ratio of two consecutive Fibonacci numbers tends to the golden ratio as n increases.

©TippaPatt/Shutterstock.com

In the context of the Fibonacci sequence, Binet’s formula works by using the golden ratio, which is (1 + √5) / 2, and its conjugate, which is 1 – √5) / 2.

An example in Python would be:

def fibonacci(n):

    golden_ratio = (1 + 5**0.5) / 2

    return round((golden_ratio**n – (1 – golden_ratio)**n) / 5**0.5)

The function first defines the golden ratio. Next, we calculate the n-th term using Binet’s formula and return the rounded integer value of the result.

This implementation of Binet’s formula is computationally efficient because it requires only a few simple arithmetic operations to calculate any term of the Fibonacci sequence, without the need to calculate all the preceding terms.

This is proven by the time complexity of Binet’s formula is O(1), which is constant. This is because the formula only requires a few arithmetic operations to compute the nth Fibonacci number. The space complexity is O(1), which is constant, because the program uses only a few variables to compute the nth Fibonacci number.

Greedy Algorithm

The greedy algorithm is a technique used to solve optimization problems. It works by making the best decision at each step and hoping that the decisions made will lead to an optimal solution. This algorithm follows the “greedy” strategy of always choosing the largest possible number at each step.

One can generate Fibonacci numbers using this approach by starting with the two base cases and then iterating through the series to find the next number.

First, save the two preceding numbers in two variables and then add them to get the next Fibonacci number. This process continues until the n-th number in the sequence is generated.

We can implement a program for Fibonacci numbers using the Greedy algorithm in a simple way, as follows:

def fibonacci(n):

    if n <= 1:

        return n

    prev, curr = 0, 1

    for i in range(2, n+1):

        prev, curr = curr, prev + curr

    return curr

We first check if n satisfies the base case, in which case we returned the appropriate value. Otherwise, we create a list to store the previous two Fibonacci numbers (0 and 1), and then use a loop to generate the next numbers in the sequence by adding the last two numbers in the list. The loop continues until the n-th number in the sequence is generated, at which point we return it.

The time complexity of the Greedy Algorithm is O(n), which is linear, as we need to iterate n times to generate the n-th number in the sequence. The space complexity is also O(n), as we need to store the previous two numbers in the sequence for each iteration of the loop. 

However, we could optimize the space complexity by only storing the previous two numbers instead of all the numbers generated so far.

Rounding Up

The Fibonacci sequence is an important and timeless mathematical concept with numerous applications in computer science and programming. With the right algorithm, you can quickly and easily generate any Fibonacci number. 

Generally speaking, iteration and dynamic programming are the most efficient algorithms in terms of time and space complexity, while matrix exponentiation is the most efficient in terms of time complexity for larger values of n. Recursion is the most intuitive but also the least efficient in terms of time complexity and space complexity.

In the end, the best algorithm to use depends on the problem at hand. It’s, therefore, important to understand them to know when to use each method for optimizing the performance of your Fibonacci programs.

Frequently Asked Questions

Archimedes Spiral, Golden Ratio, Fibonacci Sequence: What’s the connection?

The Archimedes spiral and the Fibonacci sequence can be related through the golden ratio.

The golden ratio is a mathematical constant that is equal to the ratio of two numbers in the Fibonacci sequence. This ratio can also be found in the shape of the Archimedes Spiral.

Therefore, the three concepts are interrelated and have important applications in mathematics, science, and art.

What is the difference between recursion and iteration when generating Fibonacci numbers?

Recursion is a technique in which a function calls itself to solve a problem, while iteration is a looping structure that repeats a set of instructions until a certain condition is met.

Recursion is the most straightforward approach for generating Fibonacci numbers, but it is inefficient due to its exponential time complexity. Iteration is more efficient and can compute the n-th number in linear time.

How is the Fibonacci sequence applied to computer science and programming?

The Fibonacci sequence has many applications, such as in algorithms for generating random numbers, graphs, trees, sorting algorithms, and data compression. Additionally, the Fibonacci sequence is used in cryptography and in analyzing the performance of algorithms.

What is the significance of the Fibonacci sequence?

It is often used to model growth patterns in nature, such as the branching of trees, the arrangement of leaves on a stem, and the spirals in a seashell or pinecone. It is also used in technical analysis to predict market trends and in cryptography to generate random numbers.

What are some examples of Fibonacci numbers in nature and art?

The golden ratio is found in many natural phenomena such as the spirals in certain plants and animals (e.g., pineapples, sunflowers, shells, etc.).

It is also found in certain structures such as the Parthenon in Athens, Greece, music such as Beethoven’s Fifth Symphony, art such as Leonardo da Vinci’s Mona Lisa, and astronomy such as the orbits of planets around stars.

To top