-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathfactorial.py
More file actions
44 lines (33 loc) · 876 Bytes
/
factorial.py
File metadata and controls
44 lines (33 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Factorial of a number using memoization
from functools import lru_cache
@lru_cache
def factorial(num, _memo=None):
"""
Returns the factorial of a non-negative integer using memoization.
>>> factorial(0)
1
>>> factorial(5)
120
>>> 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]
"""
if _memo is None:
_memo = {}
if num < 0:
raise ValueError("Number should not be negative.")
if num in _memo:
return _memo[num]
if num in (0, 1):
_memo[num] = 1
else:
_memo[num] = num * factorial(num - 1, _memo)
return _memo[num]
if __name__ == "__main__":
import doctest
doctest.testmod()