-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib_constant_time.py
More file actions
65 lines (54 loc) · 1.29 KB
/
fib_constant_time.py
File metadata and controls
65 lines (54 loc) · 1.29 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from functools import cache
import time
import math
from typing import Optional
n=6
# def fib(x):
# if x<=1:
# return 1
# return fib(x-1)+fib(x-2)
# start_time= time.time()
# print(fib(n))
# end_time = time.time()
# recursion = start_time - end_time
# print("Execution Time:",recursion)
# 0 1 1 2 3 5 8 13 21 34 55 89
# 1 2 3 4 5 6 7 8 9 10 11 12
class Raj():
@cache
def fib(self, x):
if x<=1:
return 1
return self.fib(x-1)+self.fib(x-2)
ob=Raj()
start_time= time.time()
print(ob.fib(n))
end_time = time.time()
cached_recursion= start_time - end_time
print("Execution Time:",cached_recursion)
class Raj1():
def fib(self, n:int)->int:
memo={0:0,1:1}
def f(n):
if n in memo:
return memo[n]
memo[n]= f(n-1)+f(n-2)
return memo[n]
return f(n)
ob1=Raj1()
start_time= time.time()
print(ob1.fib(n))
end_time = time.time()
cached_recursion= start_time - end_time
print("Execution Time:",cached_recursion)
def fibo(n):
sqrt_5= math.sqrt(5)
f=(sqrt_5+1)/2
s=(sqrt_5-1)/2
ans= (f**n - s**n)//n
return ans
start_time= time.time()
print(fibo(n))
end_time = time.time()
cached_recursion= start_time - end_time
print("Execution Time:",cached_recursion)