# Programming the Fibonacci Sequence: Computer Science Basics

*I love teaching others about basic concepts in computer science.*

## 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, I would like to first give an overview of what is the Fibonacci sequence.

**Who Was Leonardo Fibonacci?**

Leonardo Pisano Boglio (aka Leonardo Fibonacci) was an Italian mathematician during the Middle Ages (c. 1170 - 1250). He is considered to be 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.

## 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.

## Recommended for you

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.

*This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.*

## Please place you comments here.

**Binkster (author)** on January 07, 2012:

Thanks ib,

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

Bink

**Brad Masters** from Southern California on January 07, 2012:

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