1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ronch [10]
3 years ago
8

The fundamental source of the inefficiency is not the fact that recursive calls are being made, but that values are being recomp

uted. One way around this is to compute the values from the beginning of the sequence instead of from the end, saving them in an array as you go. Although this could be done recursively, it is more natural to do it iteratively. Proceed as follows: a. Add a method fib2 to your Fib class. Like fib1, fib2 should be static and should take an integer and return an integer. b. Inside fib2, create an array of integers the size of the value passed in. c. Initialize the first two elements of the array to 0 and 1, corresponding to the first two elements of the Fibonacci sequence. Then loop through the integers up to the value passed in, computing each element of the array as the sum of the two previous elements. When the array is full, its last element is the element requested. Return this value. d. Modify your TestFib class so that it calls fib2 (first) and prints the result, then calls fib1 and prints that result. You should get the same answers, but very different computation times.

Mathematics
2 answers:
Fudgin [204]3 years ago
8 0

Step-by-step explanation:

<em>(you can download the attached PDF for a better view)</em>

The Fibonacci sequence is a well-known mathematical sequence in which each term is the sum of the two previous terms.

More specifically, if fib(n) is the nth term of the sequence, then the sequence can be defined as follows:

fib(0) = 0

fib(1) = 1

fib(n) = fib(n-1) + fib(n-2) n>1

1. Because the Fibonacci sequence is defined recursively, it is natural to write a recursive method to determine the nth

number in the sequence. File Fib.java contains the skeleton for a class containing a method to compute Fibonacci

numbers. Save this file to your directory. Following the specification above, fill in the code for method fib1 so that it

recursively computes and returns the nth number in the sequence.

2. File TestFib.java contains a simple driver that asks the user for an integer and uses the fib1 method to compute that

element in the Fibonacci sequence. Save this file to your directory and use it to test your fib1 method. First try small

integers, then larger ones. You'll notice that the number doesn't have to get very big before the calculation takes a very

long time. The problem is that the fib1 method is making lots and lots of recursive calls. To see this, add a print

statement at the beginning of your fib1 method that indicates what call is being computed, e.g., "In fib1(3)" if the

parameter is 3. Now run TestFib again and enter 5—you should get a number of messages from your print statement.

Examine these messages and figure out the sequence of calls that generated them. (This is easiest if you first draw the

call tree on paper.) . Since fib(5) is fib(4) + fib(3),you should not be surprised to find calls to fib(4) and fib(3) in the

printout. But why are there two calls to fib(3)? Because both fib(4) and fib(5) need fib(3), so they both compute it—very

inefficient. Run the program again with a slightly larger number and again note the repetition in the calls.

3. The fundamental source of the inefficiency is not the fact that recursive calls are being made, but that values are being

recomputed. One way around this is to compute the values from the beginning of the sequence instead of from the end,

saving them in an array as you go. Although this could be done recursively, it is more natural to do it iteratively. Proceed

as follows:

a. Add a method fib2 to your Fib class. Like fib1, fib2 should be static and should take an integer and return an integer.

b. Inside fib2, create an array of integers the size of the value passed in.

c. Initialize the first two elements of the array to 0 and 1, corresponding to the first two elements of the Fibonacci

sequence. Then loop through the integers up to the value passed in, computing each element of the array as the sum

of the two previous elements. When the array is full, its last element is the element requested. Return this value.

d. Modify your TestFib class so that it calls fib2 (first) and prints the result, then calls fib1 and prints that result. You

should get the same answers, but very different computation times.

// ******************************************************************

// Fib.java

//

// A utility class that provide methods to compute elements of the

// Fibonacci sequence.

// ******************************************************************

public class Fib

{

//--------------------------------------------------------------

// Recursively computes fib(n)

//--------------------------------------------------------------

public static int fib1(int n)

{

//Fill in code -- this should look very much like the

//mathematical specification

}

// ******************************************************************

// TestFib.java

//

// A simple driver that uses the Fib class to compute the

// nth element of the Fibonacci sequence.

// ******************************************************************

import java.util.Scanner;

public class TestFib

{

public static void main(String[] args)

{

int n, fib;

Scanner scan = new Scanner(System.in);

System.out.print("Enter an integer: ");

n = scan.nextInt();

fib = Fib.fib1(n);

System.out.println("Fib(" + n + ") is " + fib);

}

}

Download pdf
sweet-ann [11.9K]3 years ago
7 0

Answer:

Refer below for the answer.

Step-by-step explanation:

Refer to the pictures attached for the code of all four parts.

You might be interested in
Find the volume of a box 24 in. x 20 in x 23 in.
kaheart [24]
<span>The definition for discovering the volume of a box is when the person takes the length, the width, and the height of the box. And because we have those values here, there is no math that is necessary to do other than multiplying the numbers together and creating a new unit of inches cubed. The answer is 11040 inches cubed.</span>
8 0
3 years ago
Find the inverse for each relation:<br> 1. {(1,-2), (2, 3),(3, -3),(4, 2)}
andrey2020 [161]

Answer:

the inverse for each relation:

1. {(1,-2), (2, 3),(3, -3),(4, 2)} is

<u>{</u><u>(</u><u>-</u><u>2</u><u>,</u><u>1</u><u>)</u><u>,</u><u>(</u><u>3</u><u>,</u><u>2</u><u>)</u><u>,</u><u>(</u><u>-</u><u>3</u><u>,</u><u>3</u><u>)</u><u>,</u><u>(</u><u>2</u><u>,</u><u>4</u><u>)</u><u>}</u><u>.</u>

3 0
3 years ago
In triangle ABC below, BD is the altitude. Point A represents Omari's house, point C represents the restaurant where Omari works
tester [92]

Answer:

B

Step-by-step explanation:

B

4 0
2 years ago
Maria works as an electrician and earns $24.68/h. If she worked for 15 hours on one job, how much did she earn? *
rodikova [14]

Answer:

she would have $370.20

Step-by-step explanation:

24.68*15=370.2

8 0
3 years ago
Goes through the 2 points (-3,5) (4,5). what's the equation?
Ierofanga [76]
Slope = 0
b = 5

equation
y = 5
3 0
3 years ago
Other questions:
  • A board is 7 3/4 feet in length. How many full size pieces of board 1 1/3 feet in length can a carpenter cut from that board of
    11·2 answers
  • Solve the inequality<br> - 7&lt;-5.
    10·2 answers
  • Which set includes rational numbers but not natural numbers?
    10·2 answers
  • What is the image point of ( -8,3) after a translation right 2 units and up 3 units? Submit Answer attempt out of 2 N​
    11·1 answer
  • Two sides of a triangle measure 15 cm and 21 cm. which of the following could be the measure of the third side
    9·2 answers
  • Elliott’s backyard is 4800 square feet. His dad is going to pave 15% of this area to use as a patio. How many square feet will b
    13·1 answer
  • Please help this is timed! I’ll give brainliest.
    13·1 answer
  • 8. Divide: (6x² - 4x)+2x<br>A. 3x - 4x<br>B. 3x - 2x<br>C. 6x-2<br>D. 3x - 2​
    15·1 answer
  • Two numbers multiply to equal -24 but adds up to equal -4
    11·2 answers
  • 4(x−2+y)<br> It's for khan academy
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!