Calculate Fibonacci series example shows how to calculate and print Fibonacci series in Java using for loop (non-recursive) or using recursion and calculating Fibonacci for large numbers.
What is the Fibonacci series?
From Wikipedia,
In mathematics, the Fibonacci numbers or Fibonacci sequence are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…
The Fibonacci sequence is named after Italian mathematician Leonardo of Pisa, known as Fibonacci.
How to calculate the Fibonacci series in Java?
The Fibonacci series can be calculated in two ways, using for loop (non-recursive) or using a recursion.
Using for loop
The Fibonacci series can be calculated using for loop as given in the below example.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
package com.javacodeexamples.basic; import java.util.Scanner; public class FibonacciExample { public static void main(String[] args) { //get input number from console int number = getInputNumberFromConsole(); //define first two numbers of the series int a = 0; int b = 1; //variable to hold the sum int sum = 0; System.out.println("Fibonacci series"); System.out.print(a + " " + b + " "); for( int i = 2; i < number ; i++ ){ //sum last two numbers sum = a + b; //change the numbers, shift them one place in the series a = b; b = sum; System.out.print(sum + " "); } } private static int getInputNumberFromConsole(){ //create a scanner Scanner s = new Scanner(System.in); System.out.println("Input a number"); //get the int value from the console int n = s.nextInt(); //close the scanner s.close(); return n; } } |
Output
1 2 3 4 |
Input a number 15 Fibonacci series 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
Using recursion
The Fibonacci series can also be calculated using the recursive method as given below.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.javacodeexamples.basic; import java.util.Scanner; public class FibonacciExample { public static void main(String[] args) { //get input number from console int number = getInputNumberFromConsole(); for(int i = 0 ; i < number; i++) System.out.print( fibonacci(i) + ", " ); } //recursive method private static int fibonacci(int n){ /* * first three number in series are * 0, 1, 1 * so return numbers as is */ if(n < 2 ) return n; //else sum previous two numbers and return return fibonacci(n - 1) + fibonacci(n - 2); } private static int getInputNumberFromConsole(){ //create a scanner Scanner s = new Scanner(System.in); System.out.println("Input a number"); //get the int value from the console int n = s.nextInt(); //close the scanner s.close(); return n; } } |
Output
1 2 3 |
Input a number 5 0, 1, 1, 2, 3, |
How to calculate the series of large numbers?
Let’s try to calculate the Fibonacci series for 50 numbers using the above program.
Output
1 2 3 |
Input a number 50 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, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, -1323752223, 512559680, -811192543, |
As you can see from the output, the result started to become negative for larger numbers. This is because the int in Java is a 32 bit signed integer. Once we overflow that limit, it becomes a negative number. We can use the long data type which is 64 bit in Java. If we want to calculate the series for even larger numbers, the BigInteger should be used instead of long as given below.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
package com.javacodeexamples.basic; import java.math.BigInteger; import java.util.Scanner; public class FibonacciExample { public static void main(String[] args) { //get input number from console int number = getInputNumberFromConsole(); //define first two numbers of the series BigInteger a = BigInteger.ZERO; BigInteger b = BigInteger.ONE; //object to hold the sum BigInteger sum = BigInteger.ONE; System.out.println("Fibonacci series"); System.out.print(a + " " + b + " "); for( int i = 2; i < number ; i++ ){ //sum last two numbers sum = a.add(b); //change the numbers, shift them one place in the series a = b; b = sum; System.out.print(sum + " "); } } private static int getInputNumberFromConsole(){ //create a scanner Scanner s = new Scanner(System.in); System.out.println("Input a number"); //get the int value from the console int n = s.nextInt(); //close the scanner s.close(); return n; } } |
1 2 3 4 5 6 7 8 9 10 11 |
Output 1 Input a number 10 Fibonacci series 0 1 1 2 3 5 8 13 21 34 Output 2 Input a number 50 Fibonacci series 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 75025 121393 196418 317811 514229 832040 1346269 2178309 3524578 5702887 9227465 14930352 24157817 39088169 63245986 102334155 165580141 267914296 433494437 701408733 1134903170 1836311903 2971215073 4807526976 7778742049 |
This example is a part of the Java Basic Examples.
Please let me know your views in the comments section below.