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?
4 Réponses
+ 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
...
+ 4
https://code.sololearn.com/cDPTDCT15IBf/?ref=app
this is recursion with accumulation .
Might fit better to what you wanted.
+ 3
don't care too much about that.
It is not recursion and wont work.
It is buggy if wanted to be recursion .
0
It is working and it's output is
Output:
Recursion Example Results 1 3 6 10 15 21