Question & Answer: The recursive function for calculating a was horribly inefficient because of repeated calls for following. Instead o…..

Fil stam n der “de- … dim thhe se vdr The (10 points) The recursive function for caleulating was horribly inefficient because of repested calle for following. Instead of caleulating the usual sequece, is previous 3 values instead of just 2 values modi with dem not (a) What does it display if n is 6? Note that each f-… semicolon, so something will be printeodf (b) Show the calling tree (illustrating the nested calls). function f fibonacci(n) BaveF = zeros ( 1, n); saveF(1:3) - [11 13 f % Note: Nested function so saver is sees oreryvin % Might display here % Might display here fibo (n) function f-fibo(n) ifn<4 f=1 r saveF (n) f elseif saveF(n) else fibo (n-3) fibo(n-2) fibo (n-1) % Might display here - + + saveF (n)-f end end end Here is the original inefficient version: function x - fibo (n) ifn<= 2 x = 1; else fibo(n-2) + fibo(n-1); x = end

The recursive function for calculating a was horribly inefficient because of repeated calls for following. Instead of calculating the usual sequence, is previous 3 values instead of just 2 values. (a) What does it display if n is 6? Note that each f = semicolon, so something will be printed! (b) Show the calling tree (illustrating the nested calls). function f = fibonacci(n) saveF = zeros (1, n): saveF(1: 3) = [1 1 1]: f = fibo(n) function f = fibo(n) if n 0 f = saveF (n) else f = fibo (n – 3) + fibo (n – 2) + fibo (n – 1) saveF = f end end end Here is the original inefficient version: function x = fibo (n) if n

Expert Answer

 

The function is designed to print the fibonacci series which goes like ,

Don't use plagiarized sources. Get Your Custom Essay on
Question & Answer: The recursive function for calculating a was horribly inefficient because of repeated calls for following. Instead o…..
GET AN ESSAY WRITTEN FOR YOU FROM AS LOW AS $13/PAGE
Order Essay

0 1 1 2 3 5 8 …… and so on.

Note:- There is a slight argument on whether zero should be included in the series or not but in the end its up to you.Use what ever you like.

a) This gives the fibonacci number at that position ‘n’. As here in this case n = 6 , so the answer is 5.

b) As its is visible in the question that it uses recursion technique for calculating the fibonacci solution and relayes on the first three numbers ans break points, so for fibonacci(6) the tree structure is going to be.

fibonacci <–> fibo :- Just to make the answer shorter to write i am using the name fibo instead of fibonacci.

fibo(6) = fibo(6-3)+fibo(6-2)+fibo(6-1)

fibo(3) => f =1

fibo(4) = fibo(1)+fibo(2)+fibo(3) => f=1+f=1+f=1

fibo(5) =  fibo(4)+fibo(3)+fibo(2) => fibo(4)+f=1+f=1 =>  f=1+f=1+f=1 +f=1+f=1 (using fibo(4) from above.)

Still stressed from student homework?
Get quality assistance from academic writers!