forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciSeries.java
More file actions
33 lines (30 loc) · 932 Bytes
/
FibonacciSeries.java
File metadata and controls
33 lines (30 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.
* <p>
* Example:
* 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ...
* </p>
*/
public final class FibonacciSeries {
private FibonacciSeries() {
throw new UnsupportedOperationException("Utility class");
}
/**
* Calculates the nth term in the Fibonacci sequence using recursion.
*
* @param n the position in the Fibonacci sequence (must be non-negative)
* @return the nth Fibonacci number
* @throws IllegalArgumentException if n is negative
*/
public static int fibonacci(int n) {
if (n < 0) {
throw new IllegalArgumentException("n must be a non-negative integer");
}
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}