Skip to content

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

Open
Bhumit267 wants to merge 1 commit into
TheAlgorithms:masterfrom
Bhumit267:fix/catalan-numbers-factorial-overflow
Open

Bhumit267 wants to merge 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

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.27%. Comparing base (8b19b87) to head (381d2a9).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7732      +/-   ##
============================================
- Coverage     82.28%   82.27%   -0.02%     
+ Complexity     8414     8412       -2     
============================================
  Files           843      843              
  Lines         26150    26150              
  Branches       5104     5105       +1     
============================================
- Hits          21518    21515       -3     
  Misses         3842     3842              
- Partials        790      793       +3     

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

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