Fibonacci Java

The Fibonacci sequence is a very popular sequence in Math. The first two numbers in the sequence is 0 and 1. And all the succeeding numbers in the series is derived by adding the previous two numbers before it. For example, the third number in the sequence is derived by adding the first two which is 0 and 1, that equals to 1. The fourth is derived by adding the second and third 1+1 = 2. The fifth number is derived by adding the third and fourth 1+2 =3. And so on. And it is interesting to know that this sequence, which by first impression doesn't make sense, has many application in multiple discipline. For example, some traders who study technical analysis in stocks and other securities has observed that certain patterns in price movements follow the Fibonacci sequence. In software development, particularly in Agile practitioner, they follow task estimation using Fibonacci numbers. There are many more application of this. But this post will try to explore how to generate the Fibonacci series in Java.

Fibonacci Sequence Java

Below is a simple example of generating the first 10 numbers in the Fibonacci sequence using Java. If we want to adjust how many items we want to display, just modify the constant NUMBER_OF_ITEMS.

package test;
/**
 * A simple example that displays the Fibonmacci sequence in Java using a for loop.
 */
public class FibonacciExample {
    private final static int NUMBER_OF_ITEMS = 10;
    public static void main(String[] args) {
        int firstNumber = 0;
        int secondNumber = 1;
        System.out.print(firstNumber);
        System.out.print(", "+secondNumber);
        for (int i = 3; i <= NUMBER_OF_ITEMS; ++i)
        {
            int nextNumber = firstNumber + secondNumber;
            System.out.print(", "+nextNumber);
            firstNumber = secondNumber;
            secondNumber = nextNumber;
        }
    }
}

0, 1, 1, 2, 3, 5, 8, 13, 21, 34

Fibonacci Series Java Nth term

Sometimes we don't want to get the whole sequence or series, but just the nth term in the sequence. A function is a good way to do that, here is a simple example:

package test;
/**
 * A simple example gets the nth term of a Fibonacci sequence:
 */
public class FibonacciExample {
    private static int getFibonacci(int term) {
        if (term == 1) {
            return 0;
        }
        if (term == 2) {
            return 1;
        }
        int firstNumber = 0;
        int secondNumber = 1;
        int nextNumber = 1;
        for (int i = 3; i <= term; ++i)
        {
            nextNumber = firstNumber + secondNumber;
            firstNumber = secondNumber;
            secondNumber = nextNumber;
        }
        return nextNumber;
    }
    public static void main(String[] args) {
        System.out.println(getFibonacci(1));
        System.out.println(getFibonacci(2));
        System.out.println(getFibonacci(3));
        System.out.println(getFibonacci(4));
        System.out.println(getFibonacci(5));
        System.out.println(getFibonacci(6));
        System.out.println(getFibonacci(7));
        System.out.println(getFibonacci(8));
        System.out.println(getFibonacci(9));
        System.out.println(getFibonacci(10));
    }
}
As you can see, we just modified our algorithm from the first example to come up with a function. And we try to display the first 10 items in the Fibonacci series in Java. The result is shown below as expected:
0
1
1
2
3
5
8
13
21
34

Fibonacci Recursive Java

Using for loop is not romantic or elegant to look at. Recursion always simplifies the code and it looks very cool. We modify the code above to produce the nth term in a Fibonacci series in Java. See below:
package test;
/**
 * A simple example gets the nth term of a Fibonacci sequence using recursion:
 */
public class FibonacciExample {
    private static int getFibonacci(int term) {
        if (term == 1) {
            return 0;
        }
        if (term == 2) {
            return 1;
        }
        return getFibonacci(term - 1) + getFibonacci(term - 2);
    }
    public static void main(String[] args) {
        System.out.println(getFibonacci(1));
        System.out.println(getFibonacci(2));
        System.out.println(getFibonacci(3));
        System.out.println(getFibonacci(4));
        System.out.println(getFibonacci(5));
        System.out.println(getFibonacci(6));
        System.out.println(getFibonacci(7));
        System.out.println(getFibonacci(8));
        System.out.println(getFibonacci(9));
        System.out.println(getFibonacci(10));
    }
}
This is the beauty of recursion as the solution is close to how we describe a Fibonacci number. That is the first term is 0, the second is 1, and all the succeeding terms are produced by adding the previous two. getFibonacci(n) = getFibonacci(n - 1) + getFibonacci(n - 2). Recursion is actually much slower than the for loop solution, but it works and it is so pretty! The output is the same as shown below:
0
1
1
2
3
5
8
13
21
34