Skip to content

fix(maths): use overflow-safe Euclidean GCD in LeastCommonMultiple - #7795

Merged
DenizAltunkapan merged 3 commits into
TheAlgorithms:masterfrom
mayankcodes-dev:fix/lcm-euclidean-overflow-safe
Dec 2, 2026
Merged

DenizAltunkapan merged 3 commits into
TheAlgorithms:masterfrom
mayankcodes-dev:fix/lcm-euclidean-overflow-safe

Conversation

@mayankcodes-dev

@mayankcodes-dev mayankcodes-dev commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Describe your change:

Replaces the O(lcm / max) brute-force addition loop in LeastCommonMultiple with an O(log(min(a, b))) overflow-safe Euclidean GCD implementation, matching the standard C++17 std::lcm contract.

Bugs in the previous implementation:

LeastCommonMultiple.lcm(10, 0);        // throws ArithmeticException: / by zero (while num1 != 0 evaluates high % num2)
LeastCommonMultiple.lcm(-12, 18);      // mishandles negative inputs
LeastCommonMultiple.lcm(60_000, 90_000); // loops tens of thousands of iterations; naive (a * b) / gcd overflows int

Updated behavior & additions:

LeastCommonMultiple.lcm(10, 0);                 // 0
LeastCommonMultiple.lcm(-12, 18);               // 36
LeastCommonMultiple.lcm(60_000, 90_000);        // 180_000 (divides before multiplying, no intermediate overflow)
LeastCommonMultiple.lcm(50_000, 50_001);        // throws ArithmeticException instead of silent wrap-around
LeastCommonMultiple.lcm(4, 6, 8, 12);           // 24 (int... varargs)
LeastCommonMultiple.lcm(2_000_000_000_000L, -3_000_000_000_000L); // 6_000_000_000_000L (long & long...)
LeastCommonMultiple.lcm(BigInteger.valueOf(Long.MAX_VALUE), BigInteger.valueOf(Long.MAX_VALUE - 1));
  • Preserves all 10 existing parameterized cases in LeastCommonMultipleTest and adds full coverage for zeros, negative inputs, Integer.MIN_VALUE / Long.MIN_VALUE overflow checks, varargs, and BigInteger.
  • Verified locally with javac --release 21 -Xlint:all -Werror, JUnit 5, Checkstyle, and clang-format 16.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Dec 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.97%. Comparing base (972969c) to head (0263d07).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7795      +/-   ##
============================================
+ Coverage     82.94%   82.97%   +0.03%     
- Complexity     8643     8677      +34     
============================================
  Files           848      848              
  Lines         26455    26507      +52     
  Branches       5160     5172      +12     
============================================
+ Hits          21943    21995      +52     
- Misses         3725     3726       +1     
+ Partials        787      786       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 18:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@DenizAltunkapan DenizAltunkapan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@DenizAltunkapan
DenizAltunkapan merged commit 71ccf32 into TheAlgorithms:master Dec 2, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants