Skip to content

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 8 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/main/java/com/thealgorithms/maths/CatalanNumbers.java
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 src/test/java/com/thealgorithms/maths/CatalanNumbersTest.java
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)));
}
}