Daily Coding Challenge - Sum of Squares

Tell us what’s happening:

Could someone explain why this recursive function works correctly up to n=997 but has a maximum recursion limit beyond that? Does this mean we can’t solve this problem recursively or am I missing something obvious? Thanks!

Your code so far

def sum_of_squares(n):
    if n == 1:
        return 1;
    result = n*n + sum_of_squares(n-1)
    return result

print(sum_of_squares(997))

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36

Challenge Information:

Daily Coding Challenge - Sum of Squares

https://www.freecodecamp.org/learn/daily-coding-challenge/2025-08-19

If I recall correctly, I think the browser gets upset if the call stack is too big? @ArielLeslie knows better than I tho.

Fun side note - there is as zero loop/no recursion solution for this one!

2 Likes

Yes, that makes sense. I’ll get my maths head on.

Would it make sense to change test 5 to a number slightly lower than 1000, so that there aren’t recursion limit problems?

5. sum_of_squares(1000) should return 333833500.

I suspect that 1000 was chosen specifically to force this issue, if I was to guess :slight_smile:

1 Like

this is ‘tricky effective learning’ :slight_smile: the previous exercise involved a recursion, so you build a recursion here too….then u see that you do not need a recursion at all…but at the end you learnt double