Skip to content

fix(maths): prevent integer overflow and optimize loop in PronicNumber - #7771

Open
spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/pronic-number-overflow
Open

spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/pronic-number-overflow

Conversation

@spidervirus

Copy link
Copy Markdown
Contributor
  • 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

Description

In com.thealgorithms.maths.PronicNumber:

  1. In isPronic(int inputNumber), the loop for (int i = 0; i <= inputNumber; i++) checked i * (i + 1) == inputNumber.
    For values where \ge 46,341$, i * (i + 1) overflows 32-bit signed int into negative numbers.
    Additionally, running up to $ takes (N)$ operations, which hangs on large values.
  2. The check i != inputNumber was an unnecessary workaround for zsh$, and negative input numbers were not explicitly guarded against.
  3. isPronic was package-private rather than public.

Fix

  • Bounded the loop in isPronic with (long) i * (i + 1) <= inputNumber, reducing time complexity from (N)$ to (\sqrt{N})$ and avoiding 32-bit integer overflow.
  • Added explicit negative checks (inputNumber < 0).
  • Made isPronic public.
  • Added tests in PronicNumberTest for large pronic numbers (up to the max 32-bit pronic number 2147441940), Integer.MAX_VALUE, and negative numbers.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.45%. Comparing base (e79b0f1) to head (fb44fad).
⚠️ Report is 1 commits behind head on master.

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.
📢 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.

@spidervirus
spidervirus marked this pull request as ready for review November 19, 2026 11:55
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.

2 participants