[Solved] From recursion to iteration (loop) [closed]


Directly apply the definition of Fibonacci series:

reg

ini

To get a 1:1 translation:

int f(int N) {
    int fn = N; // Edit: init fn with N
    int fn_1 = 1;
    int fn_2 = 0;
    while (N >= 2) {
        fn = fn_1 + fn_2;
        fn_2 = fn_1;
        fn_1 = fn;
        N--;
    }
    return fn;
}

2

solved From recursion to iteration (loop) [closed]