Repository: TheAlgorithms/Java
Pull request #5846 (2024-10-15 09:43:50)

Implement a function to compute the nth Catalan number
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

UTSAVS26: @saahil-mahato changes made as per requested, thank you for your time.
UTSAVS26: @alxkm please review my PR.

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/CatalanNumbers.java b/src/main/java/com/thealgorithms/maths/CatalanNumbers.java
new file mode 100644
index 00000000..387756bd
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/CatalanNumbers.java
@@ -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;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/CatalanNumbersTest.java b/src/test/java/com/thealgorithms/maths/CatalanNumbersTest.java
new file mode 100644
index 00000000..2248ffd9
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/CatalanNumbersTest.java
@@ -0,0 +1,43 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.stream.Stream;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+/**
+ * Test class for CatalanNumbers
+ */
+class CatalanNumbersTest {
+
+    /**
+     * Provides test data for the parameterized Catalan number test.
+     * Each array contains two elements:
+     * [input number, expected Catalan number for that input]
+     */
+    static Stream<Object[]> catalanNumbersProvider() {
+        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});
+    }
+
+    /**
+     * Parameterized test for checking the correctness of Catalan numbers.
+     * Uses the data from the provider method 'catalanNumbersProvider'.
+     */
+    @ParameterizedTest
+    @MethodSource("catalanNumbersProvider")
+    void testCatalanNumbers(int input, int expected) {
+        assertEquals(expected, CatalanNumbers.catalan(input), () -> String.format("Catalan number for input %d should be %d", input, expected));
+    }
+
+    /**
+     * Test for invalid inputs which should throw an IllegalArgumentException.
+     */
+    @Test
+    void testIllegalInput() {
+        assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-1));
+        assertThrows(IllegalArgumentException.class, () -> CatalanNumbers.catalan(-5));
+    }
+}