From 1d4fc814fac69ecd821559b0a540f4eea588b922 Mon Sep 17 00:00:00 2001 From: Username Date: Mon, 6 Sep 2021 13:16:58 -0400 Subject: [PATCH] Updated file --- maths/gamma_recursive.py | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 maths/gamma_recursive.py diff --git a/maths/gamma_recursive.py b/maths/gamma_recursive.py new file mode 100644 index 000000000..11c14c2cb --- /dev/null +++ b/maths/gamma_recursive.py @@ -0,0 +1,83 @@ +""" +Gamma function is a very useful tool in physics. +It helps calculating complex integral in a convenient way. +for more info: https://en.wikipedia.org/wiki/Gamma_function +""" + +# Importing packages +from math import sqrt, pi +from re import match +from typing import Union + + +def gamma(num : Union[int, float]) -> Union[int, float]: + """ + 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: + >>> Gamma of: 0.5 + √π + >>> Gamma of: 2 + 1 + >>> Gamma of: 3.5 + 1.875√π + """ + if num == 1: + return 1 + elif num == 0.5: + return sqrt(pi) + elif num > 1: + return (num - 1) * gamma(num - 1) + # Error + return -2 + + +def test_gamma() -> None: + """ + >>> test_gamma() + """ + assert sqrt(pi) == gamma(0.5) + assert 1 == gamma(1) + assert 1 == gamma(2) + + +if __name__ == "__main__": + # Initialize boolean + number = True + # Get input from user + input_ = input("Gamma of: ") + # Ensure valid input + try: + # Ensure input matches half-integer (float) pattern + if match(r"^[0-9]*\.5$", input_): + # Convert string to float + num = float(input_) + # Ensure input matches an integer pattern + elif match(r"^[1-9][0-9]*$", input_): + # Convert string to int + num = int(input_) + # Input is not a valid number + else: + # raise an error + raise ValueError + # Ensure print an error message + except ValueError: + print("Error: Input must be an integer or an half-integer!") + number = False + finally: + # Ensure input is a valid number + if number: + print(f"\u0393({num}) = ", end="") + # Ensure input is an integer + if isinstance(gamma(num), int): + # Print result + print(gamma(num)) + # Otherwise print results with √π (gamma of 0.5 is √π) + # Therefore all results will be a number times √π + else: + results = f"{gamma(num) / sqrt(pi):.4f}" + results = results.rstrip("0").rstrip(".") + if results == "1": + results = "" + print(results + "\u221A\u03c0") \ No newline at end of file