compute double factorial using recursive method recursion can be costly for large numbers to learn about the theory behind this algorithm https en wikipedia orgwikidoublefactorial from math import prod alldoublefactorialrecursivei prodrangei 0 2 for i in range20 true doublefactorialrecursive0 1 traceback most recent call last valueerror doublefactorialrecursive only accepts integral values doublefactorialrecursive1 traceback most recent call last valueerror doublefactorialrecursive not defined for negative values compute double factorial using iterative method to learn about the theory behind this algorithm https en wikipedia orgwikidoublefactorial from math import prod alldoublefactorialiterativei prodrangei 0 2 for i in range20 true doublefactorialiterative0 1 traceback most recent call last valueerror doublefactorialiterative only accepts integral values doublefactorialiterative1 traceback most recent call last valueerror doublefactorialiterative not defined for negative values compute double factorial using recursive method recursion can be costly for large numbers to learn about the theory behind this algorithm https en wikipedia org wiki double_factorial from math import prod all double_factorial_recursive i prod range i 0 2 for i in range 20 true double_factorial_recursive 0 1 traceback most recent call last valueerror double_factorial_recursive only accepts integral values double_factorial_recursive 1 traceback most recent call last valueerror double_factorial_recursive not defined for negative values 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_factorial_iterative 0 1 traceback most recent call last valueerror double_factorial_iterative only accepts integral values double_factorial_iterative 1 traceback most recent call last valueerror double_factorial_iterative not defined for negative values
def double_factorial_recursive(n: int) -> int:
    
    if not isinstance(n, int):
        raise ValueError("double_factorial_recursive() only accepts integral values")
    if n < 0:
        raise ValueError("double_factorial_recursive() not defined for negative values")
    return 1 if n <= 1 else n * double_factorial_recursive(n - 2)


def double_factorial_iterative(num: int) -> int:
    
    if not isinstance(num, int):
        raise ValueError("double_factorial_iterative() only accepts integral values")
    if num < 0:
        raise ValueError("double_factorial_iterative() not defined for negative values")
    value = 1
    for i in range(num, 0, -2):
        value *= i
    return value


if __name__ == "__main__":
    import doctest

    doctest.testmod()