Skip to content

Commit 90c5958

Browse files
handle gcd base case when b == 0 and improve input parsing
1 parent e2a78d4 commit 90c5958

File tree

1 file changed

+15
-55
lines changed

1 file changed

+15
-55
lines changed

maths/greatest_common_divisor.py

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,36 @@
11
"""
2-
Greatest Common Divisor.
3-
4-
Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor
5-
6-
gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b) by definition of divisibility
2+
Greatest Common Divisor (GCD) Program
73
"""
84

9-
105
def greatest_common_divisor(a: int, b: int) -> int:
116
"""
12-
Calculate Greatest Common Divisor (GCD).
13-
>>> greatest_common_divisor(24, 40)
14-
8
15-
>>> greatest_common_divisor(1, 1)
16-
1
17-
>>> greatest_common_divisor(1, 800)
18-
1
19-
>>> greatest_common_divisor(11, 37)
20-
1
21-
>>> greatest_common_divisor(3, 5)
22-
1
23-
>>> greatest_common_divisor(16, 4)
24-
4
25-
>>> greatest_common_divisor(-3, 9)
26-
3
27-
>>> greatest_common_divisor(9, -3)
28-
3
29-
>>> greatest_common_divisor(3, -9)
30-
3
31-
>>> greatest_common_divisor(-3, -9)
32-
3
7+
Calculates the Greatest Common Divisor (GCD) using recursion.
338
"""
34-
return abs(b) if a == 0 else greatest_common_divisor(b % a, a)
9+
# Bug fixed: Correct recursive definition
10+
return abs(a) if b == 0 else greatest_common_divisor(b, a % b)
3511

3612

3713
def gcd_by_iterative(x: int, y: int) -> int:
3814
"""
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-
>>> gcd_by_iterative(24, 40)
42-
8
43-
>>> greatest_common_divisor(24, 40) == gcd_by_iterative(24, 40)
44-
True
45-
>>> gcd_by_iterative(-3, -9)
46-
3
47-
>>> gcd_by_iterative(3, -9)
48-
3
49-
>>> gcd_by_iterative(1, -800)
50-
1
51-
>>> gcd_by_iterative(11, 37)
52-
1
15+
Calculates the GCD using an iterative method (more memory efficient).
5316
"""
54-
while y: # --> when y=0 then loop will terminate and return x as final GCD.
17+
while y:
5518
x, y = y, x % y
5619
return abs(x)
5720

5821

5922
def main():
60-
"""
61-
Call Greatest Common Divisor function.
62-
"""
23+
"""Main function for user input and output."""
6324
try:
6425
nums = input("Enter two integers separated by comma (,): ").split(",")
65-
num_1 = int(nums[0])
66-
num_2 = int(nums[1])
67-
print(
68-
f"greatest_common_divisor({num_1}, {num_2}) = "
69-
f"{greatest_common_divisor(num_1, num_2)}"
70-
)
71-
print(f"By iterative gcd({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}")
72-
except (IndexError, UnboundLocalError, ValueError):
73-
print("Wrong input")
26+
num_1 = int(nums[0].strip())
27+
num_2 = int(nums[1].strip())
28+
29+
print(f"greatest_common_divisor({num_1}, {num_2}) = {greatest_common_divisor(num_1, num_2)}")
30+
print(f"gcd_by_iterative({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}")
31+
32+
except (IndexError, ValueError):
33+
print(" Invalid input! Please enter two valid integers separated by a comma.")
7434

7535

7636
if __name__ == "__main__":

0 commit comments

Comments
 (0)