+ 2

Please explain this code with the process that is taking place here

I want to understand logically what is really happening there. I went through other questions in here but don't understand them in clarity. Explain with this def fib(num): if num==1 or num==0: return 1 else: return fib(num-2)+fib(num-1)

6th Jan 2020, 7:36 PM
Vuyisile Lucas Ncipha
Vuyisile Lucas Ncipha - avatar
3 ответов
+ 2
Edited 3 to 5: Vuyisile Lucas Ncipha For ex: n=5. fib(5) =>fib(5-2) +fib(5-1)= fib(3)+fib(4) =>[ fib(3-2) + fib(3-1) ] +[ fib(4-2)+fib(4-1) ] = >[ fib(1) + fib(2)] +[fib(2) +fib(3)] =>1 + fib(2-2)+ fib(2-1) +fib(2-2)+fib(2-1) +[ fib(1) + fib(2)] =>1+fib(0)+fib(1)+ fib(0)+fib(1) + [ 1 + fib(2-2)+ fib(2-1)] =>1+1+1+1+1+ [ 1+ fib(0)+ fib(1)] =>5+[ 1+1+1] => 5+3 = 8.
6th Jan 2020, 7:57 PM
Jayakrishna 🇮🇳
+ 2
Def instruction. A function in python is an object that takes arguments and returns a value. The function is usually defined using the def statement. In your example is a function "fib" that takes the argument "num" if num is 1 or 0, the function returns a value 1 if otherwise, then returns value (num-2)+(num-1)
6th Jan 2020, 7:53 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
I am trying how did the it give 8 for fib(5). I want to know the step by process for it to reach 8
6th Jan 2020, 7:57 PM
Vuyisile Lucas Ncipha
Vuyisile Lucas Ncipha - avatar