-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex1.5.py
41 lines (34 loc) · 968 Bytes
/
ex1.5.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
import timeit
import matplotlib.pyplot as plt
# Improved Fibonacci Pseudocode
def func1(n, memo={}):
if n == 0 or n == 1:
return n
if n in memo:
return memo[n]
else:
memo[n] = func1(n-1, memo) + func1(n-2, memo)
return memo[n]
# Original Fibonacci Pseudocode
def func(n):
if n == 0 or n == 1:
return n
else:
return func(n-1) + func(n-2)
#Store the results
results_impro = []
results_orig = []
for i in range(36):
impro_time = timeit.timeit(lambda: func1(i), number=1)
results_impro.append(impro_time)
for i in range(36):
orig_time = timeit.timeit(lambda: func(i), number=1)
results_orig.append(orig_time)
# Plot the results
plt.plot(results_orig, label="Original Fibonacci", color="blue")
plt.plot(results_impro, label="Improved Fibonacci", color="red")
plt.xlabel("Value of n")
plt.ylabel("Time (s)")
plt.title("Time Comparison of the Algorithms")
plt.legend()
plt.show()