Fibonacci Series in Java
Fibonacci Series in java is a series of increasing numbers where the first two terms are 0 and 1 respectively. All other subsequent terms are obtained by adding the last two terms before them.
Mathematically,
Let f(n) returns us the nth fibonacci number
There are several methods to write the Fibonacci series. In this article, we will discuss the following methods in detail:
- Fibonacci Series in Java Using for Loop
- Fibonacci Series in Java Using while Loop
- Fibonacci Series in Java Using Iterative Approach
- Fibonacci Series in Java Using recursion
- Fibonacci Series in Java Using Dynamic Programming
Fibonacci Series in Java Using for Loop
Output:
Explanation:
The Java program calculates the nth number of the Fibonacci sequence, taking n as input from the user.
It initializes an integer array of size to store the Fibonacci sequence and uses a for loop to calculate the remaining elements. Finally, it prints the nth Fibonacci number to the console.
Time Complexity
O(N) - (Linear Time Complexity)
This is because, for finding the nth Fibonacci number, we move through an array of size n (actually, we are moving indices in the array).
Space Complexity
O(N)
Fibonacci Series in Java Using while Loop
Output:
Explanation:
The Java program calculates the nth number of the Fibonacci sequence using a while loop instead of a for loop.
It takes n as input from the user and initializes an integer array of size n to store the Fibonacci sequence. The loop condition i < n is used to calculate the remaining elements of the sequence. Finally, it prints the nth Fibonacci number to the console.
Time Complexity
O(N) - (Linear Time Complexity)
This is because, to find the nth Fibonacci number, we move through an array of size n (actually, we are moving n-2 indices in the array) using a while loop.
Space Complexity
O(N)
Fibonacci Series in Java Using Iterative Approach
Output:
Explanation: The above program generates and prints the Fibonacci series up to 15 terms using the previous two Fibonacci values iteratively.
Time Complexity
O(N) - (Linear Time Complexity)
Space Complexity
O(1)
Fibonacci Series in Java Using recursion
The function returns the nth Fibonacci number by recursively calling itself for previous values until the input becomes less than or equal to 2. By calling this function to compute each Fibonacci number, we can print the Fibonacci series.
Output:
Explanation: In the above example, we defined a recursive function fib to get nth Fibonacci number and call it repeatedly (for i = 0 to i = 8) to create a Fibonacci series of length 9.
Read to know more about Fibonacci Series Using recursion in Java
Time Complexity
The Time Complexity is , i.e. exponential time.
Space Complexity
O(1)
Join our expert-led Dynamic Programming Certification course to master advanced problem-solving techniques and enhance your programming prowess.
Fibonacci Series in Java Using Dynamic Programming
Output:
Explanation: In the above program, we use the tabulation method of dynamic programming to calculate the Fibonacci series. It stores the value of the Fibonacci term in the array and calculates further values using the previous two values.
Time Complexity
O(N)
Space Complexity
O(N)
Conclusion
The Fibonacci Series in java is a sequence of numbers in which each number (except the first two) is the sum of the two preceding numbers.
- The recursive approach uses a function that calls itself to calculate the Fibonacci numbers. It typically has a base case for the first two numbers and recursively computes subsequent numbers.
- The time complexity of the recursive approach is exponential, specifically &O(2^n)&, due to repeated function calls and redundant calculations.
- The iterative approach has a linear time complexity of O(n), where n is the desired number in the Fibonacci Series.