We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 45d11ab + 6e13fed commit be1247eCopy full SHA for be1247e
students_submissions/gcd_sk3763.py
@@ -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
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