I have to write a recursive function sumSquares() that takes a non-negative (>= 0) integer n as a parameter and returns the sum of the squares of the numbers between 1 and n. Example:
>>>sumSquares(2)
5
>>>sumSquares(3)
14
This is what I have so far:
def sumSquares(n):
if n==0:
return 0
else:
return sumSquares(n-1)+sumSquares(n-1)
Can I also have an explanation of what you did, I’m still in the process of learning recursion, and it would help a lot. Thanks.
Say you represent your function by S(x), x>0
this can be written as
Now for the program.
But the problem is this doesn’t know when to stop. We have to give a base case that tells it when to stop.
You know that S(0) = 0 or S(1) = 1.
therefore