Skip to content

Fix ZeroDivisionError on zero operands in least_common_multiple#14845

Open
CharlesCNorton wants to merge 2 commits into
TheAlgorithms:masterfrom
CharlesCNorton:fix-lcm-zero-division
Open

Fix ZeroDivisionError on zero operands in least_common_multiple#14845
CharlesCNorton wants to merge 2 commits into
TheAlgorithms:masterfrom
CharlesCNorton:fix-lcm-zero-division

Conversation

@CharlesCNorton

Copy link
Copy Markdown

Describe your change:

least_common_multiple_slow and least_common_multiple_fast raise ZeroDivisionError when either operand is 0:

  • least_common_multiple_slow(0, 5), least_common_multiple_slow(5, 0), and least_common_multiple_slow(0, 0) divide by zero in the common_mult % first_num / common_mult % second_num loop guard.
  • least_common_multiple_fast(0, 0) divides by greatest_common_divisor(0, 0) == 0.

The least common multiple is 0 whenever an operand is 0, which matches the standard library (math.lcm(0, 5) == 0, math.lcm(0, 0) == 0). Both functions now return 0 in that case before the division, so they agree with math.lcm and with each other. Behavior for non-zero inputs is unchanged. Doctests for the zero cases are added.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.

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.
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Jun 22, 2026

@zain-cs zain-cs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix! The zero-check guard correctly handles all zero operand cases and matches the behavior of math.lcm. Doctests for the zero cases are well added.
One minor suggestion: consider adding the zero cases to TestLeastCommonMultiple.test_inputs as well, so the unit test class is as comprehensive as the doctests.
Otherwise looks clean and solid! 👍

@CharlesCNorton

Copy link
Copy Markdown
Author

@zain-cs Thanks, good call. Added the three zero cases to test_inputs/expected_results so the unittest class matches the doctests; both _slow and _fast are exercised on them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants