Skip to content

Commit

Permalink
feat: Concatenate both factorial implementations (TheAlgorithms#8099)
Browse files Browse the repository at this point in the history
* feat: Concatenate both factorial implementations

* fix: Rename factorial recursive method
  • Loading branch information
CaedenPH authored and Cjkjvfnby committed Mar 13, 2023
1 parent 6fb38f0 commit 971a543
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
24 changes: 24 additions & 0 deletions maths/factorial_iterative.py → maths/factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ def factorial(number: int) -> int:
return value


def factorial_recursive(n: int) -> int:
"""
Calculate the factorial of a positive integer
https://en.wikipedia.org/wiki/Factorial
>>> import math
>>> all(factorial(i) == math.factorial(i) for i in range(20))
True
>>> factorial(0.1)
Traceback (most recent call last):
...
ValueError: factorial() only accepts integral values
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: factorial() not defined for negative values
"""
if not isinstance(n, int):
raise ValueError("factorial() only accepts integral values")
if n < 0:
raise ValueError("factorial() not defined for negative values")
return 1 if n == 0 or n == 1 else n * factorial(n - 1)


if __name__ == "__main__":
import doctest

Expand Down
28 changes: 0 additions & 28 deletions maths/factorial_recursive.py

This file was deleted.

0 comments on commit 971a543

Please sign in to comment.