0
fib(num-1) + fib(num-2)
I'm confused about order of operations I guess. If num = 4, you would do the parenthesis first right? and then you would get 3 and and add them?????? I know the output should be 3, but I don't know why... Can someone please tell me what the computer is doing when it gets this code????? I'm just a beginner so I'm not too good at this...
2 Answers
+ 1
to get fib(4) you need to get sum of fib(3) and fib(2)
to get fib(3) you need to get sum of fib(2) and fib(1)
to get fib(2) you need to get sum of fib(1) and fib(0)
to get fib(1) your function returns 1
to get fib(0) your function returns 0
Now you can calculate:
fib(2) = 0 + 1 = 1
fib(3) = 1 + 1 = 2
fib(4) = 1 + 2 = 3
:)
0
You're thinking about it too hard. If num is 4, then fib(num-1) is the same as fib(3), and fib(num-2) is the same as fib(2). It's basically the same thing as adding the third and second Fibonacci numbers, or 2 and 1 respectively, to get 3 (the fourth Fibonacci number).