subject
Mathematics, 12.03.2020 05:42 austinhamy1

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.

ansver
Answers: 1

Other questions on the subject: Mathematics

image
Mathematics, 21.06.2019 13:00, babyduckies37
In a box lenght 1m breadth 60 cm and height 40 cm small rectangular packets of dimension 5 cm into 8 cm into 10 cm are packed. how many such small packets can be packed in the big box?
Answers: 2
image
Mathematics, 21.06.2019 21:20, mirmir62
If f(x) = 4 - x2 and g(x) = 6x, which expression is equivalent to (g-f)(3)? 06-3-(4 + 3)2 06-3-(4-33 6(3) - 4 +32 6(3) -4 -32
Answers: 1
image
Mathematics, 21.06.2019 22:30, Carlosanddana123
What is the approximate value of  x  in the equation below.–3.396–0.7080.3040.955
Answers: 1
image
Mathematics, 22.06.2019 01:00, rrgg6234
(05.02) mr. morris is going to save money and replace his sailboat's mainsail himself. he must determine the area of the mainsail in order to buy the correct amount of material. calculate the area of the parallelogram to determine how much material should be purchased. be sure to explain how to decompose this shape into rectangles and triangles. describe their dimensions and show your work.
Answers: 1
You know the right answer?
The fundamental source of the inefficiency is not the fact that recursive calls are being made, but...

Questions in other subjects:

Konu
Mathematics, 04.10.2021 14:10
Konu
Mathematics, 04.10.2021 14:10