please explain the code given in the description ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

please explain the code given in the description ?

What is the result of this code? def fib(x): if x == 0 or x == 1: return 1 else: return fib(x-1) + fib(x-2) print(fib(4))

3rd Dec 2018, 11:46 AM
Priyanshu Ghatiya
Priyanshu Ghatiya - avatar
3 Answers
+ 5
Fib(4) | _______/ \________ | + | Fib(3) Fib(2) | | _______/ \____ ____/ \_______ | + | | + | Fib(2) Fib(1) Fib(1) Fib(0) _______/ \_______ | + | Fib(1) Fib(0) Fib(4) = Fib(1) + Fib(0) + Fib(1) + Fib(1) + Fib(0) = 1 + 1 + 1 + 1 + 1 = 5 Implements function -- 1 ,if n=0 or n=1 | T(n) = ---| | -- T(n-1)+T(n-2) , if n>1
3rd Dec 2018, 1:03 PM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 5
Wow, I salute your effort Prokopios Poulimenos !
3rd Dec 2018, 4:07 PM
Lambda_Driver
Lambda_Driver - avatar
+ 2
Wow, that must have taken a while! :)
3rd Dec 2018, 1:31 PM
HonFu
HonFu - avatar