I need to write a function that is the recursive version of summing up numbers 1 through n. It needs to be recursive, which I have no idea how to do, although I did the iterative version quite easily.
All I know about recursion is that you call the function in the function. Any help on where to get started is greatly appreciated.
Here is the iterative version I did.
def summ(n):
result = 0
for i in range(1,n+1,1):
result = result + i
return result
As always with recursive functions, define a base case and a recursive case, then implement these in a function that checks whether it’s reached the base case. There are various recursive algorithms for this problem, one of which is:
Base case.
n==1. The sum is trivial.Recursive case. If you have the sum of the numbers through
n, how do you get the sum throughn+1?