Repository navigation
fix(maths): prevent 32-bit integer overflow in PythagoreanTriple - #7769
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 #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. 🚀 New features to boost your workflow:
|
spidervirus
force-pushed
the
fix/pythagorean-triple-overflow
branch
from
November 19, 2026 11:54
b22ab2a to
947e91d
Compare
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
spidervirus
force-pushed
the
fix/pythagorean-triple-overflow
branch
from
November 19, 2026 11:56
947e91d to
2a0b923
Compare
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$\approx 46,340$ , the intermediate squares overflow 32-bit
com.thealgorithms.maths.PythagoreanTriple.isPythagTriple(int a, int b, int c):The check
x * x + y * y == hypotenuse * hypotenuseperforms calculations using 32-bit signed integers.When , y$ or $ exceed
int(Integer.MAX_VALUE = 2,147,483,647).This produces silent overflow and severe false positives:
isPythagTriple(65536, 65536, 65536): ^2 = 2^{32} \equiv 0 \pmod{2^{32}}$, so0 + 0 == 0evaluates totrue, falsely identifying an equilateral triangle as a Pythagorean triple.(30000, 40000, 50000)also experience intermediate integer overflow.Fix
longbefore squaring and summing: for 32-bit integer inputs, ^2 \approx 4 \cdot 10^{18}$, which safely fits into 64-bit signedlong(Long.MAX_VALUE \approx 9.22 \cdot 10^{18}).PythagoreanTripleTestfor large true triples, near misses, and false-positive edge cases like(65536, 65536, 65536).