# Programming the Fibonacci Sequence: Computer Science Basics

Updated on December 21, 2016 ## Introduction to the Fibonacci Sequence

In this article, I am going to discuss the second method in my series of recursive algorithms. Like factorials, the Fibonacci sequence is another algorithm that shows exponential growth over time. It is also one of the four methods used in the study of recursion. So, with that said, first I would like to give an overview of what is the Fibonacci Sequence.

## Who was Fibonacci, What is the Fibonacci Sequence, and What is its Impact on Computer Science?

Who was Leonardo Fibonacci?

Leonardo Pisano Boglio (aka Leonardo Fibonacci) was an Italian Mathematician during the Middle Ages (c. 1170 - 1250). He is considered one of the top mathematicians of his time and is credited in creating the book "Liber Abaci," which is a book based on mathematical calculations. Of course the most famous algorithm in the book is the Fibonacci sequence which is based on solving a problem dealing with population growth of rabbits. The actual sequence was not his own however. The algorithm is actually based on knowledge he gained from Hindu mathematicians who discovered it around the 6th century. However, it was the first time that the algorithm was introduced to the west and gave Fibonacci the modern reputation as being one of the people who helped to introduce the Hindu/Arabic number system to Europe.

So, What Exactly is The Fibonacci Sequence?

The sequence was an answer to a problem dealing with the exponential growth of a population. In the case of Fibonacci's book, it dealt with the population growth of rabbits. The algorithm takes into account the number of iterations or times a function is called and adds the sum of the iteration minus one and the previous number minus two. However, in the case of and iteration count of 0 or 1, the sum would always equal 0 and 1 respectively. Yet, when the iteration count gets higher than one, you will see an exponential growth in the sum that is produced. The following is how the formula is written to give a better idea in what is going on:

Fib(n) where n = 0, the sum is always 0 [base case]

Fib(n) where n = 1, the sum is always 1 [base case]

However, Fib (n) where all integers n > 1 then Fib(n) is (Fib(n-1) + Fib(n-2)).

So, if the iteration is 0 or 1 then the number will equal 0 and 1 respectively. However, as the iterations increase beyond 1, you start seeing exponential growth in the output.

The Fibonacci Sequence as it Pertains to Computer Science:

In academia, computer science programming courses like to use this algorithm in their study of recursive methods. In C.S., a recursive method is a method that is being defined within its own definition. Basically, instead of the method being called by another method, it actually calls itself. Which in its own way makes it another way to program a loop?

The reasoning behind the study is to give students an understanding on how to solve certain problems that requires a solution from a base case. This is why the Fibonacci sequence is so popular because it gives a base case then allows a program to make repeated calls to a method to solve the problem. With that said, below is a Java example of recursions using Fibonacci formula:

## Recursion Example using the Fibonacci Sequence.

```//***************************************************************
//
// Example of recursion using the Fibonacci sequence in Java.
//
// Author: Bink
//
//**************************************************************
package fibonaccirecursion;

import java.math.BigInteger;//For math function

public class FibonacciRecursion {

//Set initial value for BigInterger: return number
private static BigInteger TWO = BigInteger.valueOf(2);
//Do the recursive calculations
public static BigInteger fibonacciCalculations(BigInteger number){
if(number.equals(BigInteger.ZERO)||number.equals(BigInteger.ONE))
return number;
else
fibonacciCalculations(number.subtract(TWO)));
}//end function
public static void main(String[] args) {

//run through the sequence until counter == 30
for(int counter = 0; counter <= 30; counter++){
System.out.printf("Fibonacci of %d is: %d\n", counter,
fibonacciCalculations(BigInteger.valueOf(counter)));

}//end for
}//end main
}//end class
```

## Conclusion:

So, that sums up what recursion is and how important Fibonacci's formula is to the study of recursion. If you copy the code above, you will see that after every iteration, the numbers go up exponentially until you reach whatever iteration you set. In the case of the program above, the iteration limit was set to 30.

Again, as stated earlier, the Fibonacci sequence is a good way to learn how to program a recursive method. It is widely used in college academia from computer science to math degrees and it gives a programmer another tool in their arsenal to solve problems.

## Related

0 of 8192 characters used
• AUTHOR

Binkster

8 years ago

Thanks ib,

I am new to the game so thank you for the advice. I'll start doing that.

Bink

• 8 years ago from Southern California

Bink

Nice hub, and interesting background.

If I were doing this program, my style is to put the overview of the calculations in the comment box.

But that is my personal style.

Thankks

working