18 Feb 2022

Posted on:

17 Feb 2022

0

Hello thanks for the lecture.
Are we getting the total of each recursion at the end of the process?
Cheers!

Instructor
Posted on:

18 Feb 2022

3

Hey,

I am not sure if I completely understand it, but let me answer in the following way.

Let's consider the example with the sum of the first n Fibonacci numbers, let's for instance set n = 3. To return the sum of the first 3 Fibonacci numbers, you'd call the function fib_2(3).

Phase 1:
Entering the fib_2(3) function, you would do the following:
1. check whether n = 0, which it is not;
2. check whether n = 1, which it is not;
3. call the functions fib_2(2) and fib_2(1).

Phase 1.1:
Let's enter the function fib_2(2) which is part of the output from Phase 1. You would do the following:
1. check whether n = 0, which it is not;
2. check whether n = 1, which it is not;
3. call the functions fib_2(1) and fib_2(0).

Phase 1.1.1:
Let's enter the function fib_2(1) which is part of the output from Phase 1.1. You would do the following:
1. check whether n = 0, which it is not;
2. check whether n = 1, which it, so this function would return a 1;

Phase 1.1.2:
Let's enter the function fib_2(0) which is part of the output from Phase 1.1. You would do the following:
1. check whether n = 0, which it is, so this function would return a 0;

Therefore, the result of the function fib_2(2) from Phase 1.1 is 1 + 0 = 1.

Phase 1.2:
Let's enter the function fib_2(1) which is part of the output from Phase 1. You would do the following:
1. check whether n = 0, which it is not;
2. check whether n = 1, which it is, so this function would return a 1;

Therefore, the result of the function fib_2(3) from Phase 1 is fib_2(2)fib_2(1) = 1 + 1 = 2.

I hope my notation makes sense to you and that I also managed to answer your question :)

Kind regards,
365 Hristina