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
The function is designed to print the fibonacci series which goes like ,
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.)