• Topics List
  • Help and Tips
    • Frequently Asked Questions
    • Testimonials
    • Advertisements
    • Weekly News Letters
    • Referral System
    • Other Links
Login JOIN Sirgo!
  Search
HyperLink

Home  »  Topics  »  Computers & Electronics  »  PCs  »  Computer Software

Programming the Fibonacci Sequence: Computer Science Basics.

Like this Sirgo Page?1
Tweet

The Fibonacci Sequence Impact on Computer Science.

One of the first things that basic programming classes in college will cover is recursion. Recursion is the concept of repetition which involves a function or method repeating itself over and over until a specific criteria is met. Basically, it is a function that one would create if they needed to make the same call to a method within a loop.

When this subject is taught in computer science the most popular algorithm that is used is the Fibonacci sequence. This sequence gives a perfect example of exponential growth and it is a perfect teaching tool for creating a recursive function.

In this article, I will cover this algorithm by giving out a short bio of Fibonacci and what exactly is the algorithm he helped endorse. I will also sum up what the algorithm is supposed to prove and its importance to the study of Computer Science. Finally, I will also give you an example on how to create a recursive function that uses Fibonacci's algorithm.

Who was 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 the 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:



//***************************************************************
//
// 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 TWO: 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
return fibonacciCalculations(number.subtract(BigInteger.ONE))
.add(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.
 

Amazon Carousel
 
Old Comments Module
 

New Guest Book Comments

   

Submit
Created by  Binkster



Binkster's Published Articles
Top Article: Programming an Insertion Sort Algorithm in Java



Copyright 2011 by SirGo.com Inc.   |  Privacy Statement  |  Terms Of Use