Group A
Fibonacci numbers and analyze their time and space complexity.
Objective of the Assignment: Students should be able to perform non-recursive and recursive
programs o calculate Fibonacci numbers and analyze their time and space complexity.
Prerequi
I
Basie of Python or Java Programming
Concept of Recursive and Non-recursive functions
Execution flow of calculate Fibonacci numbers
Basic of Time and Space complexity
Contents for Theory:
1
2
Introduction to Fibonacci numbers
Time and Space complexity
BIHARAT! VIDY APEETIES COLLEGE OF ENGINFLRING LAVALE,PUNLsion to Fi
pre!
Pere Fibonacci series hamea
non as Fibonacei, is a se
| ibonacei sequence are given as: 0,1.»
Wo terms, starting fo
‘ences, the term ‘0’ might be omitted.
Gand t |
as first and Second 17M. In ome Ok ef,
hat is the Fibonacei Seri
¢ Fibonacci series is the sequence
The ths seavence oF numbers (aso eae’ Fibonacel ‘pumbers), where
ow “EWse numbers, such thatthe first two terms are and
+ Insome older versions of the serie,
he term '0'm
I Boot A Fon sere cn ts |
be given as 0. Te 1 W can be thus be observed tha
ceakeuluted by adding the two
3.21, 34,
every term ean be |
ms before i
+ Given the frst term, FO and second term. Fl ag ind “I. the third term here can be given as, |
21038 7°S peotlel
eu
Similarly.
sos Similarly
uossed Jo ae1+1
PLE
* Fas2+1=3
pans
Nd OZ Given a number n, print n-th Fibonacci Number, |
lasqou
KEtires
i Sequene
pibonace
terete sequence is defined ws
re kishofl partis FO=0 and F1=)
ne roursive EBON PATS Fa Fast
ris noted tat the sequence starts with o
‘ather than 1. So, FS shouldbe the 6th erm ofthe sequence
examples:
=2
Inputs
Output! ‘
Input |
wow | Output: 4
The list of Fibonacci numbers are calculated as follow
Fa
o
0
1
1
2 1
3 2
4 3
5 5a“ |
6 : |
7
13 |
8 |
2 i
7 |
uM |
_.o and $0 01
|
gethod 4 (Use Non-recursion)
assinple method that is a direct recursive implementation of mathematical recurrence relation is |
given above. 1
ri, wellstore Oand 1 in FO] and £11], respectively.
Next, we'll iterate through array positions 2 t0 n-J. At each position i, we store the sum of the two
preceding aay values in FU]
Finally. we eturn the value of [7-1], giving us the number at position m in the sequence.
Here’sa visual representation of this process:Stan F(n)
|
|
Stop F(n)
# Programto display the Fibonacei sequence up to n-th term
aterms = int(input("How many terms? "))
# fit two terms
al.w2=0,1
count =0
# check if the number of terms is valid
if nterms <= 0:
print("Fibonacci sequence upto" nterms.
print(n})
# generate fibonacci sequencegn Hpac sequence")
hile count mers
printint)
nth= ni¢nd
s upsate values
a=
m= nth
count += 1
output
ow many terms? 7
ators sequence:
0
Time and Space Complexity of Space Optimized Method
of two terms
ir. We have to find the
1 The time complexity of the Fibonacei series is TN) ie, lim
and itis repeated n times depending on the value of n.
+ The space complexity of the Fibonacci series using dynamie programming is O(1)
Time Complexity and Space Complexity of Dynamic Programming
inear. We have to find the sum of two terms and it
' The time complexity of the above code is T(N) i.
istepeated n times depending on the value of,the space complenity of the above code is O(N),
method 2 (Use Recursion)
pes Sart by defining F(n) as the function that returns the value of Fn.
qo evaluate FU) for > 1, We can reduce our problem into two smaller problems of the
di Fel) and F(n-2), We can further reduce Furl) and F(n-2) to (ned) and
1).2h and M(1-2)-1) and M((n-2)-2), respect
same
Fr
fwe repeat this reduction, we'll eventually reach our known base eases and, thereby, obtain
solution 10 F().
Employing this logic. our algorithm for F(2) will have two steps:
heck ifs 1. Iso, return n.
Check ifm > 1. Iso, call our function F with inputs n-1 and n-2, and return the sum of the
wo results.
ation of this algorithm:
vga visual repre
Start F(n) =
feed
back
results
isnect?
Stop F(n)python program to display the Fibonace sequence
‘else:
retur(recur_fibo(n-1) + recur_fibo(n-2))
merms * 7
# check if the number of terms is valid
itnterms <=
prin(*Plese enter a positive integer")
else:
ibonacci sequence:")
print(
for iin range(nterms):
print(recur_fibo(i))
Output
Fibonacci seque:
0
Time and Space Complexity
* The time complexity of the above code is T(24N
exponential,1 The Space complenity of the above code is O(N) for a recursive series,
Method ‘Time complexity Space com
Using recursion Von) = Vint) + Tn-2) O(n)
Using DP L O(n) ow)
Space optimization of DP O(n) ou)
Using the power of matrix O(n) on)
method
Optimized matrix method O(log n) O(log n)
Recursive method in O(log n) O(log n) O(n)
time
Using direct formula O(log n) ou)
DP using memoization O(n) ou)
Applications of Fibonacci Series
The Fibonacci series finds application in different fields in our day-to-day lives. The different
pattems found in a varied number of fields from nature, to music, and to the human body follow the
Fitonacci series. Some of the applications of the series are given as,
+ It is used in the grouping of numbers and used to study different other special mathematical
sequences.
+ It finds application in Coding (computer algorithms, distributed systems, ete). For example.
Fibonacci series are important in the computational run-time analysis of Euel algorithm, used for
determining the GCF of two inte;
+ Itis applied in numerous fields of science like quantum mechanics, eryptography, ete.
' In finance market trading, Fibonacci retracement levels are widely used in technical analysis.
Conclusion- In this way we have explored Concept of Fibonacei series using recursive and non
recursive method and also learn time and space complexityPs
assignment Question
1, What is the Fibonacci Sequence of numbers?
2, How do the
mnacci work?
43, What is the Golden Ratio?
4
‘What is the Fibonacei Search technique?
, What is the real application for Fibonacci series
Reference link