Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

IntermediatePhase 6 - Category 4: Mathematical CombinationsExample 1 of 10
sum-of-squares.py
1# Take n
2n = int(input("Enter n: "))
3
4# Calculate sum of squares
5total = 0
6for i in range(1, n + 1):
7 total += i * i
8
9print(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. 1

    Initialize total zero.

  2. 2

    Visit one through n and add i * i.

  3. 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.