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

Function factorial

dynamic_programming/factorial.py:7–21  ·  view source on GitHub ↗

>>> factorial(7) 5040 >>> factorial(-1) Traceback (most recent call last): ... ValueError: Number should not be negative. >>> [factorial(i) for i in range(10)] [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]

(num: int)

Source from the content-addressed store, hash-verified

5
6@lru_cache
7def factorial(num: int) -> int:
8 """
9 >>> factorial(7)
10 5040
11 >>> factorial(-1)
12 Traceback (most recent call last):
13 ...
14 ValueError: Number should not be negative.
15 >>> [factorial(i) for i in range(10)]
16 [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
17 """
18 if num < 0:
19 raise ValueError("Number should not be negative.")
20
21 return 1 if num in (0, 1) else num * factorial(num - 1)
22
23
24if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected