-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq3.py
More file actions
60 lines (56 loc) · 1.49 KB
/
q3.py
File metadata and controls
60 lines (56 loc) · 1.49 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
import time
print()
def fib_iter(n):
a = 0
b = 1
# print("Fibonacci series ( using iterative method ) is : ")
# print(a,b,end=" ")
for i in range(n - 2):
c = a + b
a = b
b = c
# print(c,end=" ")
return c
def fib_rec(n):
a = 0
b=1
if n <= 0:
print("No series")
else:
if n == 1:
return a
elif n == 2:
# print("1")
return b
else:
# print(fib_rec(n-2)+fib_rec(n-1),end=" ")
return fib_rec(n - 2) + fib_rec(n - 1)
# print("Fibonacci series ( using recursive method ) is : ")
ci=0
cr=0
for i in range(10,31,5):
print(f"{i}th Fibonacci number ( using iterative method ) is : ",end=" ")
t1 = time.time()
print(fib_iter(i))
t2 = time.time()
ti = t2 - t1
print(f"time taken taken by iterative method : {t2 - t1}")
print()
print(f"{i}th Fibonacci number ( using recursive method ) is : ",end=" ")
t3 = time.time()
print(fib_rec(i))
t4 = time.time()
tr = t4 - t3
print(f"time taken taken by iterative method : {t4 - t3}")
print()
if tr > ti:
print("iterative method is faster")
ci+=1
else:
print("recursive method is faster")
cr+=1
print()
print("--------------------------------------------------------------------------------")
print()
print(f"Number of times iterative method is faster is : {ci} and number of times recursive method is faster is : {cr}")
print()