n
is 0 or 1, return n
as it is the base case.prev1
and prev2
, to represent the last two Fibonacci numbers. Initially, prev1
is set to 1 and prev2
to 0.n
, and for each iteration:
a. Calculate the current Fibonacci number as the sum of prev1
and prev2
.
b. Update prev2
to the value of prev1
.
c. Update prev1
to the value of the current Fibonacci number.prev1
which now holds the value of F(n)
.