Use a recursive formula to define a static method to compute the nth Fibonacci number, given n as an argument. Your method should not use a loop to compute all the Fibonacci numbers up to the desired one, but should be a simple recursive method. Place this static recursive method in a program that demonstrates how the ratio of Fibonacci numbers converges. Your program will ask the user to specify how many Fibonacci numbers it should calculate. It will then display the Fibonacci numbers, one per line. After the first two lines, it will also display the ratio of the current and previous Fibonacci numbers on each line. (The initial ratios do not make sense.) The output should look something like the following if the user
5:
Fibonacci #1 = 0
Fibonacci #2 = 1
Fibonacci #3 = 1; 1/1 = 1
Fibonacci #4 = 2; 2/1 = 2
Fibonacci #5 = 3; 3/2 = 1.5
The Fibonacci sequence occurs frequently in nature as the growth rate for certain idealized animal populations. The sequence begins with 0 and 1, and each successive Fibonacci number is the sum of the two previous Fibonacci numbers. Hence, the first ten Fibonacci numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. The third number in the series is 0 + 1, which is 1; the fourth number is 1 + 1, which is 2; the fifth number is 1 + 2, which is 3; and so on.
Besides describing population growth, the sequence can be used to define the form of a spiral. In addition, the ratios of successive Fibonacci numbers in the sequence approach a constant, approximately 1.618, called the “golden mean.” Humans find this ratio so aesthetically pleasing that it is often used to select the length and width ratios of rooms and postcards.
The recursive algorithm for Fibonacci numbers is a little more involved than the series calculations in the previous Projects. Base cases for 0, 1 or two numbers simply return a value, and all other numbers make two recursive calls to get the previous two Fibonacci numbers to add together to obtain the current number. The method to calculate a Fibonacci number is recursive, but the code to print the output is not; it uses a for-loop to cycle through the Fibonacci numbers and ratios.
See the code in Fibonacci.java.
You might also like to view...
Rather than selling a software package at a fixed, one-time price, software publishers are now offering products you can obtain by paying a monthly subscription fee. This arrangement is known as ____.
A. software as a service B. shareware C. public domain D. open source
Which of the following commands will show the current user cron jobs that have been created for the currently logged in user?
A. cronjobs --list B. cron --list C. cron -l D. crontab -l