In [1]:
>>> def triangle(n):
... total = n
... if n > 0:
... total += triangle(n - 1)
... return total
...
In [2]:
triangle(4)
Out[2]:
In [3]:
def triangle(n):
if n <= 0:
return 0
return n + triangle(n - 1)
In [4]:
triangle(4)
Out[4]:
In [5]:
def triangle(n):
if n > 0:
return n + triangle(n - 1)
return 0
In [6]:
triangle(4)
Out[6]:
One line, but ugly, so don't do this.
In [7]:
>>> def triangle(n):
... return n + (triangle(n-1) if n>0 else 0)
In [8]:
triangle(4)
Out[8]:
In [9]:
import sys
In [10]:
sys.getrecursionlimit()
Out[10]:
In [11]:
sys.setrecursionlimit(100000)
In [12]:
sys.getrecursionlimit()
Out[12]: