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
Twenty-eight slices of pizza cost $12. Approximately how much would 70 slices cost?
Mademuasel [1]

Answer:

$30.

Step-by-step explanation:

70/28 = 2.5

12 x 2.5 = 30

5 0
2 years ago
28 girls and 38 boys volunteer to plant trees at a school. their teacher wants to put them in groups that have girls AND boys. h
Delvig [45]

Answer:

so 14 can go into 28, 2 times and 19 can go into 38 2 times so the techaer can make 2 group with 14 girls and 19 boys in each group.

Step-by-step explanation:

5 0
3 years ago
C.) of a total of $10,000, part was invested at 10% simple interest and the remainder at
nlexa [21]

Answer:

$3,500 at 10% and $6,500 at 7%.

Step-by-step explanation:

M * 10% + (10000 - M) * 7% = 805

0.1M + 700 - 0.07M = 805

0.03M = 105

M = 3500

4 0
3 years ago
(1 point) Find an equation of the largest sphere with center (5,3,5)(5,3,5) and is contained in the first octant. Be sure that y
Hoochie [10]

Answer:

x^2+y^2+z^2-10x-6y-10z +50 =0

Step-by-step explanation:

Given that a sphere is contained in the first octant

Centre of the sphere is given as (5,3,5)

Since this is contained only in the first octant radius should be at most sufficient to touch any one of the three coordinate planes

When it touches we can get the maximum sphere

We find that y coordinate is the minimum of 3 thus radius can be atmost 3 so that then only it can touch y =0 plane i.e. zx plane without crossing to go to the other octants.

Hence radius =3

Equation of the sphere would be

(x-5)^2 +(y-3)^2+(z-5)^2 = 3^2\\x^2+y^2+z^2-10x-6y-10z +50 =0

8 0
3 years ago
What is a reasonable estimate for 786 x 217?
zhuklara [117]

Answer:

160,000

Step-by-step explanation:

Estimations like this usually involve rounding to easy numbers to multiply. You could do this in a number of ways depending on your ability to keep track of factors in your head, but the easiest way (and what I assume your teacher is looking for) is to round to the nearest, largest-power-of-ten, which in this case is the hundreds. 786 is closest to 800, and 217 is closest to 200, and 200*800 is 160,000. No calculator required is the key. You can also be slightly more sure that this is <em>reasonable</em> because you rounded up for one and down for the other, approximately by the same amount, too.

5 0
3 years ago
Other questions:
  • URGENT an athlete can lift a maximum of 80 pounds. if she uses 3 pound weights when she exercises, what percent is she lifting
    13·1 answer
  • A pizza parlor sold 38 1/2 pizzas during a dinner hour. If each pizza contained 8 slices, how many slices of pizza were sold?
    10·2 answers
  • A student is given the rectangle and the square shown. The student states that the two figures have the
    7·1 answer
  • Smart people help me please​
    6·2 answers
  • Please help! John-Mark recorded the stats for several baseball pitchers. He made a scatterplot showing the
    12·1 answer
  • 1<br> What is the solution to the equation 5x + 4 =19
    15·1 answer
  • Find the volume of a pyramid with a square base, where the area of the base is 19.8 cm2 and the height of the pyramid is 13.9cm.
    13·1 answer
  • Samuel orders four DVDs from an online music store. Each DVD costs $9.11. He has a 25% discount code, and sales tax is 7%. Round
    13·2 answers
  • 14. Write an equation with vanables on each
    9·1 answer
  • Mark<br> used 6428 blocks build a castle round the number of block to the nearest tens
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!