FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
TheAlgorithms-Python/dynamic_programming/factorial.py at master · windNight/TheAlgorithms-Python · GitHub
windNight
/
TheAlgorithms-Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
TheAlgorithms-Python
/
dynamic_programming
/
factorial.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (20 loc) · 585 Bytes
Breadcrumbs
TheAlgorithms-Python
/
dynamic_programming
/
factorial.py
Copy path
File metadata and controls
27 lines (20 loc) · 585 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
# Factorial of a number using memoization
from
functools
import
lru_cache
@
lru_cache
def
factorial
(
num
:
int
)
->
int
:
"""
>>> 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
num
<
0
:
raise
ValueError
(
"Number should not be negative."
)
return
1
if
num
in
(
0
,
1
)
else
num
*
factorial
(
num
-
1
)
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
Back
|
FazBrowse Home
|
New Git URL