3 Different ways to print the Fibonacci series in Java
The concept of Fibonacci Series needs no introduction. Every established or a budding programmer has to go through this concept not once but multiple times.
Fibonacci Series is simply defined as a sequence of numbers determined by adding the two previous terms present in the series keeping 0 and 1 as the starting two terms. There can be more than one method to find and print the Fibonacci series.
Well, if you are still confused about the Fibonacci series and how you can print a Fibonacci series in Java, we have got you covered.
Here, we have discussed all the methods that you can use to find a Fibonacci series in Java.
Understanding the Fibonacci Series
Fibonacci series is a sequence of numbers in which the first two elements are 0 and 1. The sequence is continued by summing up the previous two terms present in the series before the current element.
Mathematically, you can find a Fibonacci series using the following formula:
F(n)= F(n-1) + F(n-2)
Here, F(0)= 0 and F(1)= 1. So, you will have to find the Fibonacci series from 2 to n.
To understand the Fibonacci series better, let’s consider the following example:
You are given the following input n=5.
The output of this program will be 0 1 1 2 3
Explanation:
0+1= 1
1+1= 2
2+1= 3
After knowing the basic term, you should know how you can print the Fibonacci Series in Java.
Methods To Print Fibonacci Series In Java
There are three different methods that can be used to print the Fibonacci series in Java. These include
Iterative Methods
With The Help of Recursion
Using Dynamic Programming
Let’s explore each of these methods in detail.
Method 1: Iterative Method
This is one of the basic methods that you can use to print the Fibonacci series in Java. Initialise the first two numbers as 0 and 1. After this, you will have to print these two numbers.
Then, begin the iterative while loop in which we will get the next occurring number by summing up the previous two numbers. At the same time, we have to swap the first and second numbers and the second and third numbers.
Complexity Analysis
Time Complexity: The time complexity of this method is calculated as O(N).
Space complexity: The space complexity of this method is calculated as O(1).
Method 2: With The Help Of Recursion
Another method that we can use to print the Fibonacci series in Java is using recursion. Now that we are aware that a Fibonacci series is the summation of the previously existing number, we will have to use this fact and implement recursion in the following way:
To begin with, you will have to get the number for which you need to calculate the Fibonacci series.
Now you need to recursively iterate the values from N to 1 where you need to consider the following cases:
Base cases: In case the recursion value is smaller than 1, the function will return 1.
Recursive Calls: In case the base condition is not fulfilled, you will have to recursively call the previous values like: recursive(n-1) + recursive(n-2).
Return Statements: After every recursive call, you will have to return the previous two values as: recursive(n-1) + recursive(n-2).
Complexity Analysis
Time Complexity: The time complexity of this method is calculated as O(2^N).
Space complexity: The space complexity of this method is calculated as O(1).
Method 3: Dynamic Programming
With the methods mentioned above, the time complexity increases as we have to run the same code again and again. To avoid repeating the same code, we can use dynamic programming.
To implement dynamic programming, we have to follow the following steps:
Initialise and declare an array Ar of size N.
Next, initialize ar[0]= 0 and ar[1]= 1.
When done, you need to iterate the array from 2 to N and keep updating the array as
ar[i]= ar[i-2] + ar[i-1].
In the end, return the value of ar[n].
Complexity Analysis
Time Complexity: The time complexity of this method is calculated as O(N).
Space complexity: The space complexity of this method is calculated as O(N).
These were the top 3 methods that you can use to print the Fibonacci series in Java. Now that we are familiar with the concept of the Fibonacci series and how to print in Java, another most commonly asked question is to write a Fibonacci series program in Python.
Can you follow the same methods in Python as well? Are the same logics applicable in Python as well?
If you are also looking for the answers to these questions, let’s check out how to write a Fibonacci series program in Python.
How to Write A Fibonacci Series Program In Python?
When it comes to Python, we can use three different methods to print a Fibonacci series which include:
Using recursion
Using dynamic programming
And, using a while loop.
With Python, dynamic programming is often used to print a Fibonacci series. Though, at the same time, it is more time and space-consuming.
However, you can still choose a simpler method like using a while loop to find a Fibonacci series as it is quite a space-efficient method.
Conclusion
The Fibonacci series may seem like a simple series of numbers but may need a lot of logic for implementation. However, in Java, you can use three methods to write the Fibonacci series that include dynamic programming, recursion and an iterative method.
Each method has its own time and space constraints that you need to keep in mind while writing code to print a Fibonacci series in Java.
Though, you can use the method that seems most optimised and convenient for you.
Comments