rewrite the algorithm from scratch (#1351)

This commit is contained in:
Yurii 2019-10-18 08:27:55 +03:00 committed by Christian Clauss
parent 14c23bc847
commit 870eebf349

View File

@ -1,19 +1,21 @@
"""Python program to find the factorial of a number provided by the user."""
def factorial(input_number: int) -> int:
"""
Non-recursive algorithm of finding factorial of the
input number.
>>> factorial(1)
1
>>> factorial(6)
720
>>> factorial(0)
1
"""
# change the value for a different result
NUM = 10
# uncomment to take input from the user
# num = int(input("Enter a number: "))
FACTORIAL = 1
# check if the number is negative, positive or zero
if NUM < 0:
print("Sorry, factorial does not exist for negative numbers")
elif NUM == 0:
print("The factorial of 0 is 1")
else:
for i in range(1, NUM + 1):
FACTORIAL = FACTORIAL * i
print("The factorial of", NUM, "is", FACTORIAL)
if input_number < 0:
raise ValueError('Input input_number should be non-negative')
elif input_number == 0:
return 1
else:
result = 1
for i in range(input_number):
result = result * (i + 1)
return result