Fibonacci Series Assignment
Fibonacci Series Assignment
Name:
Umm e Aimen
Submitted To:
Prof Danish Kaleem
Roll No:
CS-21-20
Fibonacci Series:-
The Fibonacci series is a sequence of numbers in
which each number is the sum of the two preceding
numbers. The series starts with 0 and 1, and the next
number in the sequence is the sum of the previous two
numbers.
The first few numbers in the Fibonacci series are:0, 1, 1,
2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
Python Program:-
Def Fibonacci (n)
N1,n2=0,1
If n<=0:
Print (“enter number greater than 0”)
Else
Print(Fibonacci (n-1)+ Fibonacci (n-2))
Complexity Analysis:-
Worst Case:-
The worst-case scenario for this
implementation is when we call the Fibonacci()
function with a large value of n. Since this
implementation uses recursion, the function will keep
calling itself until it reaches the base cases of n=0 and
n=1. This can result in a large number of function calls
and can be very slow, making the worst-case time
complexity exponential, O(2^n), because the number
of function calls grows exponentially with n.
Best Case:-
In the best case , when n is 0 or 1, the
function returns the value of n itself without making
any recursive calls. This makes the best-case time
complexity constant, O(1)
Average Case:-
The average-case time complexity of
generating the Fibonacci sequence using a recursive
function is also exponential, which means that the
function runs in a reasonable amount of time for small
input sizes but becomes slow for large input sizes. The
average-case time complexity can be expressed as
O(2^n) as well because the function generates the same
values multiple times, leading to redundant
computations.