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)

Example

The following are implementations of the above approach in various programming languages −

#include<stdio.h>
#include<stdlib.h>
// Define a recursive function to calculate the sum of the series.
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
   printf("The sum of the series is: %lld", SeriesSum(num));
   return 0;
}

Output

The sum of the series is: 420
#include<bits/stdc++.h>
using namespace std;
// Define a recursive function to calculate the sum of the series.
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 sorted string as per ASCII values of the characters is: $%()7jkw
public class Main {
   // Define a recursive function to calculate the sum of the series.
   public static long seriesSum(int n) {
      // Base case
      if (n == 0) return 0;
      // Calculate the value for the current term
      long term = n * (n + 1) * (n + 2);

      // Make a recursive call to calculate the sum of the remaining terms
      return term + seriesSum(n - 1);
   }
   public static void main(String[] args) {
      int num = 5; // Define the number of terms in the series
      long result = seriesSum(num); // Call the function to get the output
      System.out.println("The sum of the series is: " + result);
   }
}

Output

The sum of the series is: 420
#recursive function to calculate the sum of the series.
def SeriesSum(n):
   # Base case
   if n==0:
      return 0
   #store each value in a variable m
   m= (n*(n+1)*(n+2))
   #making a recursive call
   return m + SeriesSum(n-1)
num=5
print("The sum of the series is:", SeriesSum(num));

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

Iteration is a technique that involves repeating a certain number of steps continuously until a given condition is successfully met.

Example

The following are implementations of the above approach in various programming languages −

#include<stdio.h>
int main(){
   int num=5;
   // Declare a variable ‘num’
   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
   printf("The sum of series is: %ld", ans);
   return 0;
}

Output

The sum of series is: 420
#include<bits/stdc++.h>
using namespace std;
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
public class Main {
   public static void main(String[] args) {
      int n = 5; // Define the number of terms in the series
      long sum = 0; // Initialize the sum variable
      for (int i = 1; i <= n; i++) {
         // Calculate the i-th term of the series
         long term = i * (i + 1) * (i + 2);
         sum += term; // Add the term to the sum
      }
      System.out.println("The sum of the series is: " + sum);
   }
}

Output

The sum of series is: 420
num=5;
ans=0;
# Iteration process for getting required answer using the last term
for i in range (1, num+1):
   ans += (i*(i+1)*(i+2))
    
# Print the output
print("The sum of series is:",ans)

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

The following are implementations of the above formula in various programming languages −

#include <stdio.h>
// 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(){
   int n=5;
   printf("The number n is given as %d\n", n);
   // Call the function to get the output
   printf("The sum of the series is: %d\n", SeriesSum(n));
   return 0;
}

Output

The number n is given as 5
The sum of the series is: 420
#include <bits/stdc++.h>
using namespace std;
// 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(){
   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
public class Main {
   public static 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
   public static void main(String[] args) {
      int n=5;
      System.out.println("The number n is given as "+ n);
      // Call the function to get the output
      System.out.println("The sum of the series is: "+ SeriesSum(n));
   }
}

Output

The number n is given as 5
The sum of the series is: 420
def SeriesSum(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 int(((n * (n + 1)) / 2) * (((n + 2) * (n + 3)) / 2))

def main():
   n = 5
   print("The number n is given as", n)
   # Call the function to get the output
   print("The sum of the series is:", SeriesSum(n))

if __name__ == "__main__":
   main()

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: 05-Feb-2024

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements