Translating a recursive function into a iterator function

Hey, how do I change the following recursive function into a iterator function and basically what should it look like?

1
2
3
4
5
6
7
int f(int i)
{
    if(i<2)
       return i;

     return f(i-2)+f(i-1);
}
To handle this you need to computer the values f(3), f(4), f(5), ... f(i). Do this in a loop. Keep the previous 2 results in local variables, let's call then f1 and f2, then at each step you have result = f1+f2 and then you have to update f1 and f2 for the next step.

If you do it right it will be about 5 lines of code.

Topic archived. No new replies allowed.