In this problem, we are given a number n that defines the nth term of the series. Our task is to create a program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2) in C++.
Problem description − Here, we will find the sum till n term of the given series with is 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2). This can be decoded as the summation of n*(n+1)*(n+2).
Let’s take an example to understand the problem
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
Solution Approach
A simple way to solve the problem is using a loop from 1 to n and find the product at each iteration and add it to the sumVar. After the loop ends, return the sumVar.
Algorithm
- Step 1 − loop i = 1 to n.
- Step 1.1 − update sumVar, sumVar += i*(i+1)*(i+2)
- Step 2 − print sumVar.
Program to illustrate the working of our solution
Example
#include <iostream>
using namespace std;
int calcSeriesSum(int n){
int sumVar = 0;
for(int i = 1; i <= n; i++)
sumVar = sumVar + ( (i)*(i+1)*(i+2) );
return sumVar;
}
int main(){
int n = 7;
cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n);
return 0;
}Output
The sum of series till 7 is 1260
This approach is not efficient as it takes time complexity of the order of N.
Another approach is using the mathematical formula for the sum of series. As we have discussed in the problem description, the series can be said the summation of (n)*(n+1)*(n+2).
Using this information let’s create a general formula for the sum.
$Sum =\sum_{\square=1}^\square\blacksquare((\square)\ast(\square+1)\ast(\square+2))$
$=\sum\lbrace{(n^2+n)(n+2)}\rbrace$
$=\sum\lbrace{n^3 + n^2 + 2n^2 + 2n}\rbrace$
$=\sum\lbrace{n^3 +3n^2 + 2n}\rbrace$
$=\sum_{\square=1}^\square\blacksquare\square^3+3\sum_{\square=1}^\square\blacksquare\square^2+2\sum_{\square=1}^\square\blacksquare\square^\blacksquare$
Now, using the general formula for the sums,
$\sum_{\square=1}^\square\blacksquare\square^3=\frac{(\square\ast(\square+1))^2}{2}$
$\sum_{\square=1}^\square\blacksquare\square^2=\frac{(\square\ast(\square+1)\ast(2\square+1))^\blacksquare}{6}$
$\sum_{\square=1}^\square\blacksquare\square^\blacksquare=\frac{(\square\ast(\square+1)^\blacksquare}{2}$
Adding all these to sum formula makes,
$Sum=\frac{(\square\ast(\square+1))^2}{2^2}+\frac{3(\square\ast(\square+1)\ast(2\square+1))^\blacksquare}{6}+\frac{2(\square\ast(\square+1))^\blacksquare}{2}$
$=\frac{(\square\ast(\square+1))^\blacksquare}{2}[( (n * (n+1))/2 ) + (3(2n+1)/3) + 2]$
$=\frac{(\square\ast(\square+1))^\blacksquare}{4}[n^2+ n + 4n + 2 + 4]$
$=\frac{(\square\ast(\square+1))^\blacksquare}{4}[n^2+ 5n + 6]$
$=\frac{(\square\ast(\square+1))^\blacksquare}{4}[(n+2)(n+3)]$
=¼[ (n)*(n+1)*(n+2)*(n+3) ]
The of the series till nth term is calculated using the formula,
¼[ (n)*(n+1)*(n+2)*(n+3) ]
Program to illustrate the working of our solution
Example
#include <iostream>
using namespace std;
int calcSeriesSum(int n){
int sumVar = 0;
sumVar = ( (n)*(n + 1)*(n + 2)*(n + 3)/4 );
return sumVar;
}
int main(){
int n = 7;
cout<<"The sum of series till "<<n<<" is "<<calcSeriesSum(n);
return 0;
}Output
The sum of series till 7 is 1260