+ 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;(

21st Nov 2016, 4:57 PM
Daniil Ivanov
Daniil Ivanov - avatar
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
21st Nov 2016, 5:36 PM
Elias Edgardo Segundo Antonio
Elias Edgardo Segundo Antonio - avatar
+ 3
Elias Edgardo Segundo Antonio, great job! Thank you!
22nd Nov 2016, 9:57 AM
Daniil Ivanov
Daniil Ivanov - avatar
+ 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
21st Nov 2016, 7:09 PM
Demeth
Demeth - avatar