From 9d7733f0e44d1aa84d8241e07a2fdfd8e17ca589 Mon Sep 17 00:00:00 2001 From: CharlesCNorton <135471798+CharlesCNorton@users.noreply.github.com> Date: Mon, 22 Jun 2026 10:19:08 -0400 Subject: [PATCH 1/2] Fix ZeroDivisionError on zero operands in least_common_multiple least_common_multiple_slow raised ZeroDivisionError for a zero operand (0 % n in the loop guard), e.g. least_common_multiple_slow(0, 5), and least_common_multiple_fast raised it for (0, 0) via floor-division by gcd(0, 0) == 0. The least common multiple is 0 whenever an operand is 0 (consistent with math.lcm), so both functions return 0 in that case. Adds doctests covering the zero inputs. --- maths/least_common_multiple.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/maths/least_common_multiple.py b/maths/least_common_multiple.py index a5c4bf8e3625..5e4f03988333 100644 --- a/maths/least_common_multiple.py +++ b/maths/least_common_multiple.py @@ -14,7 +14,15 @@ def least_common_multiple_slow(first_num: int, second_num: int) -> int: 10 >>> least_common_multiple_slow(12, 76) 228 + >>> least_common_multiple_slow(0, 5) + 0 + >>> least_common_multiple_slow(5, 0) + 0 + >>> least_common_multiple_slow(0, 0) + 0 """ + if first_num == 0 or second_num == 0: + return 0 max_num = first_num if first_num >= second_num else second_num common_mult = max_num while (common_mult % first_num > 0) or (common_mult % second_num > 0): @@ -30,7 +38,15 @@ def least_common_multiple_fast(first_num: int, second_num: int) -> int: 10 >>> least_common_multiple_fast(12,76) 228 + >>> least_common_multiple_fast(0, 5) + 0 + >>> least_common_multiple_fast(5, 0) + 0 + >>> least_common_multiple_fast(0, 0) + 0 """ + if first_num == 0 or second_num == 0: + return 0 return first_num // greatest_common_divisor(first_num, second_num) * second_num From 3fd97356f6acb62347262d2eb95b047a659a9c2b Mon Sep 17 00:00:00 2001 From: CharlesCNorton Date: Thu, 25 Jun 2026 07:09:57 -0400 Subject: [PATCH 2/2] Add zero-operand cases to least_common_multiple unit test --- maths/least_common_multiple.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/maths/least_common_multiple.py b/maths/least_common_multiple.py index 5e4f03988333..425cbf528ec3 100644 --- a/maths/least_common_multiple.py +++ b/maths/least_common_multiple.py @@ -75,8 +75,11 @@ class TestLeastCommonMultiple(unittest.TestCase): (12, 25), (10, 25), (6, 9), + (0, 5), + (5, 0), + (0, 0), ) - expected_results = (20, 195, 124, 210, 1462, 60, 300, 50, 18) + expected_results = (20, 195, 124, 210, 1462, 60, 300, 50, 18, 0, 0, 0) def test_lcm_function(self): for i, (first_num, second_num) in enumerate(self.test_inputs):