Method Fibonacci
# Java Program to Generate Fibonacci Sequence
The Fibonacci sequence is a classic mathematical sequence where each number (known as a Fibonacci number) is the sum of the two preceding ones.
The sequence typically starts as follows:
$$0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, \dots$$
### Mathematical Definition
* **$F(0) = 0$** (The 0th term is 0)
* **$F(1) = 1$** (The 1st term is 1)
* **$F(n) = F(n-1) + F(n-2)$** for $n \ge 2$
---
## Implementation Methods
In Java, the Fibonacci sequence can be implemented using several approaches. Below, we explore the classic **Recursive Approach** (as shown in the original example), followed by more efficient alternatives like the **Iterative Approach** and **Memoization**.
### 1. Recursive Approach (Standard)
This approach uses a recursive method to calculate the $n$-th Fibonacci number by calling itself for the two preceding values.
#### Code Example: `MainClass.java`
```java
public class MainClass {
public static void main(String[] args) {
// Print the Fibonacci numbers from index 0 to 10
for (int counter = 0; counter <= 10; counter++) {
System.out.printf("Fibonacci of %d is: %d\n", counter, fibonacci(counter));
}
}
/**
* Calculates the n-th Fibonacci number using recursion.
*
* @param number The index of the Fibonacci sequence to retrieve.
* @return The Fibonacci number at the specified index.
*/
public static long fibonacci(long number) {
// Base cases: F(0) = 0, F(1) = 1
if ((number == 0) || (number == 1)) {
return number;
} else {
// Recursive step: F(n) = F(n-1) + F(n-2)
return fibonacci(number - 1) + fibonacci(number - 2);
}
}
}
```
#### Output
```text
Fibonacci of 0 is: 0
Fibonacci of 1 is: 1
Fibonacci of 2 is: 1
Fibonacci of 3 is: 2
Fibonacci of 4 is: 3
Fibonacci of 5 is: 5
Fibonacci of 6 is: 8
Fibonacci of 7 is: 13
Fibonacci of 8 is: 21
Fibonacci of 9 is: 34
Fibonacci of 10 is: 55
```
---
### 2. Iterative Approach (Highly Efficient)
While recursion is elegant, the standard recursive approach has an exponential time complexity of $O(2^n)$ because it recalculates the same subproblems multiple times.
An **iterative approach** runs in linear time $O(n)$ and uses $O(1)$ auxiliary space, making it much more suitable for larger values of $n$.
#### Code Example
```java
public class FibonacciIterative {
public static void main(String[] args) {
int n = 10;
System.out.println("Fibonacci sequence up to " + n + ":");
printFibonacciSequence(n);
}
public static void printFibonacciSequence(int limit) {
long first = 0, second = 1;
for (int i = 0; i <= limit; i++) {
System.out.printf("Fibonacci of %d is: %d\n", i, first);
// Compute the next term
long next = first + second;
first = second;
second = next;
}
}
}
```
---
## Performance Comparison & Considerations
| Metric | Recursive Approach | Iterative Approach |
| :--- | :--- | :--- |
| **Time Complexity** | $O(2^n)$ (Exponential) | $O(n)$ (Linear) |
| **Space Complexity** | $O(n)$ (Due to call stack) | $O(1)$ (Constant) |
| **Stack Overflow Risk** | High (for large $n$) | None |
| **Best Used For** | Educational purposes, small $n$ | Production environments, large $n$ |
### Key Considerations
1. **Integer Overflow**: Fibonacci numbers grow extremely fast.
* A standard 32-bit signed `int` will overflow after $F(46)$ (which is $2,971,215,073$, exceeding `Integer.MAX_VALUE`).
* A 64-bit signed `long` will overflow after $F(92)$.
* For calculating values beyond $F(92)$, use Java's `java.math.BigInteger` class to prevent arithmetic overflow.
2. **Recursion Limit**: Avoid using the basic recursive method for $n > 40$, as the execution time will increase dramatically and may cause a `StackOverflowError`.
YouTip