Question: in java please 19. The Fibonacci sequence is a sequence of numbers in which the first two numbers are 1 and each subsequent number is

 in java please 19. The Fibonacci sequence is a sequence of

in java please

19. The Fibonacci sequence is a sequence of numbers in which the first two numbers are 1 and each subsequent number is the sum of the previous two Fibonacci numbers. The sequence is 1, 1, 2, 3, 5.8. 13, 21, 34, and so on. The following is a correct, but inefficient, method to compute the nth Fibonacci number: public static int fibonacci(int n) { if (n=2) return 1; } else { return fib(n-1) + fib(n - 2):- } The code shown runs very slowly for even relatively small values of n; it- can take minutes or hours to compute even the 40th or 50th Fibonacci- number. The code is inefficient because it makes too many recursive calls. It ends up recomputing each Fibonacci number many times. Write a new version of this method that is still recursive and has the same header but is more efficient. Do this by creating a helper method that accepts additional parameters, such as previous Fibonacci numbers, that- you can carry through and modify during each recursive call

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!