Programming the Fibonacci Sequence: Computer Science Basics

Updated on December 21, 2016
Sometimes even the CPU gets fed up!
Sometimes even the CPU gets fed up! | Source

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

Please place you comments here.

    0 of 8192 characters used
    Post Comment

    • Binkster profile imageAUTHOR

      Binkster 

      6 years ago

      Thanks ib,

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

      Bink

    • ib radmasters profile image

      ib radmasters 

      6 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

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, turbofuture.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://turbofuture.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)