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 14:30
Tanya has $42 to spend at the dolphin island store. tshirts sell for $7 each and a pair of sunglases sells for $6.tanya buys 3 tshirts. how many pairs of sunglases can she buys with the amount of money she has left
Answers: 3
question
Mathematics, 21.06.2019 18:30
Players on the school soccer team are selling candles to raise money for an upcoming trip. each player has 24 candles to sell. if a player sells 4 candles a profit of$30 is made. if he sells 12 candles a profit of $70 is made. determine an equation to model his situation?
Answers: 3
question
Mathematics, 21.06.2019 22:20
Which is the focus of a parabola with equation y2=4x
Answers: 1
question
Mathematics, 21.06.2019 23:10
Which graph represents the linear function below? y-4= (4/3)(x-2)
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, 23.10.2020 19:40
question
Mathematics, 23.10.2020 19:40
Questions on the website: 13722367