-
Notifications
You must be signed in to change notification settings - Fork 1
/
FibonacciRec.py
38 lines (36 loc) · 1.08 KB
/
FibonacciRec.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
class FibonacciRec:
def fib(n):
if(n <=0):
return 0
elif(n==1):
return 1
else:
return FibonacciRec.fib(n-1) + FibonacciRec.fib(n-2)
def fibRec( n, prev, curr):
if(n==0):
return curr
else:
prev,curr = curr,curr+prev
return FibonacciRec.fibRec(n-1, prev,curr)
def fibMemo(n, memo):
if(n<=0):
return 0
elif(n==1):
return 1
elif(not memo[n]):
memo[n] = FibonacciRec.fibMemo(n-1, memo) + FibonacciRec.fibMemo(n-2, memo)
return memo[n]
def fibTail(n):
prev, curr = 0,1
return FibonacciRec.fibRec(n-1, prev, curr)
def fibIterative(n):
fibPrev,fib = 0,1
for index in range(1,n):
fibPrev,fib = fib,fib+fibPrev
return fib
if __name__ == '__main__':
print(FibonacciRec.fib(10))
print(FibonacciRec.fibIterative(10))
memo = [0] * 11
print(FibonacciRec.fibMemo(10, memo))
print(FibonacciRec.fibTail(10))