top of page

Fibonacci Sequence

  • Writer: Yahvin Gali
    Yahvin Gali
  • Feb 26, 2021
  • 3 min read

To aid the user in finding the Fibonacci number of a particular index 'n', until the user wishes to quit.



A truly mesmerizing view, isn't it? The above picture of a spiraling staircase is a perfect display of the "Golden Ratio", a model best approximated by the Fibonacci sequence that explains certain perfections in nature and architecture.


The Golden Ration (ϕ; phi; ~ 1.618) is referred as "The Most Beautiful Number in The Universe" due to the fact that it is seen in almost everything from geometry, to mother nature, to the human body!

Human Anatomy follows the Golden Ratio...

And so do some plants!


The Golden Ratio & Fibonacci

This project serves the purpose of allowing the user to browse through the Fibonacci sequence via index (n). However, the user cannot view terms above a certain index in order to prevent unnecessary complications (more on that).


Intro to Recursion

This program displays the term of the Fibonacci Sequence at a particular index n. This is done by utilizing recursion, a programming technique that essentially takes the output from on iteration and places it back into the same method. This done until the method reaches a "base case" where nothing more can be done. The code shown below is an example of recursion.

public static double factorial(double num){
    if (num == 0) return 1;
    else if (num == 1) return 1;
    else return num*factorial(num-1);
}

When the factorial() method is run, with an argument of 5 for instance, and the output is printed using System.out.println()

System.out.println("Result: " + factorial(5));

the following output is displayed:

Result: 120

The resulting value is the same as 5! which is 5*4*3*2*1 = 120.


How Does it Work?

Once the program is run, a continuous while loop begins, that asks the user whether they would like to input an index or not. If the user types anything other than "Y", the program quits; if the user does type "Y", then the user is prompted to enter an integer number, which pertains to the index. The pertaining Fibonacci number is then displayed, after which the program asks if the user would like to continue. When the user does finally choose to end the program, the program displays a farewell message. An example of output is shown below (User input is bolded).

Begin? (Y/N):
Y

Please enter an integer number:
12

The Fibonacci number is: 144

Continue? (Y/N):
N

Have a nice day!

As mentioned before, there are some complications that may result from indexes that are too large. Through repeated testing, it was found that an index either equal to or larger than 50 takes an unbelievably long compile time. This is to be expected, as the program utilizes recursion which has certain limits due to its Big O Notation (an equation that represents compile time for programming techniques. More on that in later posts).


To prevent such compile errors, a try-catch statement was used. If the index is above 50, equal to 50, or not a number, an error statement is displayed, and the while loop allows the user to enter another index. The entire code is available on this Github page.

Begin? (Y/N):
Y

Please enter an integer number:
g

Input is either too long or not a number. Please try again.
Please enter an integer number:
56

Number is larger than 50. Please try again.
Please enter an integer number:
25

The Fibonacci number is: 75025

Continue? (Y/N):
N

Have a nice day!

Final Thoughts

I enjoyed this project. Although it was quite difficult implementing the try and catch loops so that the program continuously ran, I persevered. The result was a perfectly working program that did not cause any unnecessary complications for the user, such as an overload of RAM or the computer crashing. All in all, it was a great learning experience.

Comments


IMG_5139.jpeg

About Me

Aspiring AI Engineer. Ardent environmentalist. Believer in upliftment through service. Thalassophile. Rookie food experimentalist who brews a mean Chai. Loves to play Piano.

 

Read More

 

© 2021 by Yahvin Gali. Proudly created with Wix.com

  • LinkedIn
  • Facebook
  • Instagram
bottom of page