Skip to content

fix(maths): prevent 32-bit integer overflow in PythagoreanTriple - #7769

Open
spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/pythagorean-triple-overflow
Open

spidervirus wants to merge 1 commit into
TheAlgorithms:masterfrom
spidervirus:fix/pythagorean-triple-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.PythagoreanTriple.isPythagTriple(int a, int b, int c):
The check x * x + y * y == hypotenuse * hypotenuse performs calculations using 32-bit signed integers.
When , y$ or $ exceed $\approx 46,340$, the intermediate squares overflow 32-bit int (Integer.MAX_VALUE = 2,147,483,647).

This produces silent overflow and severe false positives:

  • For example, isPythagTriple(65536, 65536, 65536): ^2 = 2^{32} \equiv 0 \pmod{2^{32}}$, so 0 + 0 == 0 evaluates to true, falsely identifying an equilateral triangle as a Pythagorean triple.
  • Legitimate large Pythagorean triples like (30000, 40000, 50000) also experience intermediate integer overflow.

Fix

  • Cast values to long before squaring and summing: for 32-bit integer inputs, ^2 \approx 4 \cdot 10^{18}$, which safely fits into 64-bit signed long (Long.MAX_VALUE \approx 9.22 \cdot 10^{18}).
  • Added tests in PythagoreanTripleTest for large true triples, near misses, and false-positive edge cases like (65536, 65536, 65536).

@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.45%. Comparing base (e79b0f1) to head (2a0b923).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7769      +/-   ##
============================================
+ Coverage     82.42%   82.45%   +0.02%     
- Complexity     8508     8512       +4     
============================================
  Files           847      847              
  Lines         26289    26291       +2     
  Branches       5133     5134       +1     
============================================
+ Hits          21670    21677       +7     
+ Misses         3828     3825       -3     
+ Partials        791      789       -2     

☔ 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 force-pushed the fix/pythagorean-triple-overflow branch from b22ab2a to 947e91d Compare November 19, 2026 11:54
@spidervirus
spidervirus marked this pull request as ready for review November 19, 2026 11:55
@spidervirus
spidervirus force-pushed the fix/pythagorean-triple-overflow branch from 947e91d to 2a0b923 Compare November 19, 2026 11:56
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