+ 7

Can someone tell me the execution of this code?

https://code.sololearn.com/cZ5P5k9746t8/?ref=app

13th May 2020, 6:43 AM
Punit Kumar
Punit Kumar - avatar
24 ответов
+ 6
s=011235 is the series u create s[5]=5
13th May 2020, 6:45 AM
Oma Falk
Oma Falk - avatar
+ 13
def num(n): if n==0: return 0 if n==1: return 1 else: return ((num(n-1)+num(n-2))) print (num(5)) Here, you have called the function with int 5 so n=5 num(5-1) + num(5-2) ------------------------------ | num(4) + num(3) | | num(3) + num(2)+num(2) + num(1) | | | | num(2)+num(1) | num(1)+num(0) | | | | | | | 1+0 + 1 + 1+0 + 1 + 0 + 1 if n==1: return 1 if n==0: return 0 1+0+1+1+0+1+0+1 = 5
14th May 2020, 1:29 AM
Jenson Y
+ 9
Code Crasher pencil and paper will do. you are experienced enough to get a foot into recursion. give it a try... it is fascinating
13th May 2020, 1:07 PM
Oma Falk
Oma Falk - avatar
+ 8
Punit Kumar yes it is and it takes a while. but it us worth it. Do you agree?
13th May 2020, 1:13 PM
Oma Falk
Oma Falk - avatar
+ 7
This is Fibonacci series.. A pain in the *** , sorry but it's really difficult. It's often set as an example when teaching recursion. The iterative alternative of your code is : first,second = 0,1 for i in range(n): # e.g.: n=5 first, second = second, first+second print(first)
14th May 2020, 7:25 AM
Aymen
+ 4
5
14th May 2020, 10:37 AM
Sanika
+ 3
Pen and paper worked fine for me later but it's quite complicated mentally...But you method is lit Oma Falk
13th May 2020, 1:11 PM
Punit Kumar
Punit Kumar - avatar
+ 3
Oma Falk Totally agreed 👍
13th May 2020, 1:14 PM
Punit Kumar
Punit Kumar - avatar
+ 3
PEN and PAPER will do the trick! It's simple recursion...
13th May 2020, 5:42 PM
Nikhil Jain
Nikhil Jain - avatar
+ 3
Pranav Shinde pencil and paper😉
13th May 2020, 6:28 PM
Oma Falk
Oma Falk - avatar
+ 3
It is fibonachi series 01123581321 so each number in set is a sum of 2 previous( first in set is 0 second is 1)
13th May 2020, 8:33 PM
george
george - avatar
+ 3
According to your code you will get the output as 5
14th May 2020, 2:01 PM
SARAN G
+ 3
It is a fibonacci series.
14th May 2020, 10:26 PM
Santosh Kumar Kashyap
Santosh Kumar Kashyap - avatar
+ 3
It is a recursive function which can call itself. In this situration you call it with the parameter 5 which is "n". n is not 0 or 1, so the function call itself again with the new parameters until n is 0 or 1. Be careful that the very first function called is the last one which comoletes. Just draw a dragram as Lay_in_life says.
15th May 2020, 4:46 AM
Win Htay 🇲🇲
Win Htay 🇲🇲 - avatar
+ 2
Take a pen and paper and start writing in order of execution
14th May 2020, 12:26 AM
MANSOOR PASHA
MANSOOR PASHA - avatar
+ 2
IT's 5
14th May 2020, 7:20 PM
Rob
Rob - avatar
+ 2
num(5) ↓ num(4)+num(3)→num(2)+num(1) ↓ . num(3)+num(2) ↓ num(2)+num(1)→1 ↓ num(1)+num(0) ↓ ↓ 1 0 num(2)=1 num(3)=num(2)+num(1)=1+1=2 num(4)=num(3)+num(2)=2+1=3 num(5)=num(4)+num(3)=3+2=5
14th May 2020, 8:58 PM
Golam Rabby
Golam Rabby - avatar
+ 1
5, guys above explained you why and i had a lot of fun doing it. You should try yourself also:)
14th May 2020, 9:35 PM
Adrian710
+ 1
Answer is 5 bro. num[5]=num[4]+num[3] =num[3]+num[2]+num[2]+num[1] And here given for num[1]=1 and num[0]=0 so, =num[2]+num[1]+num[1]+num[0]+num[1]+num[0]+1 =num[1]+num[0]+1+1+0+1+0+1 =1+0+1+1+0+1+0+1 =5
15th May 2020, 1:48 AM
Kaushal Kishore
Kaushal Kishore - avatar