Sum of Squares
Sum integer squares through n as a final accumulation exercise. Students trace one square per term and compare the loop with the closed-form result if desired.
1# Take n2n = int(input("Enter n: "))34# Calculate sum of squares5total = 06for i in range(1, n + 1):7 total += i * i89print(f"Sum of squares: {total}")
Output
Enter n: 5 Sum of squares: 55
In this one
- Build a square total
- Include n as a term
- Use direct accumulation
How it works
- 1
Initialize total zero.
- 2
Visit one through n and add i * i.
- 3
Print the completed sum.
Edge cases
What if n = 0?
What if n = 0?
The total stays zero.
Is this the square of the natural-number sum?
Is this the square of the natural-number sum?
No. Each term is squared before addition.
What's going on
total begins at zero. Each i from one through n contributes i * i, so the final sum combines square values, not original values.
For four terms, 1 + 4 + 9 + 16 gives thirty. The loop computes each contribution explicitly instead of using a formula. Values below one produce an empty range and zero; the code does not label negative limits invalid.
What if n = 0? The total stays zero.
Is this the square of the natural-number sum? No. Each term is squared before addition.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does n = 4 produce?
Pick an answer to check it.
Question 2
What if n = 0?
Pick an answer to check it.
Questions people ask
What if n = 0?
The total stays zero.
Is this the square of the natural-number sum?
No. Each term is squared before addition.
What does n = 4 produce?
30.
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum