Fibonacci Numbers: A Simple Python Program for Calculation and Explanation

Introduction

The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones. It is a mathematical concept that has fascinated mathematicians for centuries and has found applications in various fields such as computer science, biology, and finance. In this article, we will develop a simple Python program to calculate Fibonacci numbers and provide a detailed explanation of the underlying principles.

Understanding the Fibonacci Sequence

The Fibonacci sequence starts with 0 and 1, and each subsequent number is the sum of the two preceding ones. Mathematically, it can be defined as:

F(0) = 0 F(1) = 1 F(n) = F(n-1) + F(n-2) for n > 1

Here is a simple recursive function in Python that calculates the nth Fibonacci number:

def fibonacci(n):
    if n <= 0:
        return 0
    elif n == 1:
        return 1
    else:
        return fibonacci(n-1) + fibonacci(n-2)

Let's test this function by calculating the 10th Fibonacci number:

print(fibonacci(10))

The output will be 55, as the 10th Fibonacci number is 55.

An Iterative Approach

While the recursive approach is simple and intuitive, it has performance issues for large values of n. The function makes redundant calculations and has an exponential time complexity. To overcome this, we can use an iterative approach that calculates the Fibonacci numbers in a loop.

Here is an iterative Python function to calculate the nth Fibonacci number:

def fibonacci_iterative(n):
    if n <= 0:
        return 0
    elif n == 1:
        return 1

    previous = 0
    current = 1
    for i in range(2, n+1):
        temp = previous + current
        previous = current
        current = temp

    return current

Using this function, let's calculate the 20th Fibonacci number:

print(fibonacci_iterative(20))

The output will be 6765, as the 20th Fibonacci number is 6765.

Visualizing the Fibonacci Sequence

To better understand the Fibonacci sequence, let's visualize it using a graph. The following ER diagram shows the relationship between consecutive Fibonacci numbers:

erDiagram
    F(0) ||--o{ F(1) : "F(n)" as "n > 1"
    F(1) ||--o{ F(2) : "F(n)" as "n > 1"
    F(2) ||--o{ F(3) : "F(n)" as "n > 1"
    F(3) ||--o{ F(4) : "F(n)" as "n > 1"
    F(4) ||--o{ F(5) : "F(n)" as "n > 1"
    F(5) ||--o{ F(6) : "F(n)" as "n > 1"
    F(6) ||--o{ F(7) : "F(n)" as "n > 1"
    F(7) ||--o{ F(8) : "F(n)" as "n > 1"

This diagram depicts the relationship between consecutive Fibonacci numbers, where each number is connected to its two preceding numbers.

Analyzing the Time Complexity

The time complexity of the recursive Fibonacci function is exponential. To see this, let's consider the number of function calls required to calculate F(n):

  • F(n) requires F(n-1) and F(n-2)
  • F(n-1) requires F(n-2) and F(n-3)
  • ...
  • F(2) requires F(1) and F(0)

In total, we have 2^n function calls, resulting in exponential time complexity.

On the other hand, the iterative Fibonacci function has a linear time complexity of O(n) since it only requires a single loop to calculate the nth Fibonacci number.

Using a Gantt Chart

Let's create a Gantt chart to visualize the time complexity of both approaches:

gantt
    title Fibonacci Time Complexity

    section Recursive Approach
    Calculation :a1, 1, 1
    Total Time :a2, 1, 1

    section Iterative Approach
    Calculation :a3, 1, 2
    Total Time :a4, 2, 2

This Gantt chart shows that the recursive approach has a longer calculation time compared to the iterative approach.

Conclusion

In this article, we developed a simple Python program to calculate Fibonacci numbers and explored the underlying principles. We started with a recursive approach and explained its time complexity issues. To overcome these issues, we introduced an iterative approach that performs calculations in a loop. We also visualized the Fibonacci sequence using an ER diagram and analyzed the time complexity using a Gantt chart.

The Fibonacci sequence is a fascinating concept that has practical applications in various fields. By understanding its principles