From fc8fcc351ca2b902777e588ac1df0d8902ffede4 Mon Sep 17 00:00:00 2001 From: Kris Date: Thu, 5 Feb 2026 18:50:28 -0500 Subject: [PATCH] Implement GCD function --- students_submissions/gcd_cs727.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 students_submissions/gcd_cs727.py diff --git a/students_submissions/gcd_cs727.py b/students_submissions/gcd_cs727.py new file mode 100644 index 0000000..4fcb8bf --- /dev/null +++ b/students_submissions/gcd_cs727.py @@ -0,0 +1,24 @@ +def gcd(a: int, b: int) -> int: + # Type checking + if not isinstance(a, int) or not isinstance(b, int): + print("Error: Both arguments must be integers.") + return None + + # Handle the undefined case gcd(0, 0) + if a == 0 and b == 0: + print("Error: GCD is undefined for a = 0 and b = 0.") + return None + + # Work with absolute values to handle negatives + a, b = abs(a), abs(b) + + # Base case: gcd(a, 0) = a + if b == 0: + return a + + # Recursive Euclidean Algorithm + return gcd(b, a % b) + +print(gcd(54, 24)) +print(gcd(48, 18)) +print(gcd(101, 10))