Skip to content

fix(maths): prevent intermediate integer overflow in CatalanNumbers - #7732

Merged
alxkm merged 1 commit into
TheAlgorithms:masterfrom
Bhumit267:fix/catalan-numbers-factorial-overflow
Nov 8, 2026
Merged

alxkm merged 1 commit into
TheAlgorithms:masterfrom
Bhumit267:fix/catalan-numbers-factorial-overflow

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

In CatalanNumbers.java, catalan(n) computes the $n$-th Catalan number using the factorial formula:
$$\frac{(2n)!}{(n+1)! \cdot n!}$$

Previously, factorial(int n) used standard 64-bit signed long arithmetic. Because $21!$ and higher overflow 64-bit long (where 20! is $\approx 2.43 \times 10^{18}$ and Long.MAX_VALUE is $\approx 9.22 \times 10^{18}$), computing $(2n)!$ for any $n \ge 11$ ($2n \ge 22$) caused silent integer overflow, resulting in incorrect and negative results (e.g. catalan(11) evaluated to -65 instead of 58786).

Changes

  • Updated factorial(int n) to use BigInteger internally to prevent intermediate integer overflow during multiplication.
  • Kept the public API signature public static long catalan(final int n) unchanged.
  • Added validation for $n > 35$ (throwing IllegalArgumentException) because $C_{35} = 3,116,285,494,907,301,262$ is the maximum Catalan number representable within a signed 64-bit long ($C_{36} > \text{Long.MAX_VALUE}$).
  • Updated CatalanNumbersTest.java with test cases for $n = 11$, $n = 15$, and boundary value $n = 35$, along with testing invalid inputs ($n = 36$).

Verification

  • mvn test -Dtest=CatalanNumbersTest passes (15/15 tests passing).
  • mvn checkstyle:check passes with 0 violations.

@codecov-commenter

codecov-commenter commented Nov 6, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.33%. Comparing base (1e59594) to head (8d47d21).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #7732   +/-   ##
=========================================
  Coverage     82.33%   82.33%           
+ Complexity     8445     8443    -2     
=========================================
  Files           843      843           
  Lines         26154    26154           
  Branches       5108     5109    +1     
=========================================
  Hits          21535    21535           
  Misses         3830     3830           
  Partials        789      789           

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

@Bhumit267
Bhumit267 force-pushed the fix/catalan-numbers-factorial-overflow branch from 381d2a9 to 8d47d21 Compare November 8, 2026 05:28

@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. Thanks for the contribution.

@alxkm
alxkm merged commit dbcf156 into TheAlgorithms:master Nov 8, 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