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 the inverting configuration of Fig. 5-13 switch the inverting and noninverting inputs of the op amp so that the inverting input is connected to the ground and a positive feedback is provided to...
-
Write a code to test a Gaussian pseudorandom number generator. If you do not have a canned generator available, write a generator based on the Box-Muller algorithm in Appendix I. Apply the following...
-
Hastings estimates that if it acquires Vandell, interest payments will be $1,500,000 per year for 3 years after which the current target capital structure of 30 percent debt will be maintained....
-
The technical support call centre for a software company has a mean wait time of 210 s, with a standard deviation of 40 s. The management team wants to continue to improve customer satisfaction by...
-
Platino Bhd leases the following assets from three lessors. The assets are to be used in the property development of the company. The details of the assets are as follows: Lessor Colton Bhd Marisha...
-
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...
-
Hard water often contains dissolved Ca 2 + and Mg 2 + ions. One way to soften water is to add phosphates. The phosphate ion forms insoluble precipitates with calcium and magnesium ions, removing them...
-
Oslo Manufacturing incurred $72,000 of direct materials costs, direct labor costs of $24,500, and factory overhead of $20,500. If 1,000 direct materials equivalent units and 900 conversion equivalent...
-
Explore the impact of demographic shifts, such as aging populations and changing consumer preferences, on the business strategies and product offerings of retail banks in developed economies.
-
List three (3) aspects for inducting staff into your business
-
3. Discuss at least two advantages of using a mandrel during swaging process. (25 point) Tube Die Mandrel (a) (b) Fig. 3. (a) Swaging of tubes without a mandrel, (b) Swaging with a mandrel.
-
1. Why do we call CO2 and methane greenhouse gases? 2. How is Earth's atmosphere similar to the plastic of the collection bottle? 3. Why are we worried about CO2 levels if CO2 is part of the natural...
-
Swirsky Company uses the cash receipts and cash payments journals illustrated in this appendix for a perpetual inventory system. In October, the following selected cash transactions occurred: 1. Made...
-
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....
-
The cumulative incidence of myocardial infarction is 180.0 new cases per 10,000 persons among individuals with severe hypertension. In contrast, the cumulative incidence of myocardial infarction is...
-
Rundle Industries produces two electronic decoders, P and Q. Decoder P is more sophisticated and requires more programming and testing than does Decoder Q. Because of these product differences, the...
-
What financial statement, which can be reported with or separate from the income statement, includes income-related items that affect the balance sheet but are not included in net income?
Study smarter with the SolutionInn App