Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
Given a positive integer n and the task is to find the sum of series 1*2*3 + 2*3*4 + 4*5*6 + . . .+ n*(n+1)*(n+2).
Examples:
Input : n = 10
Output : 4290
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
= 6 + 24 + 60 + 120 + 210 + 336 + 504 +
720 + 990 + 1320
= 4290
Input : n = 7
Output : 1260
Method 1: In this case loop will run n times and calculate the sum.
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfSeries( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
sum = sum + i * (i + 1) * (i + 2);
return sum;
}
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
|
Java
public class GfG{
static int sumOfSeries( int n)
{
int sum = 0 ;
for ( int i = 1 ; i <= n; i++)
sum = sum + i * (i + 1 ) * (i + 2 );
return sum;
}
public static void main(String s[])
{
int n = 10 ;
System.out.println(sumOfSeries(n));
}
}
|
Python3
def sumOfSeries(n):
sum = 0 ;
i = 1 ;
while i< = n:
sum = sum + i * (i + 1 ) * (
i + 2 )
i = i + 1
return sum
n = 10
print (sumOfSeries(n))
|
C#
using System;
public class GfG
{
static int sumOfSeries( int n)
{
int sum = 0;
for ( int i = 1; i <= n; i++)
sum = sum + i * (i + 1) * (i + 2);
return sum;
}
public static void Main()
{
int n = 10;
Console.WriteLine(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries( $n )
{
$sum = 0;
for ( $i = 1; $i <= $n ; $i ++)
$sum = $sum + $i * ( $i + 1) *
( $i + 2);
return $sum ;
}
$n = 10;
echo sumOfSeries( $n );
?>
|
Javascript
<script>
function sumOfSeries( n) {
let sum = 0;
for ( let i = 1; i <= n; i++)
sum = sum + i * (i + 1) * (i + 2);
return sum;
}
let n = 10;
document.write(sumOfSeries(n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 2: In this case we use formula to add sum of series.
Given series 1*2*3 + 2*3*4 + 3*4*5 + 4*5*6 + . . . + n*(n+1)*(n+2)
sum of series = (n * (n+1) * (n+2) * (n+3)) / 4
Put n = 10 then
sum = (10 * (10+1) * (10+2) * (10+3)) / 4
= (10 * 11 * 12 * 13) / 4
= 4290
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfSeries( int n)
{
return (n * (n + 1) * (n + 2) * (n + 3)) / 4;
}
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sumOfSeries( int n)
{
return (n * (n + 1 ) *
(n + 2 ) * (n + 3 )) / 4 ;
}
public static void main (String[] args) {
int n = 10 ;
System.out.println(sumOfSeries(n));
}
}
|
Python3
def sumOfSeries(n):
return (n * (n + 1 ) * (n + 2
) * (n + 3 )) / 4
n = 10
print (sumOfSeries(n))
|
C#
using System;
class GFG {
static int sumOfSeries( int n)
{
return (n * (n + 1) *
(n + 2) * (n + 3)) / 4;
}
public static void Main () {
int n = 10;
Console.WriteLine(sumOfSeries(n));
}
}
|
PHP
<?php
function sumOfSeries( $n )
{
return ( $n * ( $n + 1) * ( $n + 2) *
( $n + 3)) / 4;
}
$n = 10;
echo sumOfSeries( $n );
?>
|
Javascript
<script>
function sumOfSeries(n) {
return (n * (n + 1) * (n + 2) * (n + 3)) / 4;
}
var n = 10;
document.write(sumOfSeries(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
How does this formula work?
We can prove working of this formula using
mathematical induction.
According to formula, sum of (k -1) terms is
((k - 1) * (k) * (k + 1) * (k + 2)) / 4
Sum of k terms
= sum of k-1 terms + value of k-th term
= ((k - 1) * (k) * (k + 1) * (k + 2)) / 4 +
k * (k + 1) * (k + 2)
Taking common term (k + 1) * (k + 2) out.
= (k + 1)*(k + 2) [k*(k-1)/4 + k]
= (k + 1)*(k + 2) * k * (k + 3)/4
= k * (k + 1) * (k + 2) * (k + 3)/4
Avoiding the overflow:
In the above method, sometimes due to the large value of n, the value of (n * (n + 1) * (n + 2) * (n + 3)) would overflow. We can avoid this overflow to some extent using the fact that n*(n+1) must be divisible by 2 and (n+2)*(n+3) is also divisible by 2.
Below is the code for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int sumOfSeries( int n)
{
return ((n * (n + 1) /2)* ((n + 2) * (n + 3)/2));
}
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int sumOfSeries( int n)
{
return ((n * (n + 1 ) / 2 )* ((n + 2 ) * (n + 3 )/ 2 ));
}
public static void main (String[] args) {
int n = 10 ;
System.out.println(sumOfSeries(n));
}
}
|
Python3
def sumOfSeries(n):
return ((n * (n + 1 ) / / 2 ) * ((n + 2 ) * (n + 3 ) / / 2 ))
if __name__ = = '__main__' :
n = 10
print (sumOfSeries(n))
|
C#
using System;
class Program {
static int SumOfSeries( int n) {
return ((n * (n + 1) / 2) * ((n + 2) * (n + 3) / 2));
}
static void Main( string [] args) {
int n = 10;
Console.WriteLine(SumOfSeries(n));
}
}
|
Javascript
function sumOfSeries(n) {
return ((n * (n + 1) / 2) * ((n + 2) * (n + 3) / 2));
}
let n = 10;
console.log(sumOfSeries(n));
|
Time complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
29 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...