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
Find the sum of cubes of the first N natural numbers in C#
We are given a number N, and we need to calculate the sum of the cubes of the first N natural numbers. In this article, we are going to learn how we can find the sum of cubes of the first N natural numbers in C#.
Problem Description
The task is to compute the sum: 1³ + 2³ + 3³ + ... + N³ for a given positive integer N.
Example 1
- Input: N = 3
- Output: 36
Explanation:
The cubes of the first 3 natural numbers are
1³ = 1, 2³ = 8, 3³ = 27
The sum of these cubes is: 1 + 8 + 27 = 36
Example 2
- Input: N = 5
- Output: 225
Explanation:
The cubes of the first 5 natural numbers are
1³ = 1, 2³ = 8, 3³ = 27, 4³ = 64, 5³ = 125
The sum of these cubes is: 1 + 8 + 27 + 64 + 125 = 225
Using Iterative Approach
This is a simple and direct approach to find the sum of cubes. We use a loop to calculate the cube of each number from 1 to N and add it to a cumulative sum variable.
Steps for Implementation
- Initialize a variable sum to 0.
- Loop through each number from 1 to N.
- For each number, calculate its cube and add it to the sum.
- Return the final value of the sum.
Example
using System;
class Program {
static int SumOfCubesIterative(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i * i * i;
}
return sum;
}
static void Main() {
int N = 3;
int result = SumOfCubesIterative(N);
Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
N = 5;
result = SumOfCubesIterative(N);
Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
}
}
The output of the above code is
The sum of cubes of the first 3 natural numbers is: 36 The sum of cubes of the first 5 natural numbers is: 225
Time Complexity: O(N)
Space Complexity: O(1)
Using Formula-Based Approach
In this method, we use a mathematical formula to find the sum of cubes without iterating through numbers. The formula for the sum of cubes of the first N natural numbers is
Sum = [N × (N + 1) / 2]²
This formula is based on the mathematical identity that the sum of cubes equals the square of the sum of natural numbers.
Steps for Implementation
- Calculate the sum of first N natural numbers using N × (N + 1) / 2.
- Square the result to get the sum of cubes.
- Return the final result.
Example
using System;
class Program {
static int SumOfCubesFormula(int n) {
// Calculate sum using the formula: [n*(n+1)/2]²
int sumOfNumbers = n * (n + 1) / 2;
return sumOfNumbers * sumOfNumbers;
}
static void Main() {
int N = 5;
int result = SumOfCubesFormula(N);
Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
N = 4;
result = SumOfCubesFormula(N);
Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
}
}
The output of the above code is
The sum of cubes of the first 5 natural numbers is: 225 The sum of cubes of the first 4 natural numbers is: 100
Time Complexity: O(1)
Space Complexity: O(1)
Using Recursive Approach
In this approach, we use recursive algorithm to calculate the sum of cubes. For each recursive call, we add the cube of the current number to the result of the remaining numbers.
Steps for Implementation
- Define a recursive function SumOfCubesRecursive.
- Define a base case: If n = 0, return 0.
- For recursive case return cube of n + recursive function(n - 1), i.e., n³ + SumOfCubesRecursive(n - 1).
Example
using System;
class Program {
static int SumOfCubesRecursive(int n) {
// Base case
if (n == 0) return 0;
// Recursive case: n³ + sum of cubes of (n-1) numbers
return n * n * n + SumOfCubesRecursive(n - 1);
}
static void Main() {
int N = 4;
int result = SumOfCubesRecursive(N);
Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
N = 3;
result = SumOfCubesRecursive(N);
Console.WriteLine("The sum of cubes of the first {0} natural numbers is: {1}", N, result);
}
}
The output of the above code is
The sum of cubes of the first 4 natural numbers is: 100 The sum of cubes of the first 3 natural numbers is: 36
Time Complexity: O(N)
Space Complexity: O(N) due to recursive call stack
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Iterative | O(N) | O(1) | Simple implementation, moderate N values |
| Formula-based | O(1) | O(1) | Large N values, best performance |
| Recursive | O(N) | O(N) | Educational purposes, small N values |
Conclusion
The sum of cubes of first N natural numbers can be calculated using three different approaches in C#. The formula-based approach [N×(N+1)/2]² is the most efficient with O(1) complexity, while iterative and recursive approaches provide easier-to-understand implementations with O(N) complexity.
