-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathfibonacci.py
48 lines (37 loc) · 1.43 KB
/
fibonacci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""
Find the nth item of a fibonacci series
---------------------------------------
Fibonacci Series is a series in which a number in a series is equal to the sum
of last 2 numbers in the series.
An example of a fibonacci series is 1, 1, 2, 3, 5, 8, 13, 21, 34...
Nth item of fibonacci series can be solved both using dynamic programming and
recursion however recursive functions will be much slower for higher value of n
so it is recommended to use dynamic programming to find it out.
"""
def fibonacci_recursion(n: int) -> int:
if n < 1:
raise ValueError("Negative number is not acceptable")
if n < 3:
return 1
return fibonacci_recursion(n - 1) + fibonacci_recursion(n - 2)
def fibonacci(n: int):
if n < 1:
raise ValueError("Negative number is not acceptable")
if n < 3:
return 1
a, b = 1, 1
for n in range(3, n + 1):
a, b = b, a + b
return b
if __name__ == "__main__":
print(fibonacci_recursion(8)) # 21
print(fibonacci_recursion(9)) # 34
print(fibonacci_recursion(10)) # 55
# It takes too long to find out the number for higher value of N
# and eventually maximum depth of recursion will be exceeded in such case
# print(fibonacci_recursion(40)) # 102334155 (very slow)
print(fibonacci(8)) # 21
print(fibonacci(9)) # 34
print(fibonacci(10)) # 55
print(fibonacci(40)) # 102334155
print(fibonacci(100)) # 354224848179261915075