-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfactorial.py
More file actions
35 lines (30 loc) · 779 Bytes
/
factorial.py
File metadata and controls
35 lines (30 loc) · 779 Bytes
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
# interative method
def fact(x):
k = x
for x in range(1,x):
k = k * x
return k
# recursive method
def recursiveFact(x):
if x == 1:
return 1
else:
return x * recursiveFact(x-1)
print(f"Factorial Algorithm\n")
print(f"========================================\n")
x = True
while(x):
try:
number = int(input(f"Please enter an integer -> "))
x = False
except:
print(f"Please, try again, integer number only \n")
# measure initial time
st = time.time()
print(f"\nFactorial of {number} is {fact(number)}")
ed = time.time()
print(f"\nTime spent in calculation {(ed-st)*1000} ms")
st = time.time()
print(f"\nRecursive Factorial of {number} is {recursiveFact(number)}")
ed = time.time()
print(f"\nTime spent in calculation is {(ed-st)*1000} ms")