-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFibonacci_Number.py
42 lines (30 loc) · 1.42 KB
/
Fibonacci_Number.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
from functools import lru_cache
print("\n---------- Fibonacci Number Calculator ----------\n")
while True:
try:
# Number of terms
n = int(input("Enter nth term ()\n(Note: Term count starts at 0): "))
if n < 0:
print("Input must be a positive integer!\n")
else:
break
except:
print("Invalid input! Try again.\n")
@lru_cache(maxsize=None) # Optimizes the recursion implementation
# Recursive function:
def f(n):
# Base case 1:
if n == 1:
return 0 # Returns the 1st term of the sequence if number of terms is 1
# Base case 2:
elif n == 2:
return 1 # Returns the 2nd term of the sequence if number of terms is 2
# Recursive case:
elif n > 2:
# Calculates & returns the next terms
# of the sequence using a recursive call:
return f(n - 1) + f(n - 2)
print("--------------------------------------------------")
# Output
print("f_", n, " = ", f(n), sep='')
print("--------------------------------------------------\n")