This project contains a Python script to compute and visualize the Fibonacci sequence using recursion, memoization, and the matplotlib
library for plotting. The code uses an efficient caching mechanism to store previously calculated Fibonacci numbers, speeding up the computation of higher values in the sequence.
- Fibonacci Sequence Calculation: Computes the Fibonacci sequence up to the specified
n
. - Memoization: Uses the
lru_cache
decorator from Python'sfunctools
module to cache computed Fibonacci numbers, improving performance. - Visualization: Plots the sequence using
matplotlib
to illustrate the growth of Fibonacci numbers with increasingn
.
- Python 3.x
matplotlib
library: Install withpip install matplotlib
if not already installed.
-
Clone this repository or copy the script to your local machine.
-
Ensure
matplotlib
is installed in your Python environment. -
Run the script with:
python main.py
-
The program will display a plot showing the Fibonacci numbers for the first 10 values of
n
.
- Caching with
@lru_cache
: This decorator stores results of previous function calls, reducing the need for repeated calculations. Here,@lru_cache(maxsize=1000)
caches up to 1000 results of thefibonacci
function. - Recursive Function: The
fibonacci(n)
function uses recursion to calculate then
th Fibonacci number:- If
n
is 1 or 2, the result is 1 (base cases). - Otherwise, the result is the sum of the previous two Fibonacci numbers.
- If
- Input Validation: Ensures
n
is a positive integer to prevent calculation errors. - Plotting with
matplotlib
: Generates a line plot showing Fibonacci values against the sequence indexn
.
The script plots the Fibonacci sequence values from n = 1
to n = 10
, displaying the familiar exponential growth of the sequence.