fix: integer overflow in MobiusFunction - #7574
Open
SEPURI-SAI-KRISHNA wants to merge 1 commit into
Open
Conversation
SEPURI-SAI-KRISHNA
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
August 15, 2026 17:14
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7574 +/- ##
=========================================
Coverage 80.42% 80.43%
Complexity 7460 7460
=========================================
Files 815 815
Lines 24056 24059 +3
Branches 4733 4734 +1
=========================================
+ Hits 19348 19351 +3
Misses 3945 3945
Partials 763 763 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Problem
MobiusFunction.mobiusdecides whether a prime factor is repeated withnumber % (i * i) == 0, whereiis anint. The product overflows fori > 46340, and the wrapped value can still dividenumber, which makes the method report a squared prime factor that does not exist.The worst case is
Integer.MAX_VALUE, whose square wraps to exactly1. Since every number is divisible by 1, the method returns0for it:Separately, the loop
for (int i = 1; i <= number; i++)callsPrimeCheck.isPrime(i)on every value up tonumber, so the method isO(n * sqrt(n)).mobius(2147483647)performs on the order of 2^31 iterations, each with a primality check.Fix
Replaced the scan over all candidates with ordinary trial division that divides each prime factor out of a running remainder:
(long) factor * factor <= remaining, widened tolongso it cannot overflow nearInteger.MAX_VALUE;0;1or a single prime larger than the square root, and is counted once.This removes the overflow and drops the complexity from
O(n * sqrt(n))toO(sqrt(n))—mobius(Integer.MAX_VALUE)now returns immediately. The explicitnumber == 1early return is no longer needed: the loop does not execute and the factor count of0yields1, which is the same result.Behaviour is otherwise unchanged, including the
IllegalArgumentExceptionfor non-positive input.Tests
MobiusFunctionTestpreviously only checked 1..100, all of which are well below the overflow threshold. Added a parameterized case over large inputs, includingInteger.MAX_VALUEand its neighbours, perfect powers of two,46340^2and large primes.mobius(2147483647)fails on the old code.clang-format -i --style=file path/to/your/file.java