Hi I am trying to understand this algorithm and I and trying to walk through it to see what values will result but not much luck in doing so. I think I don’t know if I am getting this right or not. This is the algorithm
Algorithm Fast-Fibonacci(n)
Let fib[0] and fib[1] be 1.
for each i from 2 to n, do:
Let fib[i] be fib[i - 2] + fib[i - 1].
end of loop
return fib[n].
say Fast-Fib(5)
fib[0] = 0
Fib[1] = 1
fib[2] = {2-2] + [2-1] = 1
fib[3] = [3-2] + [3-1] = 3
fib[4] = 4-2] + [4-1] = 5
then the loop quits right?
resulting in
01135 as the answer
You are accessing the elements of
fib, so forFast-Fib(5)you get:returning
fib[5](i.e. 5)Note: I updated the values according to the questions initial conditions (
fib[0]=fib[1]=1); oftenfib[0]=0;fib[1]=1