Skip to content

fix(maths): prevent infinite recursion on negative inputs in BinomialCoefficient - #7730

Merged
alxkm merged 1 commit into
TheAlgorithms:masterfrom
Bhumit267:fix/binomial-coefficient-negative-input
Nov 5, 2026
Merged

alxkm merged 1 commit into
TheAlgorithms:masterfrom
Bhumit267:fix/binomial-coefficient-negative-input

Conversation

@Bhumit267

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

Prevents unbounded infinite recursion leading to StackOverflowError in BinomialCoefficient.binomialCoefficient when negative arguments are passed.

Problem

In com.thealgorithms.maths.BinomialCoefficient, the recursive method:

public static int binomialCoefficient(int totalObjects, int numberOfObjects)

did not validate against negative inputs. Calling binomialCoefficient(5, -1) failed to satisfy the base cases (numberOfObjects > totalObjects is false, and equality checks fail) and recursively decremented numberOfObjects indefinitely (-2, -3, -4, ...), causing a java.lang.StackOverflowError.

Changes

  • Added input validation at the entry of binomialCoefficient to throw IllegalArgumentException when totalObjects < 0 || numberOfObjects < 0.
  • Updated Javadoc to document @throws IllegalArgumentException if totalObjects or numberOfObjects is negative.
  • Added regression tests in BinomialCoefficientTest validating that negative inputs throw IllegalArgumentException, and added boundary tests for (5, 6) and (0, 0).

Tests

  • Passed mvn test -Dtest=BinomialCoefficientTest (2 tests, 0 failures)
  • Passed mvn checkstyle:check (0 violations)

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.27%. Comparing base (4f535f5) to head (9855e08).

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

@alxkm alxkm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Thank you for the contribution.

@alxkm
alxkm merged commit 51a0156 into TheAlgorithms:master Nov 5, 2026
7 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