
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n) in C++
In this article, we are given a number n. Our task is to write a program to calculate the sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ? + (1+2+3+4+...+n). This series can be represented mathematically as: $$ \displaystyle\sum\limits_{k=1}^n \displaystyle\sum\limits_{j=1}^k j $$
The above series is also known as tetrahedral number or triangular pyramidal number. A tetrahedral number is the number of points required to form a pyramid with a triangular base. Below is an example of the tetrahedral number series up to n.
Scenario
Consider the following example scenario to calculate the 4th tetrahedral number:
Input: n = 4 Output: 20 Explanation Given, n = 4 Series: 1 + (1+2) + (1+2+3) + (1+2+3+4) = 1 + 3 + 6 + 10 = 20
The approaches for calculating the tetrahedral number are given below:
Using Nested for Loop
You can use a nested for loop where the outer loop selects an index and the inner loop finds the sum of the number from 1 to the value of i. After the completion of the outer for loop, the sum is returned containing the tetrahedral number for the given n.
Example
Here is the code implementation to calculate the nth tetrahedral number using nested for loop:
#include <iostream> using namespace std; int tetrahedral(int n) { int sum = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= i; j++) sum += j; return sum; } int main() { int n = 7; cout << "Sum of the series 1 + (1+2) + (1+2+3) + ... + (1+2+3+4+...+" << n << ") is: " << tetrahedral(n); return 0; }
The output of the above code is as follows:
Sum of the series 1 + (1+2) + (1+2+3) + ... + (1+2+3+4+...+7) is: 84
A detailed explanation of the above program is given below:
Let n = 4, sum = 0 for i = 1, j = 1, sum = 0+1 = 1 for i =2, j = 1, 2, sum = (1 + (1+2)) = 4 for i = 3, j = 1, 2, 3, sum = (4 + (1+2+3)) = 10 for i = 4, j = 1, 2, 3, 4, sum = (10 + (1+2+3+4)) = 20 sum = 20
Using Formula
In this approach, we will use the formula to calculate the nth tetrahedral number. Here is the derivation of the formula:
sum = 1 + (1+2) + (1+2+3) + (1+2+3+4) ... (1+2+3+4+....+n) = an an = ?(k=1 to n) (k(k+1))/2 sum = (? ( k^2 + k)) / 2 = (? (k^2) + ? k) / 2 sum = [ (n(n+1)(2n+1))/6 + (n(n+1)/2 )] / 2 sum = [(n(n+1)(2n+1)) + (3n(n+1))] / 12 sum = [n(n+1){2n+1 + 3}] / 12 sum = [n(n+1)(n+2)] / 6
Example
The following example uses the above derived formula to calculate the nth tetrahedral number of the series:
#include <iostream> using namespace std; int tetrahedral(int n) { int sum = (n * (n + 1) * (2 * n + 4)) / 12; return sum; } int main() { int n = 7; cout << "Sum of the series 1 + (1+2) + (1+2+3) + ... + (1+2+3+4+...+" << n << ") is: " << tetrahedral(n); }
The output of the above code is as follows:
Sum of the series 1 + (1+2) + (1+2+3) + ... + (1+2+3+4+...+7) is: 84
Complexity Comparison
Here is a comparison of the time and space complexity of all the above approaches.
Approach | Time Complexity | Space Complexity |
---|---|---|
Using Nested for Loop | O(n^2) | O(1) |
Using Formula | O(1) | O(1) |