-
Notifications
You must be signed in to change notification settings - Fork 20k
Implement a function to compute the nth Catalan number #5846
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2500ee0
feature/CatalanNumbers
UTSAVS26 7bebdb7
Update CatalanNumbers.java
UTSAVS26 4f5ced5
Update CatalanNumbersTest.java
UTSAVS26 f36198d
Clang format linter / build (pull_request) resolved
UTSAVS26 5094289
Merge branch 'master' into feature/CatalanNumbers
UTSAVS26 e609cae
Update CatalanNumbersTest.java
UTSAVS26 4935ddb
Changes
UTSAVS26 931f9de
Merge branch 'master' into feature/CatalanNumbers
UTSAVS26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.thealgorithms.maths; | ||
|
||
/** | ||
* Calculate Catalan Numbers | ||
*/ | ||
public final class CatalanNumbers { | ||
private CatalanNumbers() { | ||
} | ||
|
||
/** | ||
* Calculate the nth Catalan number using a recursive formula. | ||
* | ||
* @param n the index of the Catalan number to compute | ||
* @return the nth Catalan number | ||
*/ | ||
public static long catalan(final int n) { | ||
if (n < 0) { | ||
throw new IllegalArgumentException("Index must be non-negative"); | ||
} | ||
return factorial(2 * n) / (factorial(n + 1) * factorial(n)); | ||
} | ||
|
||
/** | ||
* Calculate the factorial of a number. | ||
* | ||
* @param n the number to compute the factorial for | ||
* @return the factorial of n | ||
*/ | ||
private static long factorial(final int n) { | ||
if (n == 0 || n == 1) { | ||
return 1; | ||
} | ||
long result = 1; | ||
for (int i = 2; i <= n; i++) { | ||
result *= i; | ||
} | ||
return result; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/com/thealgorithms/maths/CatalanNumbersTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.thealgorithms.maths; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Test class for CatalanNumbers | ||
*/ | ||
class CatalanNumbersTest { | ||
|
||
@Test | ||
void testCatalanNumbers() { | ||
assertEquals(1, CatalanNumbers.catalan(0)); // C(0) = 1 | ||
assertEquals(1, CatalanNumbers.catalan(1)); // C(1) = 1 | ||
assertEquals(2, CatalanNumbers.catalan(2)); // C(2) = 2 | ||
assertEquals(5, CatalanNumbers.catalan(3)); // C(3) = 5 | ||
assertEquals(14, CatalanNumbers.catalan(4)); // C(4) = 14 | ||
assertEquals(42, CatalanNumbers.catalan(5)); // C(5) = 42 | ||
assertEquals(132, CatalanNumbers.catalan(6)); // C(6) = 132 | ||
assertEquals(429, CatalanNumbers.catalan(7)); // C(7) = 429 | ||
assertEquals(1430, CatalanNumbers.catalan(8)); // C(8) = 1430 | ||
assertEquals(4862, CatalanNumbers.catalan(9)); // C(9) = 4862 | ||
assertEquals(16796, CatalanNumbers.catalan(10)); // C(10) = 16796 | ||
} | ||
|
||
@Test | ||
void testIllegalInput() { | ||
assertAll(() -> assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-1)), () -> assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-5))); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.