We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e2a78d4 commit 83f185aCopy full SHA for 83f185a
1 file changed
maths/factorial_recursive.py
@@ -0,0 +1,36 @@
1
+"""
2
+Recursive factorial implementation.
3
+
4
+Reference: https://en.wikipedia.org/wiki/Factorial
5
6
7
+def factorial(number: int) -> int:
8
+ """
9
+ Calculate the factorial of a non-negative integer recursively.
10
11
+ Parameters:
12
+ number (int): A non-negative integer whose factorial is to be calculated.
13
14
+ Returns:
15
+ int: The factorial of the input number.
16
17
+ Raises:
18
+ ValueError: If the input number is negative.
19
20
+ Examples:
21
+ >>> factorial(0)
22
+ 1
23
+ >>> factorial(1)
24
25
+ >>> factorial(5)
26
+ 120
27
28
+ if number < 0:
29
+ raise ValueError("number must be non-negative")
30
+ if number <= 1:
31
+ return 1
32
+ return number * factorial(number - 1)
33
34
35
+if __name__ == "__main__":
36
+ print(factorial(5))
0 commit comments