Repository: TheAlgorithms/Java
Pull request #6079 (2024-11-01 19:20:17)

adding the fibonacci series to the Recursion package
<!--
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`


Patch:
diff --git a/src/main/java/com/thealgorithms/Recursion/FibonacciSeries.java b/src/main/java/com/thealgorithms/Recursion/FibonacciSeries.java
new file mode 100644
index 00000000..a89d110b
--- /dev/null
+++ b/src/main/java/com/thealgorithms/Recursion/FibonacciSeries.java
@@ -0,0 +1,21 @@
+package com.thealgorithms.Recursion;
+
+/*
+    The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones,
+    starting with 0 and 1.
+       NUMBER    0 1 2 3 4 5 6 7  8  9  10 ...
+       FIBONACCI 0 1 1 2 3 5 8 13 21 34 55 ...
+*/
+
+public final class FibonacciSeries {
+    private FibonacciSeries() {
+        throw new UnsupportedOperationException("Utility class");
+    }
+    public static int fibonacci(int n) {
+        if (n <= 1) {
+            return n;
+        } else {
+            return fibonacci(n - 1) + fibonacci(n - 2);
+        }
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/Recursion/FibonacciSeriesTest.java b/src/test/java/com/thealgorithms/Recursion/FibonacciSeriesTest.java
new file mode 100644
index 00000000..4e4fc458
--- /dev/null
+++ b/src/test/java/com/thealgorithms/Recursion/FibonacciSeriesTest.java
@@ -0,0 +1,27 @@
+package com.thealgorithms.Recursion;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+public class FibonacciSeriesTest {
+
+    @Test
+    public void testFibonacci() {
+        assertEquals(0, FibonacciSeries.fibonacci(0));
+        assertEquals(1, FibonacciSeries.fibonacci(1));
+        assertEquals(1, FibonacciSeries.fibonacci(2));
+        assertEquals(2, FibonacciSeries.fibonacci(3));
+        assertEquals(3, FibonacciSeries.fibonacci(4));
+        assertEquals(5, FibonacciSeries.fibonacci(5));
+        assertEquals(8, FibonacciSeries.fibonacci(6));
+        assertEquals(13, FibonacciSeries.fibonacci(7));
+        assertEquals(21, FibonacciSeries.fibonacci(8));
+        assertEquals(34, FibonacciSeries.fibonacci(9));
+        assertEquals(55, FibonacciSeries.fibonacci(10));
+        assertEquals(89, FibonacciSeries.fibonacci(11));
+        assertEquals(144, FibonacciSeries.fibonacci(12));
+        assertEquals(233, FibonacciSeries.fibonacci(13));
+        assertEquals(377, FibonacciSeries.fibonacci(14));
+    }
+}