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
Calculate the sum of squares of the first N natural numbers in C#
In this problem, we are given a number N, and we need to calculate the sum of the squares of the first N natural numbers. The first N natural numbers are 1, 2, 3, ..., N, and we need to find 1² + 2² + 3² + ... + N².
Problem Description
Given a positive integer N, calculate the sum of squares of the first N natural numbers using different approaches in C#.
Example 1
- Input: N = 4
- Output: 30
Explanation:
The squares of the first 4 natural numbers are: 1², 2², 3², 4² = 1, 4, 9, 16.
The sum is: 1 + 4 + 9 + 16 = 30.
Example 2
- Input: N = 6
- Output: 91
Explanation:
The squares of the first 6 natural numbers are: 1, 4, 9, 16, 25, 36.
The sum is: 1 + 4 + 9 + 16 + 25 + 36 = 91.
Using an Iterative Approach
This is a straightforward approach where we iterate through the numbers from 1 to N, calculate their squares, and sum them up
using System;
class Program {
static int SumOfSquaresIterative(int N) {
int sum = 0;
for (int i = 1; i <= N; i++) {
sum += i * i; // Add square of the number to sum
}
return sum;
}
static void Main() {
int N = 4;
int result = SumOfSquaresIterative(N);
Console.WriteLine("The sum of squares of the first {0} natural numbers is: {1}", N, result);
// Test with another value
N = 6;
result = SumOfSquaresIterative(N);
Console.WriteLine("The sum of squares of the first {0} natural numbers is: {1}", N, result);
}
}
The output of the above code is
The sum of squares of the first 4 natural numbers is: 30 The sum of squares of the first 6 natural numbers is: 91
Time Complexity: O(N)
Space Complexity: O(1)
Using Mathematical Formula
This approach uses the mathematical formula to calculate the sum directly in O(1) time. The sum of squares of the first N natural numbers follows the formula
Sum = N × (N + 1) × (2N + 1) / 6
using System;
class Program {
static int SumOfSquaresFormula(int N) {
return (N * (N + 1) * (2 * N + 1)) / 6; // Mathematical formula
}
static void Main() {
int N = 4;
int result = SumOfSquaresFormula(N);
Console.WriteLine("Using formula for N={0}: {1}", N, result);
N = 6;
result = SumOfSquaresFormula(N);
Console.WriteLine("Using formula for N={0}: {1}", N, result);
N = 10;
result = SumOfSquaresFormula(N);
Console.WriteLine("Using formula for N={0}: {1}", N, result);
}
}
The output of the above code is
Using formula for N=4: 30 Using formula for N=6: 91 Using formula for N=10: 385
Time Complexity: O(1)
Space Complexity: O(1)
Using Recursive Approach
In this approach, we use recursion to calculate the sum of squares. The base case is when N = 0, and the recursive case adds the square of N to the sum of squares of the first N-1 numbers
using System;
class Program {
static int SumOfSquaresRecursive(int N) {
// Base case: if N is 0, sum is 0
if (N == 0)
return 0;
// Recursive case: N² + sum of squares of (N-1)
return (N * N) + SumOfSquaresRecursive(N - 1);
}
static void Main() {
int N = 5;
int result = SumOfSquaresRecursive(N);
Console.WriteLine("Using recursion for N={0}: {1}", N, result);
N = 3;
result = SumOfSquaresRecursive(N);
Console.WriteLine("Using recursion for N={0}: {1}", N, result);
}
}
The output of the above code is
Using recursion for N=5: 55 Using recursion for N=3: 14
Time Complexity: O(N)
Space Complexity: O(N) due to recursive call stack
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best Use Case |
|---|---|---|---|
| Iterative | O(N) | O(1) | Small to medium values of N |
| Mathematical Formula | O(1) | O(1) | Large values of N, optimal performance |
| Recursive | O(N) | O(N) | Educational purposes, small N values |
Common Use Cases
- Statistical Calculations: Used in computing variance and standard deviation in data analysis.
- Physics Simulations: Calculating kinetic energy, potential energy levels, and work done by forces.
- Machine Learning: Error calculations in regression analysis and optimization problems.
- Computer Graphics: Distance calculations and geometric transformations.
Conclusion
The sum of squares of first N natural numbers can be calculated using three main approaches: iterative (O(N)), mathematical formula (O(1)), and recursive (O(N)). The mathematical formula approach is most efficient for large values of N, while the iterative approach provides a good balance of simplicity and performance.
