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
Sum of the series 1.2.3 + 2.3.+ ... + n(n+1)(n+2) in C
Find the sum up to n terms of the series: 1.2.3 + 2.3.4 + ... + n(n+1)(n+2). In this series, 1.2.3 represents the first term and 2.3.4 represents the second term.
Let's see an example to understand the concept better −
Input: n = 5 Output: 420
Explanation
1.2.3 + 2.3.4 + 3.4.5 + 4.5.6 + 5.6.7 = 6 + 24 + 60 + 120 + 210 = 420
The nth term = n(n+1)(n+2); where n = 1, 2, 3, ...
Expanding: n(n+1)(n+2) = n(n² + 3n + 2) = n³ + 3n² + 2n
Using the standard summation formulas −
- ?n = n(n+1)/2
- ?n² = n(n+1)(2n+1)/6
- ?n³ = n²(n+1)²/4
The required sum = ?n³ + 3?n² + 2?n
After algebraic simplification: Sum = n(n+1)(n+2)(n+3)/4
Syntax
sum = n * (n + 1) * (n + 2) * (n + 3) / 4
Method 1: Using Mathematical Formula
This approach directly applies the derived formula for O(1) time complexity −
#include <stdio.h>
int main() {
int n = 6;
int sum = n * (n + 1) * (n + 2) * (n + 3) / 4;
printf("The sum is: %d", sum);
return 0;
}
The sum is: 756
Method 2: Using Iterative Loop
This approach calculates each term and adds them iteratively −
#include <stdio.h>
int main() {
int n = 6;
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i * (i + 1) * (i + 2);
}
printf("The sum is: %d", sum);
return 0;
}
The sum is: 756
Comparison
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Mathematical Formula | O(1) | O(1) | Large values of n |
| Iterative Loop | O(n) | O(1) | Understanding the series |
Conclusion
The series sum n(n+1)(n+2)(n+3)/4 provides an efficient O(1) solution. The iterative method helps understand the series pattern but is slower for large n values.
