Skip to content

Commit 1577ec4

Browse files
authored
Add Catalan number (#5846)
1 parent 70adf6f commit 1577ec4

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.thealgorithms.maths;
2+
3+
/**
4+
* Calculate Catalan Numbers
5+
*/
6+
public final class CatalanNumbers {
7+
private CatalanNumbers() {
8+
}
9+
10+
/**
11+
* Calculate the nth Catalan number using a recursive formula.
12+
*
13+
* @param n the index of the Catalan number to compute
14+
* @return the nth Catalan number
15+
*/
16+
public static long catalan(final int n) {
17+
if (n < 0) {
18+
throw new IllegalArgumentException("Index must be non-negative");
19+
}
20+
return factorial(2 * n) / (factorial(n + 1) * factorial(n));
21+
}
22+
23+
/**
24+
* Calculate the factorial of a number.
25+
*
26+
* @param n the number to compute the factorial for
27+
* @return the factorial of n
28+
*/
29+
private static long factorial(final int n) {
30+
if (n == 0 || n == 1) {
31+
return 1;
32+
}
33+
long result = 1;
34+
for (int i = 2; i <= n; i++) {
35+
result *= i;
36+
}
37+
return result;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.thealgorithms.maths;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.util.stream.Stream;
7+
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.MethodSource;
10+
11+
/**
12+
* Test class for CatalanNumbers
13+
*/
14+
class CatalanNumbersTest {
15+
16+
/**
17+
* Provides test data for the parameterized Catalan number test.
18+
* Each array contains two elements:
19+
* [input number, expected Catalan number for that input]
20+
*/
21+
static Stream<Object[]> catalanNumbersProvider() {
22+
return Stream.of(new Object[] {0, 1}, new Object[] {1, 1}, new Object[] {2, 2}, new Object[] {3, 5}, new Object[] {4, 14}, new Object[] {5, 42}, new Object[] {6, 132}, new Object[] {7, 429}, new Object[] {8, 1430}, new Object[] {9, 4862}, new Object[] {10, 16796});
23+
}
24+
25+
/**
26+
* Parameterized test for checking the correctness of Catalan numbers.
27+
* Uses the data from the provider method 'catalanNumbersProvider'.
28+
*/
29+
@ParameterizedTest
30+
@MethodSource("catalanNumbersProvider")
31+
void testCatalanNumbers(int input, int expected) {
32+
assertEquals(expected, CatalanNumbers.catalan(input), () -> String.format("Catalan number for input %d should be %d", input, expected));
33+
}
34+
35+
/**
36+
* Test for invalid inputs which should throw an IllegalArgumentException.
37+
*/
38+
@Test
39+
void testIllegalInput() {
40+
assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-1));
41+
assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-5));
42+
}
43+
}

0 commit comments

Comments
 (0)