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.
-
Direct Materials Budgeting Shaver Bicycle Shop assembles and sells tricycles and bicycles. The frames are purchased from one supplier and the wheels from another. The following materials are...
-
Can young infants respond to the categories cat and dog? (263)
-
Steady Construction Company is considering selling excess machinery with a book value of $ 280,000 (original cost of $ 400,000 less accumulated depreciation of $ 120,000) for $ 244,000, less a 5%...
-
Wildhorse Surplus made cash sales during October of $ 3 9 2 0 0 0 . The sales are subject to a 5 % sales tax that was also collected. Which of the following would be included in the summary journal...
-
The members AD and BC are parallel while the 7kN external force is acting vertically downwards in Fig.1. By using the method of joints or otherwise, calculate the forces in members BC and DC...
-
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...
-
Propose a mechanism to account for the fact that D-gluconic acid and D-mannonic acid are inter converted when either is heated in pyridine solvent.
-
a) Describe the following concepts in the context of organizational development. b) Discuss how these concepts interrelate and support each other within an organizational framework
-
Q2. a) Analyze the importance of communication in the change management process. b) Suggests strategies that a Disaster Management Organization can employ to ensure effective communication during...
-
Q3. a) Explain the following Change Management Models
-
Q3. b) Discuss how each model can be applied in real-world organizational change scenarios.
-
In this question, you will work step-by-step through an optimization problem. A craftsman wants to make a cylindrical jewelry box that has volume, V, equal to 55 cubic inches. He will make the base...
-
Conduct a follow-up analysis for the test in Exercise 50. Exercise 50. A study of first-year college students asked separate random samples of students from private and public universities the...
-
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....
-
Ferris Ltd. is a Canadian controlled private corporation. For the year ending December 31, 2019, its accounting Net Income Before Taxes, as determined under generally accepted accounting principles,...
-
If Faten accomplishes her projects with high-quality results, but takes more time than other managers in the process, as a manager she is ______. Select one: a. effective, but inefficient b....
-
Moore Corporation repurchased 3,700 shares of its own stock for $60 per share. The stock has a par of $15 per share. A month later Moore resold 925 shares of the treasury stock for $68 per share....
Study smarter with the SolutionInn App