Iterative Fibonacci
Keep n ≤ 15 for iterative approach.
Step: 0
Walk through the code line by line and observe variable changes.
Current depth: 0
Max height: 0
Total calls: 0
≈ Time: O(2ⁿ)
Call Stack
int F(int n) {
if (n <= 1)
return n;
int prev = 0;
int curr = 1;
int next;
for (int i = 2; i <= n; i++) {
next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}
int F(int n) {
if (n <= 1)
return n;
int left = F(n - 1);
int right = F(n - 2);
return left + right;
}
Iterative State
| n | - |
| i | - |
| prev | - |
| curr | - |
| next | - |
| iterations | 0 |
Return Value
-
Series: F(0), F(1), …, F(n)