+ 4
How to sum the squares of integers from 0 to 5?
Don't understand, please help. def sum(x): res = 0 for i in range(x): res = i*i res = i*i + i*i return res print(sum(6)) doesn't work;(
3 Answers
0
well, if you want to find the sum then use res+=i*i (res=res+i*i), just change it in your loop
+ 3
Elias Edgardo Segundo Antonio, great job! Thank you!
+ 1
you can simplify it.
first, 0*0 is always 0 so it will not affect the sum.
second, use built-in functions and generators.
def squareSum (n):
res = sum(x*x for x in range(1, n+1))
return res