The following is Algorithm 8 from §5.4. Note that it uses the following definition of the fibonacci sequence: fn = fn−1 + fn−2, f1 = 1, f0 = 0.
procedure iterative fibonacci(n: nonnegative integer)
if n = 0 then return 0
else
x := 0 y := 1
for i := 1 to n − 1 do
z := x + y x := y y := z
end for
return y
end if
end procedure
Prove this algorithm is correct.
Get Answers For Free
Most questions answered within 1 hours.