Skip to content

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

Merged
DenizAltunkapan merged 4 commits into
TheAlgorithms:masterfrom
spidervirus:fix/pronic-number-overflow
Nov 28, 2026
Merged

DenizAltunkapan merged 4 commits 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

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.47%. Comparing base (340324c) to head (2db68e6).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7771      +/-   ##
============================================
+ Coverage     82.45%   82.47%   +0.02%     
- Complexity     8512     8515       +3     
============================================
  Files           847      847              
  Lines         26293    26295       +2     
  Branches       5134     5134              
============================================
+ Hits          21679    21686       +7     
+ Misses         3824     3823       -1     
+ Partials        790      786       -4     

☔ 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
@DenizAltunkapan
DenizAltunkapan enabled auto-merge (squash) November 28, 2026 11:48
@DenizAltunkapan
DenizAltunkapan merged commit 4ff7eaf into TheAlgorithms:master Nov 28, 2026
12 of 13 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.

3 participants