1- """
2- Greatest Common Divisor.
1+ """Greatest Common Divisor (GCD).
32
4- Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor
3+ Wikipedia reference:
4+ https://en.wikipedia.org/wiki/Greatest_common_divisor
55
6- gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b) by definition of divisibility
6+ gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b)
7+ by definition of divisibility.
78"""
89
910
1011def greatest_common_divisor (a : int , b : int ) -> int :
1112 """
12- Calculate Greatest Common Divisor (GCD).
13+ Calculate the greatest common divisor (GCD).
14+
1315 >>> greatest_common_divisor(24, 40)
1416 8
1517 >>> greatest_common_divisor(1, 1)
@@ -36,8 +38,10 @@ def greatest_common_divisor(a: int, b: int) -> int:
3638
3739def gcd_by_iterative (x : int , y : int ) -> int :
3840 """
39- Below method is more memory efficient because it does not create additional
40- stack frames for recursive functions calls (as done in the above method).
41+ Iterative method to compute the greatest common divisor.
42+
43+ This method is more memory efficient as it avoids recursive calls.
44+
4145 >>> gcd_by_iterative(24, 40)
4246 8
4347 >>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40)
@@ -51,15 +55,13 @@ def gcd_by_iterative(x: int, y: int) -> int:
5155 >>> gcd_by_iterative(11, 37)
5256 1
5357 """
54- while y : # --> when y=0 then loop will terminate and return x as final GCD.
58+ while y :
5559 x , y = y , x % y
5660 return abs (x )
5761
5862
5963def main ():
60- """
61- Call Greatest Common Divisor function.
62- """
64+ """Run GCD functions with user input."""
6365 try :
6466 nums = input ("Enter two integers separated by comma (,): " ).split ("," )
6567 num_1 = int (nums [0 ])
0 commit comments