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

Function factorial

maths/factorial.py:6–35  ·  view source on GitHub ↗

Calculate the factorial of specified number (n!). >>> import math >>> all(factorial(i) == math.factorial(i) for i in range(20)) True >>> factorial(0.1) Traceback (most recent call last): ... ValueError: factorial() only accepts integral values >>> factorial(

(number: int)

Source from the content-addressed store, hash-verified

4
5
6def factorial(number: int) -> int:
7 """
8 Calculate the factorial of specified number (n!).
9
10 >>> import math
11 >>> all(factorial(i) == math.factorial(i) for i in range(20))
12 True
13 >>> factorial(0.1)
14 Traceback (most recent call last):
15 ...
16 ValueError: factorial() only accepts integral values
17 >>> factorial(-1)
18 Traceback (most recent call last):
19 ...
20 ValueError: factorial() not defined for negative values
21 >>> factorial(1)
22 1
23 >>> factorial(6)
24 720
25 >>> factorial(0)
26 1
27 """
28 if number != int(number):
29 raise ValueError("factorial() only accepts integral values")
30 if number < 0:
31 raise ValueError("factorial() not defined for negative values")
32 value = 1
33 for i in range(1, number + 1):
34 value *= i
35 return value
36
37
38def factorial_recursive(n: int) -> int:

Callers 1

factorial.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected