Sum of Squares Series Mathematical Logic
Python Code
n = int(input("Enter a positive integer n: "))
s = 0
for i in range(1, n + 1):
term = i ** 2
s += term
print("Sum of the series is:", s)
Step-by-Step Explanation
1. Taking User Input:
n = int(input(...)) takes the integer limit n up to which square values will be calculated.
2. Initializing Accumulator:
s = 0 initializes a variable to keep track of the cumulative sum of squares.
3. The For Loop Setup:
for i in range(1, n + 1) generates integer values from 1 up to n inclusive.
4. Calculating Term Power:
term = i ** 2 squares the current number i (1², 2², 3², …).
5. Accumulating Total:
s += term adds the calculated square term into the running total variable s.
6. Displaying Output:
print(...) displays the final sum of the squared series on the screen.
Worked Example: Input n = 4
Series Formula: 1² + 2² + 3² + 4²
Initial State: n = 4, s = 0
| Iteration (i) | Term (i ** 2) | Calculation | s += term (Running Sum) |
|---|---|---|---|
| 1 | 1 ** 2 | 1 | 0 + 1 = 1 |
| 2 | 2 ** 2 | 4 | 1 + 4 = 5 |
| 3 | 3 ** 2 | 9 | 5 + 9 = 14 |
| 4 | 4 ** 2 | 16 | 14 + 16 = 30 |
Final Output: Sum of the series is: 30