Saturday, June 11, 2016

python recursive function

http://stackoverflow.com/questions/479343/how-can-i-build-a-recursive-function-in-python

def a(n):
    if n == 0: return 0
    else: return n + a(n)

The two key elements of a recursive algorithm are:
  • The termination condition: n == 0
  • The reduction step where the function calls itself with a smaller number each time: factorial(n - 1)

No comments:

Post a Comment