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
9x+18+3x=1
Katen [24]
The error is in the second step they subtracted 3 from both sides but it was a 3x, which you can not do.
Correction.
9x+18+3x=1
12x+18=1
12x=-17
x=-17/12
3 0
3 years ago
Read 2 more answers
(no links pls) find the value of m
Nina [5.8K]

Answer:

D.

<P= 5

I hope this helps:)

6 0
3 years ago
Help please 11 POINTS
Y_Kistochka [10]
57.10
70.9
these are only the answers for the first two problems
4 0
3 years ago
Read 2 more answers
Stereo has a list price of $150 but is on sale for 20% off the list price what is the sale price of the stereo???
8_murik_8 [283]

Answer:

The correct answer is option A.120

Step-by-step explanation:

Let's start by analyzing the information they have given us.

We know that the stereo has a list price of $ 150, but at the time of payment, we are told that it has a 20% discount.

Now we must find out what the discount was applied to know the final price of the stereo.

One way to calculate the percentage would be multiplying 150 by 0.20

150.0.20 = 30

The discount applied is $ 30, so now we just have to subtract that amount from the original price:

150-30 = 120

Therefore, the correct answer is option A.

3 0
4 years ago
Read 2 more answers
Could someone please help me with this question!<br> SSS<br> SAS<br> AAS<br> ASA
alexgriva [62]

Answer:

7. RHS

8.RHS

9.SAS

10.SAS

11.RHS

12.ASA

Step-by-step explanation:

6 0
2 years ago
Other questions:
  • Is (-2,3) a solution of each system
    12·1 answer
  • Dane used a ruler and a protractor to draw a rhombus with a side that is 4 cm in length and an angle that measures 110°. Choose
    8·1 answer
  • The area of this circle is 72π m². What is the area of a 45º sector of this circle?
    13·2 answers
  • The sum of the BLANK of the plane figure is called the perimeter
    14·1 answer
  • Which algebra tiles are used in this equation: <br><br> 4x + 7 = 23
    12·2 answers
  • Can you please help me?
    11·1 answer
  • 100 POINTS IF YOU GUESS RIGHT PLEASE HELP
    15·1 answer
  • Find the value of c so that (x+1) is a factor of the polynomial p(x)
    14·1 answer
  • Solve for x round to the nearest tenth
    11·1 answer
  • Anthony wants to find out the percentage of students at his school who are vegetarians. Which of the following is the most repre
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!