Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for Sum of squares of first n natural numbers
In this article, we will learn how to calculate the sum of squares of the first n natural numbers. Given a positive integer N, we need to compute 1² + 2² + 3² + ... + N².
Problem Statement
We need to find the sum of squares: 1² + 2² + 3² + ... + N² for a given positive integer N. This problem can be solved using two approaches ?
- Iterative approach using a loop
- Direct mathematical formula
Method 1: Using Loop (Iterative Approach)
In this method, we iterate from 1 to n and calculate the square of each number, adding it to our sum ?
def square_sum_iterative(n):
total = 0
for i in range(1, n + 1):
total = total + pow(i, 2)
return total
# Example usage
n = 5
result = square_sum_iterative(n)
print(f"Sum of squares of first {n} natural numbers: {result}")
The output of the above code is ?
Sum of squares of first 5 natural numbers: 55
Method 2: Using Mathematical Formula
The sum of squares of first n natural numbers follows the mathematical formula ?
def square_sum_formula(n):
return (n * (n + 1) * (2 * n + 1)) // 6
# Example usage
n = 10
result = square_sum_formula(n)
print(f"Sum of squares of first {n} natural numbers: {result}")
# Verify with smaller example
n = 5
result = square_sum_formula(n)
print(f"Sum of squares of first {n} natural numbers: {result}")
The output of the above code is ?
Sum of squares of first 10 natural numbers: 385 Sum of squares of first 5 natural numbers: 55
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Iterative Loop | O(n) | O(1) | Understanding the concept |
| Mathematical Formula | O(1) | O(1) | Large values of n |
Complete Example
Here's a complete program demonstrating both methods ?
def square_sum_iterative(n):
"""Calculate sum using iterative approach"""
total = 0
for i in range(1, n + 1):
total += i * i
return total
def square_sum_formula(n):
"""Calculate sum using mathematical formula"""
return (n * (n + 1) * (2 * n + 1)) // 6
# Test both methods
n = 6
method1 = square_sum_iterative(n)
method2 = square_sum_formula(n)
print(f"For n = {n}:")
print(f"Iterative method: {method1}")
print(f"Formula method: {method2}")
print(f"Both methods give same result: {method1 == method2}")
# Manual verification: 1² + 2² + 3² + 4² + 5² + 6²
manual = 1 + 4 + 9 + 16 + 25 + 36
print(f"Manual calculation: {manual}")
The output of the above code is ?
For n = 6: Iterative method: 91 Formula method: 91 Both methods give same result: True Manual calculation: 91
Conclusion
The mathematical formula approach is more efficient with O(1) time complexity, while the iterative method helps understand the concept better. For large values of n, always prefer the formula method for optimal performance.
---