greatest common divisor wikipedia reference https en wikipedia orgwikigreatestcommondivisor gcda b gcda b gcda b gcda b by definition of divisibility calculate greatest common divisor gcd greatestcommondivisor24 40 8 greatestcommondivisor1 1 1 greatestcommondivisor1 800 1 greatestcommondivisor11 37 1 greatestcommondivisor3 5 1 greatestcommondivisor16 4 4 greatestcommondivisor3 9 3 greatestcommondivisor9 3 3 greatestcommondivisor3 9 3 greatestcommondivisor3 9 3 below method is more memory efficient because it does not create additional stack frames for recursive functions calls as done in the above method gcdbyiterative24 40 8 greatestcommondivisor24 40 gcdbyiterative24 40 true gcdbyiterative3 9 3 gcdbyiterative3 9 3 gcdbyiterative1 800 1 gcdbyiterative11 37 1 call greatest common divisor function calculate greatest common divisor gcd greatest_common_divisor 24 40 8 greatest_common_divisor 1 1 1 greatest_common_divisor 1 800 1 greatest_common_divisor 11 37 1 greatest_common_divisor 3 5 1 greatest_common_divisor 16 4 4 greatest_common_divisor 3 9 3 greatest_common_divisor 9 3 3 greatest_common_divisor 3 9 3 greatest_common_divisor 3 9 3 below method is more memory efficient because it does not create additional stack frames for recursive functions calls as done in the above method gcd_by_iterative 24 40 8 greatest_common_divisor 24 40 gcd_by_iterative 24 40 true gcd_by_iterative 3 9 3 gcd_by_iterative 3 9 3 gcd_by_iterative 1 800 1 gcd_by_iterative 11 37 1 when y 0 then loop will terminate and return x as final gcd call greatest common divisor function
def greatest_common_divisor(a: int, b: int) -> int:
    
    return abs(b) if a == 0 else greatest_common_divisor(b % a, a)


def gcd_by_iterative(x: int, y: int) -> int:
    
    while y:  
        x, y = y, x % y
    return abs(x)


def main():
    
    try:
        nums = input("Enter two integers separated by comma (,): ").split(",")
        num_1 = int(nums[0])
        num_2 = int(nums[1])
        print(
            f"greatest_common_divisor({num_1}, {num_2}) = "
            f"{greatest_common_divisor(num_1, num_2)}"
        )
        print(f"By iterative gcd({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}")
    except (IndexError, UnboundLocalError, ValueError):
        print("Wrong input")


if __name__ == "__main__":
    main()