gamma function is a very useful tool in math and physics it helps calculating complex integral in a convenient way for more info https en wikipedia orgwikigammafunction in mathematics the gamma function is one commonly used extension of the factorial function to complex numbers the gamma function is defined for all complex numbers except the nonpositive integers python s standard library math gamma function overflows around gamma171 624 calculates the value of gamma function of num where num is either an integer 1 2 3 or a halfinteger 0 5 1 5 2 5 gammaiterative1 traceback most recent call last valueerror math domain error gammaiterative0 traceback most recent call last valueerror math domain error gammaiterative9 40320 0 from math import gamma as mathgamma all 99999999 gammaiterativei mathgammai 1 000000001 for i in range1 50 true gammaiterative1mathgamma1 1 000000001 traceback most recent call last valueerror math domain error gammaiterative3 3 mathgamma3 3 0 00000001 true calculates the value of gamma function of num where num is either an integer 1 2 3 or a halfinteger 0 5 1 5 2 5 implemented using recursion examples from math import isclose gamma as mathgamma gammarecursive0 5 1 7724538509055159 gammarecursive1 1 0 gammarecursive2 1 0 gammarecursive3 5 3 3233509704478426 gammarecursive171 5 9 483367566824795e307 allisclosegammarecursivenum mathgammanum for num in 0 5 2 3 5 171 5 true gammarecursive0 traceback most recent call last valueerror math domain error gammarecursive1 1 traceback most recent call last valueerror math domain error gammarecursive4 traceback most recent call last valueerror math domain error gammarecursive172 traceback most recent call last overflowerror math range error gammarecursive1 1 traceback most recent call last notimplementederror num must be an integer or a halfinteger calculates the value of gamma function of num where num is either an integer 1 2 3 or a half integer 0 5 1 5 2 5 gamma_iterative 1 traceback most recent call last valueerror math domain error gamma_iterative 0 traceback most recent call last valueerror math domain error gamma_iterative 9 40320 0 from math import gamma as math_gamma all 99999999 gamma_iterative i math_gamma i 1 000000001 for i in range 1 50 true gamma_iterative 1 math_gamma 1 1 000000001 traceback most recent call last valueerror math domain error gamma_iterative 3 3 math_gamma 3 3 0 00000001 true calculates the value of gamma function of num where num is either an integer 1 2 3 or a half integer 0 5 1 5 2 5 implemented using recursion examples from math import isclose gamma as math_gamma gamma_recursive 0 5 1 7724538509055159 gamma_recursive 1 1 0 gamma_recursive 2 1 0 gamma_recursive 3 5 3 3233509704478426 gamma_recursive 171 5 9 483367566824795e 307 all isclose gamma_recursive num math_gamma num for num in 0 5 2 3 5 171 5 true gamma_recursive 0 traceback most recent call last valueerror math domain error gamma_recursive 1 1 traceback most recent call last valueerror math domain error gamma_recursive 4 traceback most recent call last valueerror math domain error gamma_recursive 172 traceback most recent call last overflowerror math range error gamma_recursive 1 1 traceback most recent call last notimplementederror num must be an integer or a half integer
import math

from numpy import inf
from scipy.integrate import quad


def gamma_iterative(num: float) -> float:
    
    if num <= 0:
        raise ValueError("math domain error")

    return quad(integrand, 0, inf, args=(num))[0]


def integrand(x: float, z: float) -> float:
    return math.pow(x, z - 1) * math.exp(-x)


def gamma_recursive(num: float) -> float:
    
    if num <= 0:
        raise ValueError("math domain error")
    if num > 171.5:
        raise OverflowError("math range error")
    elif num - int(num) not in (0, 0.5):
        raise NotImplementedError("num must be an integer or a half-integer")
    elif num == 0.5:
        return math.sqrt(math.pi)
    else:
        return 1.0 if num == 1 else (num - 1) * gamma_recursive(num - 1)


if __name__ == "__main__":
    from doctest import testmod

    testmod()
    num = 1.0
    while num:
        num = float(input("Gamma of: "))
        print(f"gamma_iterative({num}) = {gamma_iterative(num)}")
        print(f"gamma_recursive({num}) = {gamma_recursive(num)}")
        print("\nEnter 0 to exit...")