Exploring Recursion: Python Programs to Display the Fibonacci Sequence

Rate this post

πŸ’‘ Problem Formulation: This article addresses the challenge of writing a Python program to display the Fibonacci sequence using recursion. This involves a function that calls itself to calculate the next number in the sequence until a certain condition is met. The input is an integer n representing the number of Fibonacci numbers to display and the output is the sequence itself; for example, if n=5, the output should be 0, 1, 1, 2, 3.

Method 1: Basic Recursive Function

This method involves defining a recursive function that calculates the nth Fibonacci number. The function calls itself with decremented values n-1 and n-2 as arguments, until it reaches the base cases of 0 or 1 where it returns n directly.

Here’s an example:

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

nterms = int(input("How many terms? "))

if nterms <= 0:
   print("Please enter a positive integer")
else:
   print("Fibonacci sequence:")
   for i in range(nterms):
       print(fibonacci(i))

Output for nterms=5:

0
1
1
2
3

This code snippet starts by defining a function fibonacci. The function uses a simple conditional to check if the input n is 0 or 1 and returns these directly since these are the first two numbers of the Fibonacci sequence. For any other value of n, it sums the results of the function called with n-1 and n-2.

Method 2: Memoization

Memoization is an optimization technique that stores the results of expensive function calls and returns the cached result when the same inputs occur again. In this approach, a dictionary stores previously-computed Fibonacci numbers and returns them when they are needed again.

Here’s an example:

memo = {0: 0, 1: 1}
def fibonacci(n):
    if n not in memo:
        memo[n] = fibonacci(n-1) + fibonacci(n-2)
    return memo[n]

nterms = int(input("How many terms? "))

if nterms <= 0:
    print("Please enter a positive integer")
else:
    print("Fibonacci sequence:")
    for i in range(nterms):
        print(fibonacci(i))

Output for nterms=5:

0
1
1
2
3

In this improved version, a dictionary called memo is used to store the result of each fibonacci call, greatly reducing the number of recursive calls needed. The fibonacci function first checks if the result is already in the dictionary to avoid unnecessary computation.

Method 3: Using functools LRU Cache

The functools module in Python offers a decorator that implements memoization in a single line. The lru_cache decorator can be directly applied to the function, avoiding the explicit handling of the memoization process.

Here’s an example:

from functools import lru_cache

@lru_cache(maxsize=None)
def fibonacci(n):
    if n < 2:
        return n
    return fibonacci(n-1) + fibonacci(n-2)

nterms = int(input("How many terms? "))

if nterms <= 0:
    print("Please enter a positive integer")
else:
    print("Fibonacci sequence:")
    for i in range(nterms):
        print(fibonacci(i))

Output for nterms=5:

0
1
1
2
3

Using lru_cache decorator from functools is a clean and efficient way to add memoization to the fibonacci function. Simply adding the @lru_cache line automatically takes care of storing function calls, which makes the code shorter and faster.

Method 4: Recursive Generator

Python generators are a simple way of creating iterators. A generator for the Fibonacci sequence can be written using recursion. It uses the yield keyword to generate Fibonacci numbers one by one, which preserves memory compared to storing the entire sequence in a list.

Here’s an example:

def fibonacci(n, a=0, b=1):
    if n == 0:
        return
    else:
        yield a
        yield from fibonacci(n-1, b, a+b)

nterms = int(input("How many terms? "))

if nterms <= 0:
    print("Please enter a positive integer")
else:
    print("Fibonacci sequence:")
    for number in fibonacci(nterms):
        print(number)

Output for nterms=5:

0
1
1
2
3

This approach uses a recursive generator function, fibonacci, that yields the first number of the sequence and then delegates to itself (with yield from) for the rest of the sequence, decreasing n each time and keeping track of the last two Fibonacci numbers.

Bonus One-Liner Method 5: Recursive Lambda with Memoization

A combination of recursion, lambdas, and memoization offers a concise and sophisticated one-liner approach to compute Fibonacci sequence.

Here’s an example:

fibonacci = (lambda f, _fib_cache={}: lambda n: _fib_cache.setdefault(n, f(f)(n-1) + f(f)(n-2)) if n > 1 else n)(lambda f: lambda n: f(f)(_fib_cache)(n))

nterms = int(input("How many terms? "))

if nterms <= 0:
    print("Please enter a positive integer")
else:
    print("Fibonacci sequence:")
    for i in range(nterms):
        print(fibonacci(i))

Output for nterms=5:

0
1
1
2
3

This one-liner defines a recursive lambda function that computes Fibonacci numbers and implicitly maintains a cache to reduce computational cost. Although it may be less readable, it demonstrates the power and flexibility of Python’s lambda functions and decorators.

Summary/Discussion

  • Method 1: Basic Recursive Function. It’s simple to understand and implement. However, it has exponential time complexity, which can cause significant slowdowns for larger values of n.
  • Method 2: Memoization. It optimizes the basic recursive method by remembering previous results. It’s much faster than the basic method but requires more memory to store the results.
  • Method 3: Using functools LRU Cache. This provides an easy-to-use memoization technique that is clean and efficient, but it utilizes extra features of Python not always familiar to beginners.
  • Method 4: Recursive Generator. This method is memory-efficient and elegant. However, it might be less intuitive for those not familiar with generators.
  • Method 5: Recursive Lambda with Memoization. This showcases advanced Python features and is efficient, but it sacrifices readability, which is significant in collaborative coding environments.