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
How to find rate of change on a table
LekaFEV [45]

Answer:

   __X__l__Y_

        1     l    3

        5    l    6

        9     l    9

        13   l    12

        17   l    15

        21   l    18

On the right side, the numbers get larger by adding three each time. On the left side, the numbers get larger by adding 4 each time. It would be the same for any other pair of numbers when you add 4 to the left, and 3 to the right. For instance, the next pair would be (25 , 21). 25 would be the X value, and 21 would be the Y value.

4 0
3 years ago
Hi I have a question and I need answer <br>​
kotegsom [21]

What is your question?

7 0
3 years ago
Does anyone know this?
Margarita [4]

Answer:

[-8, 7]

Step-by-step explanation:

The range is the set of values used for the y-coordinates of the all the points of the function.

The highest value of the function is at point (8, 7), so the greatest value y has is 7.

The lowest point of the function is at point (-3, -8), so the least value of y is -8.

Range: [-8, 7]

5 0
3 years ago
Read 2 more answers
A circle has a radius of 3. An arc in this circle has a central angle of 20
ki77a [65]

A circle has a radius of 3 An arc in this circle has a central angle of 20 what is the length of the arc

8 0
3 years ago
What ace attorney game is this from
Lilit [14]

Answer:

it looks like the pc version since the quality is nice

7 0
3 years ago
Read 2 more answers
Other questions:
  • What is 1 divided by 150
    15·2 answers
  • A truck travels from town A to town B. The truck has 3/4 of gas in tank. it uses 1/3 of gas for the entire trip. The capacity of
    10·2 answers
  • Ben is 33 times as old as Daniel and is also 44 years older than Daniel. How old is Daniel?
    7·1 answer
  • Simplify an expression for the PERIMETER of the equilateral triangle shown below.
    5·1 answer
  • Which representation does not show a proportional relationship between x and y?
    8·1 answer
  • There is a number such that 7 times the number minus 15 is equal to 41
    11·2 answers
  • CAN ANYONE HELP ME ASAP AND THANKS:)
    10·2 answers
  • Question <br> -3(-4)=<br> Please help me I don’t know what to do!
    9·1 answer
  • Which of the following systems of inequalities has no solution?
    5·2 answers
  • Triangle JKL has vertices J(0,2), K(−1,2), and L(0,−3). What are the coordinates of the image of point K after a dilation with a
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!