Learning Scientific Programming with Python (2nd edition)
P2.4.6: The double factorial function
Question P2.4.6
The factorial function, $n! = 1\cdot 2\cdot 3\cdot \cdots (n-1)n$ is the product of the first $n$ positive integers and is provided by the math
module's factorial
method. The double factorial function, $n!!$, is the product of the positive odd integers up to and including $n$ (which must itself be odd):
$$
n!! = \prod_{i=1}^{(n+1)/2} (2i-1) = 1\cdot 3\cdot 5\cdots (n-2) \cdot n.
$$
Write a routine to calculate $n!!$ in Python.
As a bonus exercise, extend the formula for to allow for even $n$ as follows: $$ n!! = \prod_{i=1}^{n/2} (2i) = 2\cdot 4\cdot 6\cdots (n-2)\cdot n. $$