def f(n, i=1, res=0): res += i ** 3 if i == n: return res return f(n, i+1, res) print(f(3))
>>> def f(n): ... assert n > 0 ... if n > 1: ... return n ** 3 + f(n - 1) ... return 1 ... >>> f(3) 36 >>> f(2) 9 >>> f(1) 1 >>> f(0) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in f AssertionError >>>
reduce(lambda x, res: x+res, [i**3 for i in range(1, 10)])