mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-18 18:14:34 +08:00

committed by
Christian Clauss

parent
80e1c8748a
commit
814750e637
@ -1,21 +1,34 @@
|
||||
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
|
||||
Calculate the factorial of specified number
|
||||
|
||||
>>> factorial(1)
|
||||
1
|
||||
>>> factorial(6)
|
||||
720
|
||||
>>> factorial(0)
|
||||
1
|
||||
>>> factorial(-1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: factorial() not defined for negative values
|
||||
>>> factorial(0.1)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: factorial() only accepts integral values
|
||||
"""
|
||||
|
||||
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)
|
||||
raise ValueError("factorial() not defined for negative values")
|
||||
if not isinstance(input_number, int):
|
||||
raise ValueError("factorial() only accepts integral values")
|
||||
result = 1
|
||||
for i in range(1, input_number):
|
||||
result = result * (i + 1)
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
Reference in New Issue
Block a user