"""
Greatest Common Divisor.

Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor
"""


def greatest_common_divisor(a, b):
    """
    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
    """
    return b if a == 0 else greatest_common_divisor(b % a, a)


"""
Below method is more memory efficient because it does not use the stack (chunk of
memory).  While above method is good, uses more memory for huge numbers because of the
recursive calls required to calculate the greatest common divisor.
"""


def gcd_by_iterative(x, y):
    """
    >>> gcd_by_iterative(24, 40)
    8
    >>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40)
    True
    """
    while y:  # --> when y=0 then loop will terminate and return x as final GCD.
        x, y = y, x % y
    return x


def main():
    """Call Greatest Common Divisor function."""
    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()

@@ -97,16 +97,97 @@
 _divisor
+%0A%0Agcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b) by definition of divisibility
 %0A%22%22%22%0A%0A%0Ad
@@ -214,20 +214,37 @@
 ivisor(a
-, b)
+: int, b: int) -%3E int
 :%0A    %22%22
@@ -561,16 +561,197 @@
 )%0A    4%0A
+    %3E%3E%3E greatest_common_divisor(-3, 9)%0A    3%0A    %3E%3E%3E greatest_common_divisor(9, -3)%0A    3%0A    %3E%3E%3E greatest_common_divisor(3, -9)%0A    3%0A    %3E%3E%3E greatest_common_divisor(-3, -9)%0A    3%0A
     %22%22%22%0A
@@ -761,17 +761,22 @@
  return 
-b
+abs(b)
  if a ==
@@ -819,20 +819,73 @@
 a, a)%0A%0A%0A
-%22%22%22%0A
+def gcd_by_iterative(x: int, y: int) -%3E int:%0A    %22%22%22%0A    
 Below me
@@ -938,160 +938,150 @@
 not 
-use the stack (chunk of%0Amemory).  While above method is good, uses more memory for huge numbers because of the%0Arecursive calls required to calculate the
+create additional%0A    stack frames for recursive functions calls (as done in the above method).%0A    %3E%3E%3E gcd_by_iterative(24, 40)%0A    8%0A    %3E%3E%3E
  gre
@@ -1089,34 +1089,34 @@
 test
- 
+_
 common
- 
+_
 divisor
-.%0A%22%22%22%0A%0A%0Adef
+(24, 40) ==
  gcd
@@ -1133,22 +1133,24 @@
 ive(
-x, y):
+24, 40)
 %0A    
-%22%22%22
+True
 %0A   
@@ -1167,37 +1167,37 @@
 y_iterative(
-24, 40
+-3, -9
 )%0A    
-8
+3
 %0A    %3E%3E%3E gre
@@ -1198,41 +1198,84 @@
 %3E%3E g
-reatest_common_divisor(24, 40) ==
+cd_by_iterative(3, -9)%0A    3%0A    %3E%3E%3E gcd_by_iterative(1, -800)%0A    1%0A    %3E%3E%3E
  gcd
@@ -1288,32 +1288,29 @@
 erative(
-24, 40
+11, 37
 )%0A    
-True
+1
 %0A    %22%22%22
@@ -1426,17 +1426,22 @@
  return 
-x
+abs(x)
 %0A%0A%0Adef m
@@ -1454,16 +1454,21 @@
 %0A    %22%22%22
+%0A    
 Call Gre
@@ -1497,16 +1497,21 @@
 unction.
+%0A    
 %22%22%22%0A    

add support for negative numbers (#2628)