-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractical-4.py
executable file
·102 lines (86 loc) · 3.05 KB
/
practical-4.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
"Author": "Fenil Gandhi",
"Version": "Python 3.6.2",
"Description": "Implementation and Time analysis of factorial program using iterative and recursive method",
"Input": [1, 5, 10, 20, 30, 40, 50, 100, 500, 1000, 3000],
"Output":
"""
---------------------------
| Iterative Factorial |
---------------------------
| Input | Time |
---------------------------
| 1 | 0.00000167 |
| 5 | 0.00000215 |
| 10 | 0.00000286 |
| 20 | 0.00000477 |
| 30 | 0.00000644 |
| 40 | 0.00000858 |
| 50 | 0.00001097 |
| 100 | 0.00002360 |
| 500 | 0.00020289 |
| 1000 | 0.00066137 |
| 3000 | 0.00470996 |
---------------------------
---------------------------
| Recursive Factorial |
---------------------------
| Input | Time |
---------------------------
| 1 | 0.00000334 |
| 5 | 0.00000548 |
| 10 | 0.00000525 |
| 20 | 0.00000834 |
| 30 | 0.00001192 |
| 40 | 0.00001502 |
| 50 | 0.00002003 |
| 100 | 0.00004601 |
| 500 | 0.00072169 |
| 1000 | 0.00112271 |
| 3000 | 0.00597692 |
---------------------------
""",
}
import time
import sys
sys.setrecursionlimit(3050)
def timeit(func):
def decorated_function(*args):
a = time.time()
func(*args)
b = time.time()
return (b - a)
return decorated_function
def iterative_factorial(n):
ans = 1
while (n > 1):
ans = ans * n
n -= 1
return ans
def recursive_factorial(n):
if (n > 1):
return n * recursive_factorial(n - 1)
else:
return 1
def print_formatted(name, input_sizes, time_taken):
print(" " * 60, "{0:^50s}".format(name), "-" * 55, sep="\n")
print("| {0:^10s} | {1:^10s} |".format("Input", "Time"))
print("-" * 55)
for i in range(len(input_sizes)):
print("| {0:>10d} | {1:>2.8f} |".format(input_sizes[i], time_taken[i]))
print("-" * 55, end="\n\n")
# Decorating Recursive Functions
timed_iterative_factorial = timeit(iterative_factorial)
timed_recursive_factorial = timeit(recursive_factorial)
if __name__ == '__main__':
input_sizes = [1, 5, 10, 20, 30, 40, 50, 100, 500, 1000, 3000]
# Iterative Approach
time_taken = []
for each_input in input_sizes:
time_taken.append(timed_iterative_factorial(each_input))
print_formatted("Iterative Factorial", input_sizes, time_taken)
# Recursive Approach
time_taken = []
for each_input in input_sizes:
time_taken.append(timed_recursive_factorial(each_input))
print_formatted("Recursive Factorial", input_sizes, time_taken)