Fix ZeroDivisionError on zero operands in least_common_multiple#14845
Open
CharlesCNorton wants to merge 1 commit into
Open
Fix ZeroDivisionError on zero operands in least_common_multiple#14845CharlesCNorton wants to merge 1 commit into
CharlesCNorton wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
least_common_multiple_slowandleast_common_multiple_fastraiseZeroDivisionErrorwhen either operand is0:least_common_multiple_slow(0, 5),least_common_multiple_slow(5, 0), andleast_common_multiple_slow(0, 0)divide by zero in thecommon_mult % first_num/common_mult % second_numloop guard.least_common_multiple_fast(0, 0)divides bygreatest_common_divisor(0, 0) == 0.The least common multiple is
0whenever an operand is0, which matches the standard library (math.lcm(0, 5) == 0,math.lcm(0, 0) == 0). Both functions now return0in that case before the division, so they agree withmath.lcmand with each other. Behavior for non-zero inputs is unchanged. Doctests for the zero cases are added.Checklist: