+ 1
Hello I'm new in this community just joined didn't know how to ask question but below is my question?
In this two recursive function I'm not able to get how the procedure performing and return the result as 55 in one and 0 in other can someone experienced programmer can explain this two codes working... https://code.sololearn.com/clpNsbAzE2G9/?ref=app https://code.sololearn.com/c1IlhI8EBwv0/?ref=app
3 Réponses
+ 2
5 is too big, isn't it? Let's start from 1 :).
P2:
func2(1)
-> 0 + func1(func2(0))
-> 0 + func1(0) //func2(0) returns 0
-> 0 + 0 //func1(0) returns 0
-> 0
So func2(1) == 0
Let's increase the value to 2!
func2(2)
-> 0 + func1(func2(1))
From the example above, we know func2(1) = 0.
-> 0 + func1(0)
-> 0 + 0 = 0
So func2(2) is 0 and so on... func2( anyValue ) is 0.
P1: let i1 be i in fun1, i2 be i in fun2
fun2(1)
-> 0 + fun1(1) //i1 becomes 1 from 0
-> 0 + 1 = 1 //i2 becomes 1 from 0
So fun2(1) = 1
fun2(2)
-> 0 + fun1(2)... //i1 becomes 2 from 0
-> 0 + 2 = 2 //i2 becomes 2 from 0
-> 2 + fun1(1) //i1 becomes 3 from 2
-> 2 + 3 = 5 //i2 becomes 5 from 2
So fun2(2) = 5
From that we know fun2(n) is fun1(n) + fun1(n-1) + ... + fun(1)
where fun1(n) is i1 + n + n-1 + ... + 1
fun2(5) = fun1(5) + fun1(4) + fun1(3) + fun(2) + fun(1)
= 5
+5+4 //i1 is 5
+5+4+3 //i1 is 5+4
+5+4+3+2 //i1 is 5+4+3
+5+4+3+2+1 //i1 is 5+4+3+2
= 25 + 16 + 9 + 4 + 1
= 55
+ 1
I'm not all that with recursions, I'll just share you the tips for Q&A forum, and leave the answer for a more knowledgeable friend.
Oh, and welcome to SoloLearn ... almost forgot.
https://www.sololearn.com/Discuss/1316935/?ref=app
https://www.sololearn.com/Discuss/333866/?ref=app
+ 1
Thanks for the explanation CarrieForle.
Thanks for guideline links Ipang