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

Another question on Mathematics

question
Mathematics, 21.06.2019 13:30
In the field of thermodynamics, rankine is the unit used to measure temperature. one can convert temperature from celsius into rankine using the formula c= r/1.8 - 273, what is the temperature in celcius corresponding to r degrees rankine.
Answers: 2
question
Mathematics, 21.06.2019 17:30
In parallelogram abcd the ratio of ab to bcis 5: 3. if the perimeter of abcd is 32 find ab
Answers: 1
question
Mathematics, 21.06.2019 20:00
Donald has x twenty-dollar bills and 11 ten-dollar bill. how much money does donald have? write your answer as an expression.
Answers: 2
question
Mathematics, 21.06.2019 20:40
Lines a and b are parallel. what is the value of x? -5 -10 -35 -55
Answers: 2
You know the right answer?
The fundamental source of the inefficiency is not the fact that recursive calls are being made, but...
Questions
question
Mathematics, 10.09.2019 06:10
question
Mathematics, 10.09.2019 06:10
question
Mathematics, 10.09.2019 06:10
Questions on the website: 13722367