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
You buy a pair of jeans (j) that cost $12, two pair of socks (s) for $2 each, and three tops (t) that cost $6 each. Which expres
mars1129 [50]

Answer:

Step-by-step explanation:

1*12 + 2*2 + 3*6 = 12 + 4 +18 = $34

4 0
3 years ago
Sam borrowed $2,000,000 for a
Daniel [21]

The total amount Sam will pay in all will be $2,400,000

<h3>Simple interest </h3>

From the question, we are to determine the interest Sam pays in all

Using the simple interest formula,

I = PRT

From the given information,

P = $2,000,000

R = 5% = 0.05

T = 4

Putting the parameters into the formula,

I = $2000000 × 0.05 × 4

I = $400000

The interest Sam will pay is $400,000

The total amount Sam will pay in all will be $2,000,000 + $400,000

= $2,400,000

Hence, the total amount Sam will pay in all will be $2,400,000

Learn more on Simple interest here: brainly.com/question/16134508

#SPJ1

4 0
2 years ago
The probability of a dry summer is equal to 0.3, the probability of a wet summer is equal to 0.2, and the probability of a summe
brilliants [131]

Answer:

s

Step-by-step explanation:

4 0
3 years ago
A commercial jet and a private airplane fly from Denver to Phoenix. It takes the commercial jet 1.1 hours for the flight, and it
FinnZ [79.3K]

Answer:

The speed of the private airplane is 770 mph, the speed of the commercial airplane is 980 mph.

Step-by-step explanation:

Let x mph be the speed of private airplane. If the speed of the commercial jet is 210 miles per hour faster than the speed of the private airplane, then the speed of commercial airplane is (x+210) mph.

It takes the commercial jet 1.1 hours for the flight, then it covered the distance of

1.1(x+210)\ miles

It takes the private airplane 1.8 hours for the flight, then it covered the distance of

1.8x\ miles.

The distances are the same, so

1.1(x+210)=1.8x\\ \\1.1x+231=1.8x\\ \\1.8x-1.1x=231\\ \\0.7x=231\\ \\7x=2,310\\ \\x=770

The speed of the private airplane is 770 mph, the speed of the commercial airplane is 770 + 210 = 980 mph.

3 0
3 years ago
Read 2 more answers
At Buffalo Mild Wings, the price of chicken wings depends on the number of wings that you order.
timofeeve [1]

Answer: no

Step-by-step explanation:

4 0
3 years ago
Other questions:
  • What number is halfway between 100,000 and 200,000
    6·1 answer
  • Emily buys a toaster for 10%off if she pays 36 then what was the original price
    9·2 answers
  • What is the definition of numerical expression
    9·2 answers
  • Problem Page Write an inequality for the following statement. 1 is less than or equal to c
    6·1 answer
  • Find the number of pancakes Sandra can make in 20 minutes if she takes half an hour to prepare six pancakes at a constant rate
    11·1 answer
  • 7/8 multiplied by 33/8 divided by 7/4=​
    15·1 answer
  • What is 150% as a fration simplest form
    8·2 answers
  • Solve for a.<br> 7a + 18 = 102<br> a =
    8·2 answers
  • How many mph is 32 miles in 4 hours
    13·2 answers
  • Write 2.5896 * 10^-4 in standard form
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!