Program to find the sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)


The sum of the series is the value of all terms in the given series added together in a particular pattern. Here given pattern is in the form of the:

∑ (n*(n+1)*(n+2)) as (n*(n+1)*(n+2)) is the last term in the given pattern. The following article discusses 3 approaches in detail to find the given sum of the series with different time and space complexities.

Problem Statement

Now, let us see how to calculate the sum of series 1*2*3 + 2*3*4 + 3*4*5 + . . . + n*(n+1)*(n+2).

Sample Examples

Let’s try to understand this problem with the help of an example.

Input

n = 12

Output

8190

Explanation

1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + 5*6*7 + 6*7*8 + 7*8*9 + 8*9*10 + 9*10*11 + 10*11*12 + 11*12*13 + 12*13*14
= 6 + 24 + 60 + 120 + 210 + 336 + 504 + 720 + 990 + 1320 + 1716 + 2184
= 4290

Input

n = 7

Output

1260

Explanation

1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + 5*6*7
= 6 + 24 + 60 + 120 + 210 + 336 + 504
= 1260

Input

n = 21

Output

63756

Explanation

1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + 5*6*7 + . . . + 21*22*23
= 6 + 24 + 60 + 120 + 210 + 336 + 504 + . . . + 10626
= 63756

Problem Explanation

Let’s try to understand the problem and find its solution. We have two options to solve the above problem. One solution can be done by using recursion and the second solution can be achieved by using the iteration method (with the help of a loop).

We will see both solutions one by one.

Solution 1 Brute force solution using the last term

Recursion code by using the last term of expression i.e. n*(n+1)*(n+2)

#include<bits/stdc++.h>
using namespace std;
// Define a recursive function to calculate the sum of the series.  Here we have used long long int to avoid the situation of overflow for large values of the output data
long long int SeriesSum(int n){
   // Base case
   if(n==0)return 0;
   // store each value in a variable m
   long long int m= (n*(n+1)*(n+2));
   // making a recursive call
   return m+ SeriesSum(n-1);
}
// main function
int main(){
   int num=5;
   // Declare a variable ‘num’ Call the function to get the output
   cout<< "The sum of the series is: " << SeriesSum(num);
   return 0;
}

Output

The sum of the series is: 420

Complexities for recursive code

  • Time complexity − O(1); this code performs a fixed number of calculations, regardless of the size of the input.

  • Space complexity − O(1); the code uses a fixed number of variables to store input values and results, regardless of the size of the input.

Iterative approach

#include<bits/stdc++.h>
using namespace std;
// main function
int main(){
   int num=5;
   // Declare a variable ‘num’
   long long int ans=0;
   // Iteration process for getting required answer using the last term
   for(int i=1;i<=num;i++){
      ans += (i*(i+1)*(i+2)); 
   }
   // Print the output
   cout<< "The sum of series is: " << ans;
   return 0;
}

Output

The sum of series is: 420

Complexities for iterative code

  • Time complexity − O(n); this code performs a fixed number of calculations (iterations), which would depend on the input as the loop will run n times where n is the input.

  • Space complexity − O(1); the code uses a fixed number of variables to store input values and results, regardless of the size of the input.

Solution 2

Approach by using a formula that can be derived by using properties of summation :

We can derive the formula by using properties of summation and by using summations of already known terms.

To find the formula − (n*(n+1)*(n+2)*(n+3))/4

1*2*3 + 2*3*4 + 3*4*5 + . . . + (n*(n+1)*(n+2))/4
=> Σ (n*(n+1)*(n+2))/4
=> Σ n*(n^2 + 3n +2) 
=> Σ n^3 + Σ 3n^2 + Σ 2n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Equation 1

As we already know,

Σ n^3 = ((n*(n+1))/2) ^2;
Σ n^2 = (n*(n+1)*(2n+1))/6;
Σ n = (n*(n+1))/2;

Putting all these values in Equation 1,

=> (n*(n+1)^2/4 + (3 * (n*(n+1)*(2n+1))/6) + (2 * (n*(n+1))/2)
=> (n*(n+1))^2/4 + ((n*(n+1)*(2n+1))/2) + (2 * (n*(n+1))/2)
=> (n*(n+1))/2 * {(n*(n+1)/2) + (2n+1) + (2)}
=> (n*(n+1))/2 * {(n^2+n) + (4n+2) + (4))/2}
=> (n*(n+1))/4 * {(n^2 +5n+6)}
=> (n*(n+1))/4 * {(n+2)*(n+3)}
=> (n*(n+1)*(n+2)*(n+3))/4

Hence this formula can be derived to solve the problem directly.

Example Code using the direct formula

#include <bits/stdc++.h>
using namespace std;
// Function to calculate the sum of series.
// We just need to derive the formula, we can directly put the value of n in the formula to get our answer.
int SeriesSum(int n){
   // Here to avoid the situation of an overflow of data we are doing step-wise calculations by dividing (n*(n+1)) and ((n+2)*(n+3)) separately by 2 as  (n*(n+1)) must be divisible by 2 and ((n+2)*(n+3)) is also divisible by 2
   return ((n * (n + 1))/2) * (((n + 2) * (n + 3))/ 2);
}
// main function
int main(){
   // Declare a variable 'n'
   int n=5;
   cout<< "The number n is given as "<< n << endl;
   // Call the function to get the output
   cout<< "The sum of the series is: " << SeriesSum(n);
   return 0;
} 

Output

The number n is given as 5
The sum of the series is: 420

Complexities for the above codes

  • Time complexity − O(1); this code is using just the input n to perform the calculations.

  • Space complexity − O(1); the code uses a fixed number of variables to store input values and results, regardless of the size of the input.

Conclusion

In this article, we learned 3 different approaches to solving the same problem of the series of sums. Also, we learned how to use summation properties to derive a formula directly for a series of sums to write the code and hence save time and space. We also saw how we could avoid overflow situations of the input value problems.

Updated on: 10-Aug-2023

156 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements