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
can someone please help me with 10 and 12! this is due tomorrow and i need it done soon! thank you in advance!
aleksley [76]

You forgot to add the question

4 0
3 years ago
Help? i mark brainlist :)
Nina [5.8K]

Answer:

There are 25 students in the class.

Step-by-step explanation:

In order to find the number of students in the class, you can add the number of data points together. Because the name of each person will only be recorded once and only to one data value, that means by adding the total number of data points collected, you can find out the number of students in the sixth-grade class. There are three data points on 3, two data points on 4, five data points on 5, eight data points on 6, two data points on 7, one data point on 8, and four data points on 9, this means the total number of students will be 3 + 2 + 5 + 8 + 2 + 1 + 4 = 25 students, so this will be your answer.

7 0
3 years ago
Point A is located at (4, 1). Point B is located at (-3, 3). How did Point A move to Point B?
Marrrta [24]
I forgot but I used to know this one
6 0
3 years ago
What is equal to 7/12
Black_prince [1.1K]
14/ 24
2.5/4
3.5/6
And many moreee
8 0
3 years ago
Read 2 more answers
If it takes 4 hours to cook a 7-pound turkey, how many hours will it take to cook a 19-pound turkey?
nordsb [41]

Answer:

Roughly 11 to 11 and 1/2 hours

Step-by-step explanation:

Roughly 11 to 11 and 1/2 hours.

Hope this helps, if so please mark the brainliest, peace and love

7 0
3 years ago
Read 2 more answers
Other questions:
  • __________ is the source error that can be avoided by locating questions sensitive to bias and changing or dropping them.
    15·2 answers
  • The price of a certain new car increases by the same amount each year. The first year it was ​$24,000 and in the 5th year it was
    6·1 answer
  • Circle W has a radius of 20 in and right central angle, ∠VWX. Find the length of arc of Vx
    8·1 answer
  • What is the mean of the following set of numbers?
    5·2 answers
  • Chip and Dale have 42 marbles. chip has six more marbles than Dale .how many marbles does Chip have
    8·2 answers
  • Billy made 5 gallons of juice for a picnic. He said that he made
    13·1 answer
  • Below is the price listed for different sized pizzas
    7·2 answers
  • Of 80 shirts, 10% are white<br> 30% are blue, and the rest<br> eire Red. How many are red?
    8·1 answer
  • PLEASE HELP MATH 11. And 12.
    10·1 answer
  • Don’t answer if you don’t know
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!