Nagendra C.
asked 12/26/22sum of squares from m to n using python
sample input:
3
4
sample output:
50
More
1 Expert Answer

Aime F. answered 12/31/22
Tutor
4.7
(62)
Experienced University Professor of Mathematics & Data Science
The "sample output" should be 25, of course.
def sos(m,n) :
if m > n :
return 0 # convention if m > n
else :
s = m**2 # m = n case
for i in range(m+1, n+1) : # loop if n > m
s += i**2
return s
m = 3; n = 4
print(sos(m,n), # cf. explicit formula
max(0, (n*(n+1)*(2*n+1)-(m-1)*m*(2*m-1))/6))

William M.
tutor
No need for else in sos (No else for sanity check is standard industry practice) ------ use m, n = 3,4 (Pythonic approach) --- use print with fstring, it allows labeling and data values
Report
01/07/23

Aime F.
Thanks. My view is that else statements are helpful if the logic is as elementary as in this case. https://stackoverflow.com/questions/865741/else-considered-harmful-in-python
Report
01/07/23
Still looking for help? Get the right answer, fast.
Ask a question for free
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Find an Online Tutor Now
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Judy R.
Hello I am 22 years experienced Online Tutor and Assignment Helper for Computer Science and Math. I have been teaching IT Professionals, students from different grades, and Graduate and Post Graduate students for more than 22 years. I am ready to help you with your learning requirements, assignments, tests, and projects. For further discussion about the assignment or project help you need, please add me on skype and my skype id is nettuitions Thanks01/22/23