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...
Answer the following questions true (T) or false (F)
1. You can use any primitive type as the base type of an ArrayList class. 2. ArrayList objects do not have the array square-bracket notation.
Some operating systems include drivers for peripheral devices
Indicate whether the statement is true or false
Case-Based Critical Thinking QuestionsCase 2-2You have built your first schedule but your supervisor wants you to make some changes to it, adding some padding in some places and tightening things up in other places. This requires you to familiarize yourself with the concepts of lead and lag time.You have added positive lag to a task dependency between two tasks in an FS relationship. Which of the following can you expect to happen?
A. The Start date of the first task moves forward in time. B. The Start date of the second task moves forward in time. C. The Finish date of the second task moves forward in time. D. The Finish date of the first task moves forward in time.
Which of the following commands lets you add color to table cells?
What will be an ideal response?