Rewrite the fib method in Listing 18.2 using iterations. To compute fib(n) without recursion, you need to
Question:
Rewrite the fib method in Listing 18.2 using iterations. To compute fib(n) without recursion, you need to obtain fib(n - 2) and fib(n - 1) first. Let f0 and f1 denote the two previous Fibonacci numbers. The current Fibonacci number would then be f0 + f1. The algorithm can be described as follows:
Write a test program that prompts the user to enter an index and displays its Fibonacci number.
Listing
Transcribed Image Text:
f0 = 0; // For fib(0) f1 = 1; // For fib(1) for (int i = 1; i <= n; i++) { currentFib = f0 + f1; f0 = f1; f1 currentFib; // After the loop, currentFib is fib(n) 1 import java.util.Scanner; 2 3 public class ComputeFibonacci { 4 /** Main method */ public static void main(String] args) { // Create a Scanner Scanner input = new Scanner(System.in); System.out.print("Enter an index for a Fibonacci number: "); int index = input.nextInt(); 10 11 12 13 14 // Find and display the Fibonacci number System.out.println("The Fibonacci number at index " + index + " is " + fib(index)); 15 16 17 18 19 /** The method for finding the Fibonacci number */ public static long fib(long index) { if (index == 0) // Base case return 0;
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 83% (12 reviews)
Output Enter an index for the fibonacci number 34 The Fibonacci is 9...View the full answer
Answered By
Grace Igiamoh-Livingwater
I am a qualified statistics lecturer and researcher with an excellent interpersonal writing and communication skills. I have seven years tutoring and lecturing experience in statistics. I am an expert in the use of computer software tools and statistical packages like Microsoft Office Word, Advanced Excel, SQL, Power Point, SPSS, STATA and Epi-Info.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Modify Listing 18.2, ComputeFibonacci.java, so that the program finds the number of times the fib method is called. Listing 1 import java.util.Scanner; 2 3 public class ComputeFibonacci { 4 /** Main...
-
Rewrite the Circle class in Listing 13.2 to extend GeometricObject and implement the Comparable interface. Override the equals method in the Object class. Two Circle objects are equal if their radii...
-
Listing 20.7, DirectorySize.java, gives a recursive method for finding a directory size. Rewrite this method without using recursion. Your program should use a queue to store the subdirectories under...
-
What are the Marketing Cost Estimates of Pepsi Company? Marketing estimates, in 2013-2019? It can be write in a paragraph and explain it statistically.
-
Assume that a consumer with lexicographic preferences over two commodities requires a positive amount of both commodities so that consumption set X = 2++. Show that no optimal choice exists.
-
In Exercises find the volume of the solid generated by revolving the region bounded by the graphs of the equations about the line x = 5. xy = 3, y = 1, y = 4, x = 5
-
Develop contactor designs that combine advantages of cocurrent, crossflow, and countercurrent cascades.
-
Sycamore Plastics (SP) is a manufacturer of polyethylene plastic pellets used as a raw material by manufacturers of plastic goods around the U. S. SP currently operates four manufacturing centers in...
-
Consider the Diamond - Dybvig banking model. Assume, as in the notes, that u ( c ) = ( c ^ ( 1 - a ) ) / ( 1 - a ) and that a = 0 . 5 Assume that r = 0 . 0 5 and t = 0 . 5 using the Excel...
-
Fruity Juices, Inc. produces five different flavors of fruit juice: apple, cherry, pomegranate, orange, and pineapple. Each batch of product requires processing in three departments (blending,...
-
The gcd(m, n) can also be defined recursively as follows: If m % n is 0, gcd(m, n) is n. Otherwise, gcd(m, n) is gcd(n, m % n). Write a recursive method to find the GCD. Write a test program that...
-
Using the BigInteger class introduced in Section 10.9, you can find the factorial for a large number (e.g., 100!). Implement the factorial method using recursion. Write a program that prompts the...
-
In Problems 1750, find the partial fraction decomposition of each rational expression. 2x + 3 x4 - 9x 2
-
Your t-shirt company sees that shirts of a certain variety have demand function q = -90p + 900 where q is the number of shirts you can sell in one week if you charge $p per shirt. You have fixed...
-
Inc. hasa 10.0% coupon, with semiannual payments. YTM is11.00%. Maturity is9 yrs. What's the price? Hint: remember for semi-annual, divide both the coupon rate and YTM by 2, and multiply the number...
-
Feather Friends, Incorporated, distributes a high-quality wooden birdhouse that sells for $120 per unit. Variable expenses are $60.00 per unit, and fixed expenses total $180,000 per year. Its...
-
Question 1 0/1 point The figure below shows the market for a good. An improvement in EQ would shift the supply curve for the good from S to S. What would be the total willingness to pay of market...
-
A company reports the following beginning inventory and two purchases for the month of January. On January 26, the company sells 410 units. Ending inventory at January 31 totals 150 units. Units Unit...
-
A put option that expires in five months with an exercise price of $60 sells for $2.87. The stock is currently priced at $58, and the risk-free rate is 4.1 percent per year, compounded continuously....
-
Ex. (17): the vector field F = x i-zj + yz k is defined over the volume of the cuboid given by 0x a,0 y b, 0zc, enclosing the surface S. Evaluate the surface integral ff, F. ds?
-
An airport is developing a computer simulation of air-traffic control that handles events such as landings and takeoffs. Each event has a time stamp that denotes the time when the event will occur....
-
What does each removeMin call return within the following sequence of priority queue ADT operations: insert(5, A), insert(4, B), insert(7, F), insert(1, D), removeMin( ), insert(3, J), insert(6, L),...
-
The indented parenthetic representation of a tree T is a variation of the parenthetic representation of T (see Code Fragment 8.26) that uses indentation and line breaks as illustrated in Figure 8.22....
-
Twenty-two young asthmatic volunteers were studied to assess the short-term effects of sulfur dioxide (SO 2 ) exposure under various conditions. The baseline data were recorded regarding bronchial...
-
Investment Analysis Stock Valuation Exercise If you're aware that Al-Raed Company has paid dividends of 5 riyals, and that the growth rate of these dividends is expected to decrease by 10% for the...
-
At the beginning of this semester, you were asked to share your perspectives on how you may apply cost management information for your current work/job. Now, it is the end of the semester, and you...
Study smarter with the SolutionInn App