Skip to content

Commit be1247e

Browse files
Merge pull request #137 from Sharu-K11/sk3763-gcd-feature
Implemented GCD function
2 parents 45d11ab + 6e13fed commit be1247e

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

students_submissions/gcd_sk3763.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def gcd(a: int, b: int) -> int | None:
2+
"""
3+
Calculate the greatest common divisor (GCD) of two integers a and b
4+
using the Euclidean algorithm (recursively).
5+
Handles negative inputs and edge cases.
6+
"""
7+
if not isinstance(a, int) or not isinstance(b, int):
8+
print("Error: Both inputs must be integers.")
9+
return None
10+
11+
a, b = abs(a), abs(b)
12+
13+
if a == 0 and b == 0:
14+
print("Error: GCD is undefined for both numbers being zero.")
15+
return None
16+
17+
if a == 0:
18+
return b
19+
return gcd(b % a, a)
20+
21+
22+
# Test cases
23+
print(gcd(54, 24)) # Expected output: 6
24+
print(gcd(48, 18)) # Expected output: 6
25+
print(gcd(101, 10)) # Expected output: 1

0 commit comments

Comments
 (0)