Skip to content

Commit d60dc64

Browse files
committed
remove duplicated factorial function
1 parent 6fcf1b8 commit d60dc64

File tree

2 files changed

+18
-39
lines changed

2 files changed

+18
-39
lines changed

maths/factorial_iterative.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial
1+
"""Factorial of a positive integer -- https://en.wikipedia.org/wiki/Factorial
2+
"""
23

34

4-
def factorial(n: int) -> int:
5+
def factorial(input_number: int) -> int:
56
"""
7+
Calculate the factorial of specified number (n!).
8+
69
>>> import math
710
>>> all(factorial(i) == math.factorial(i) for i in range(20))
811
True
@@ -14,17 +17,27 @@ def factorial(n: int) -> int:
1417
Traceback (most recent call last):
1518
...
1619
ValueError: factorial() not defined for negative values
20+
>>> factorial(1)
21+
1
22+
>>> factorial(6)
23+
720
24+
>>> factorial(0)
25+
1
1726
"""
18-
if n != int(n):
27+
if input_number != int(input_number):
1928
raise ValueError("factorial() only accepts integral values")
20-
if n < 0:
29+
if input_number < 0:
2130
raise ValueError("factorial() not defined for negative values")
2231
value = 1
23-
for i in range(1, n + 1):
32+
for i in range(1, input_number + 1):
2433
value *= i
2534
return value
2635

2736

2837
if __name__ == "__main__":
38+
import doctest
39+
40+
doctest.testmod()
41+
2942
n = int(input("Enter a positive integer: ").strip() or 0)
3043
print(f"factorial{n} is {factorial(n)}")

maths/factorial_python.py

-34
This file was deleted.

0 commit comments

Comments
 (0)