-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21_write_a_pyrhon_script.py
49 lines (40 loc) · 1.42 KB
/
21_write_a_pyrhon_script.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
# Write a python script to write the execution time for a variable in a fole where the variable is used to find file where the variable is used to find factorial using iterative & recursion method
import time
def fact_iterative(n):
facto=1
if(n==0):
# print("Factorial of 0 is 1")
return 1
else:
for i in range(1,n+1):
facto=facto*i
return facto
# print("Factorial of ",n," is :",facto )
def fact_recursive(n):
if(n==0):
# print("Factorial of 0 is 1")
return 1
else:
return n*fact_recursive(n-1)
# print("Factorial of ",n," is :",facto )
# Time Calculation for Iterative method
start_iter =time.time()
for i in range(5,1000,5):
fact_iterative(i)
end_iter =time.time()
exe_iter=end_iter-start_iter
f=open("15_Python_Script","w")
exe_iterative=str(exe_iter)
f.write("Execution time of Iterative Method is "+exe_iterative)
# Excution time calulation for Recursive method
start_recursive=time.time()
for i in range(5,1000,5):
fact_recursive(i)
end_recusive =time.time()
exe_recursive=end_recusive-start_recursive
exe_recur=str(exe_recursive)
f.write("\nExecution time of Recursive Method is "+exe_recur+"\n")
# exe_diff=exe_recursive-exe_iter
# exe_difference=str(exe_diff)
# f.write("Difference between Recursive amd Iterative method is : " + exe_diff)
f.close()