Repository navigation
fix(maths): prevent infinite recursion on negative inputs in BinomialCoefficient - #7730
Merged
alxkm merged 1 commit intoNov 5, 2026
Conversation
Bhumit267
requested review from
DenizAltunkapan,
alxkm and
yanglbme
as code owners
November 5, 2026 12:53
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #7730 +/- ##
=========================================
Coverage 82.26% 82.27%
- Complexity 8408 8410 +2
=========================================
Files 843 843
Lines 26142 26144 +2
Branches 5101 5102 +1
=========================================
+ Hits 21507 21510 +3
+ Misses 3843 3842 -1
Partials 792 792 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
alxkm
approved these changes
Nov 5, 2026
alxkm
left a comment
Member
There was a problem hiding this comment.
Looks good. Thank you for the contribution.
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
Prevents unbounded infinite recursion leading to
StackOverflowErrorinBinomialCoefficient.binomialCoefficientwhen negative arguments are passed.Problem
In
com.thealgorithms.maths.BinomialCoefficient, the recursive method:did not validate against negative inputs. Calling
binomialCoefficient(5, -1)failed to satisfy the base cases (numberOfObjects > totalObjectsis false, and equality checks fail) and recursively decrementednumberOfObjectsindefinitely (-2, -3, -4, ...), causing ajava.lang.StackOverflowError.Changes
binomialCoefficientto throwIllegalArgumentExceptionwhentotalObjects < 0 || numberOfObjects < 0.@throws IllegalArgumentException if totalObjects or numberOfObjects is negative.BinomialCoefficientTestvalidating that negative inputs throwIllegalArgumentException, and added boundary tests for(5, 6)and(0, 0).Tests
mvn test -Dtest=BinomialCoefficientTest(2 tests, 0 failures)mvn checkstyle:check(0 violations)