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

Function double_factorial_iterative

maths/double_factorial.py:28–54  ·  view source on GitHub ↗

Compute double factorial using iterative method. To learn about the theory behind this algorithm: https://en.wikipedia.org/wiki/Double_factorial >>> from math import prod >>> all(double_factorial_iterative(i) == prod(range(i, 0, -2)) for i in range(20)) True >>> double

(num: int)

Source from the content-addressed store, hash-verified

26
27
28def double_factorial_iterative(num: int) -> int:
29 """
30 Compute double factorial using iterative method.
31
32 To learn about the theory behind this algorithm:
33 https://en.wikipedia.org/wiki/Double_factorial
34
35 >>> from math import prod
36 >>> all(double_factorial_iterative(i) == prod(range(i, 0, -2)) for i in range(20))
37 True
38 >>> double_factorial_iterative(0.1)
39 Traceback (most recent call last):
40 ...
41 ValueError: double_factorial_iterative() only accepts integral values
42 >>> double_factorial_iterative(-1)
43 Traceback (most recent call last):
44 ...
45 ValueError: double_factorial_iterative() not defined for negative values
46 """
47 if not isinstance(num, int):
48 raise ValueError("double_factorial_iterative() only accepts integral values")
49 if num < 0:
50 raise ValueError("double_factorial_iterative() not defined for negative values")
51 value = 1
52 for i in range(num, 0, -2):
53 value *= i
54 return value
55
56
57if __name__ == "__main__":

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected