Repository navigation
fix(maths): prevent integer overflow and optimize loop in PronicNumber - #7771
Open
spidervirus wants to merge 1 commit into
Open
spidervirus wants to merge 1 commit into
spidervirus wants to merge 1 commit into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7771 +/- ##
============================================
+ Coverage 82.42% 82.45% +0.02%
- Complexity 8508 8512 +4
============================================
Files 847 847
Lines 26289 26293 +4
Branches 5133 5134 +1
============================================
+ Hits 21670 21680 +10
+ Misses 3828 3825 -3
+ Partials 791 788 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
spidervirus
marked this pull request as ready for review
November 19, 2026 11:55
spidervirus
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 19, 2026 11:55
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.
clang-format -i --style=file path/to/your/file.javaDescription
In
com.thealgorithms.maths.PronicNumber:isPronic(int inputNumber), the loopfor (int i = 0; i <= inputNumber; i++)checkedi * (i + 1) == inputNumber.For values where \ge 46,341$,
i * (i + 1)overflows 32-bit signedintinto negative numbers.Additionally, running up to $ takes (N)$ operations, which hangs on large values.
i != inputNumberwas an unnecessary workaround for zsh$, and negative input numbers were not explicitly guarded against.isPronicwas package-private rather thanpublic.Fix
isPronicwith(long) i * (i + 1) <= inputNumber, reducing time complexity from (N)$ to (\sqrt{N})$ and avoiding 32-bit integer overflow.inputNumber < 0).isPronicpublic.PronicNumberTestfor large pronic numbers (up to the max 32-bit pronic number2147441940),Integer.MAX_VALUE, and negative numbers.