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
Jenn buys 4 balls of yarn for a total of $23.78. She pays with two $20 bills. What is her change?
tamaranim1 [39]
Her change will be $16.22 since 20 +20 is 40 and 40 - 23.78 is 16.22
3 0
3 years ago
Read 2 more answers
Cho hệ các vector
shutvik [7]

Answer: whats the question?

Step-by-step explanation:

7 0
3 years ago
An estimated regression equation that was fit to estimate ductility in steel using its carbon content was found to be significan
AnnZ [28]

Answer:

At a level of 95%, it is expected that the interval [0.45; 11.59] contains the value of the ductility in steel when its carbon content is 0.5%.

Step-by-step explanation:

Hello!

Considering the dependent variable:

Y: Ductility in steel.

And the independent variable:

X: Carbon content of the steel.

The linear regression was estimated and a prediction interval was calculated.

The prediction interval is calculated to predict a value that the variable Y (response variable) can take for a given value of the variable X (predictor variable) in the definition range of the linear regression line. Symbolically [Y/X=x_{0}]

In this case 95% prediction interval for Y/X=0.5

At a level of 95%, it is expected that the interval [0.45; 11.59] contains the value of the ductility in steel when its carbon content is 0.5%.

I hope it helps!

6 0
3 years ago
The equation of this line is
fomenos

Answer:

y= x + 3

Step-by-step explanation:

I have the same question and that was the correct answer

6 0
2 years ago
A dog is leashed to the corner of a house. How much running area does the dog have? Round answer to the nearest square foot, if
sashaice [31]

Answer:

  942 ft²

Step-by-step explanation:

The diagram shows the area to be 3/4 of that of a circle of radius 20 ft. The area of a circle is given by ...

  A = πr² = π(20 ft)² = 400π ft²

Then 3/4 of that area is ...

  dog run = (3/4)(400π ft²) = 300π ft² ≈ 942.48 ft²

The area of the dog's run is about 942 ft².

3 0
3 years ago
Other questions:
  • Which is one reason why this expression is equal to ?
    8·1 answer
  • The measure of central angle XYZ is 1.25 radians. What is the area of the shaded sector?
    15·2 answers
  • What are the answers to this? PLEASE HELP.
    9·1 answer
  • Mr.collins wrote a number between 7000 and 8000. there were more hundreds than tens. there were more ones than hundreds. what co
    5·1 answer
  • Suppose that a coin has been altered to come up heads 60% of the time. If many samples of 60 coin flips are taken, which of the
    7·1 answer
  • A vehicle travels 250 feet every three seconds find the value of the ratio, the unit rate, and the constant proportionality. How
    8·1 answer
  • In the number 344,589, how many times greater is the value of the 4 in the ten thousands place than the value of the 4 in the th
    6·1 answer
  • There are 30 red marbles, 16 blue marbles, and 14 green marbles in a jar.
    7·1 answer
  • An initial investment is $6100.
    6·1 answer
  • How do you use subsitution for5x+2y=4
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!