0

def tri_recursion(k): if(k > 0): result = k + tri_recursion(k - 1) print(result) else: result = 0 return resul

The way I am reading it, with an initial input of 6, the first print of result should be 11. 6 + (6-1). What I'm struggling with i think is how the output is being calculated with the continual recursion of k-1 and what is happening here. Can someone explain this to me as simply as possible?

2nd Oct 2020, 10:00 AM
Kumar Shubham
4 ответов
+ 4
ok now return result right? input is 6 before result is printed.... calculate f(5) calculate f(4) calculate f(3) calculate f(2) calculate f(1) calculate f(0)->0 so f(1) = 1 - >print it so f(2) = 3 - > print it ...
2nd Oct 2020, 11:00 AM
Oma Falk
Oma Falk - avatar
+ 4
https://code.sololearn.com/cDPTDCT15IBf/?ref=app this is recursion with accumulation . Might fit better to what you wanted.
2nd Oct 2020, 11:08 AM
Oma Falk
Oma Falk - avatar
+ 3
don't care too much about that. It is not recursion and wont work. It is buggy if wanted to be recursion .
2nd Oct 2020, 10:16 AM
Oma Falk
Oma Falk - avatar
0
It is working and it's output is Output: Recursion Example Results 1 3 6 10 15 21
2nd Oct 2020, 10:29 AM
Kumar Shubham