1. Add up 1 to n
Write code Easy ✓ CompletedWrite a recursive function sum_to(n) that returns 1 + 2 + ... + n. sum_to(0) should return 0. Do not use a loop and do not use the formula n * (n + 1) // 2: the function must call itself. Then print sum_to(5).
def sum_to(n):
# base case: what is the sum when n is 0?
# recursive case: n plus the sum up to n - 1
pass
print(sum_to(5))
sum_to(0) is 0. For anything bigger, sum_to(n) is n plus sum_to(n - 1).def sum_to(n):
if n <= 0:
return 0
return n + sum_to(n - 1)
print(sum_to(5))