categories: computer science
Hello everyone,
Below you will find a video with a detailed explanation of the algorithm for finding the n-th term of the Fibonacci sequence. Its operation, time complexity, and usefulness. Below you will also find ready-made code to copy if needed.
Code:
#method 1
#1, 1, 2, 3, 5, 8, 13, ...
def fib1(b):
if b <= 2:
return 1
else:
return fib1(b-1) + fib1(b-2)
print(fib1(4))
#method 2
tablicaFib = [1, 1]
def fib2(b):
global tablicaFib
if b <= 2:
return 1
else:
for i in range(2, b):
tablicaFib.append(tablicaFib[i-1] + tablicaFib[i-2])
fib2(4)
print(tablicaFib[4-1])
Video:
Thank you for reading!
Read more