MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / factorial_recursive

Function factorial_recursive

maths/factorial.py:38–59  ·  view source on GitHub ↗

Calculate the factorial of a positive integer https://en.wikipedia.org/wiki/Factorial >>> import math >>> all(factorial_recursive(i) == math.factorial(i) for i in range(20)) True >>> factorial_recursive(0.1) Traceback (most recent call last): ... ValueError:

(n: int)

Source from the content-addressed store, hash-verified

36
37
38def factorial_recursive(n: int) -> int:
39 """
40 Calculate the factorial of a positive integer
41 https://en.wikipedia.org/wiki/Factorial
42
43 >>> import math
44 >>> all(factorial_recursive(i) == math.factorial(i) for i in range(20))
45 True
46 >>> factorial_recursive(0.1)
47 Traceback (most recent call last):
48 ...
49 ValueError: factorial_recursive() only accepts integral values
50 >>> factorial_recursive(-1)
51 Traceback (most recent call last):
52 ...
53 ValueError: factorial_recursive() not defined for negative values
54 """
55 if not isinstance(n, int):
56 raise ValueError("factorial_recursive() only accepts integral values")
57 if n < 0:
58 raise ValueError("factorial_recursive() not defined for negative values")
59 return 1 if n in {0, 1} else n * factorial_recursive(n - 1)
60
61
62if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected