Jump to content

recursion example in python for loops

Recommended Posts

I don't understand how this code from the python for loops example gets the answer.

def tri_recursion(k):
    result = k+tri_recursion(k-1)
    result = 0
  return result

print("\n\nRecursion Example Results")

The answer: 1, 3, 6, 10, 15, 21.

I'm still very new at python. I thought  the way the code would work is:

result = 6 + (6-1) = 11

With the presumption that k=6. I've clearly got it wrong and would be gratefully for someone to explain how the code works.



Share this post

Link to post
Share on other sites

Add current value plus total remaining values remaining below current.

6 + (5+4+3+2+1+0) = 21

Edited by dsonesuk

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now