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++
// Program to find the sum of series // 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1) #include <bits/stdc++.h> using namespace std; // Function to calculate sum of series. int sumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + i * (i + 1) * (i + 2); return sum; } // Driver function int main() { int n = 10; cout << sumOfSeries(n); return 0; } |
chevron_right
filter_none
Java
// Java Program to find the sum of series // 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1) public class GfG{ // Function to calculate sum of series. static int sumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + i * (i + 1) * (i + 2); return sum; } // Driver Code public static void main(String s[]) { int n = 10; System.out.println(sumOfSeries(n)); } } // This article is contributed by Gitanjali. |
chevron_right
filter_none
Python3
# Python program to find the # sum of series # 1*2*3 + 2*3*4 + . . . # + n*(n+1)*(n+1) # Function to calculate sum # of series. def sumOfSeries(n): sum = 0; i = 1; while i<=n: sum = sum + i * (i + 1) * ( i + 2) i = i + 1 return sum # Driver code n = 10print(sumOfSeries(n)) # This code is contributed by "Abhishek Sharma 44" |
chevron_right
filter_none
C#
// C# Program to find the sum of series // 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1) using System; public class GfG { // Function to calculate sum of series. static int sumOfSeries(int n) { int sum = 0; for (int i = 1; i <= n; i++) sum = sum + i * (i + 1) * (i + 2); return sum; } // Driver Code public static void Main() { int n = 10; Console.WriteLine(sumOfSeries(n)); } } // This article is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP Program to find the sum of series // 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1) // Function to calculate sum of series. function sumOfSeries($n) { $sum = 0; for ($i = 1; $i <= $n; $i++) $sum = $sum + $i * ($i + 1) * ($i + 2); return $sum; } // Driver Code $n = 10; echo sumOfSeries($n); // This code is contributed by vt_m. ?> |
chevron_right
filter_none
Output:
4290
Time Complexity: O(n)
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++
// Program to find the sum of series // 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1) #include <bits/stdc++.h> using namespace std; // Function to calculate sum of series. int sumOfSeries(int n) { return (n * (n + 1) * (n + 2) * (n + 3)) / 4; } // Driver function int main() { int n = 10; cout << sumOfSeries(n); return 0; } |
chevron_right
filter_none
Java
// Program to find the // sum of series // 1*2*3 + 2*3*4 + // . . . + n*(n+1)*(n+1) import java.io.*; class GFG { // Function to calculate // sum of series. static int sumOfSeries(int n) { return (n * (n + 1) * (n + 2) * (n + 3)) / 4; } // Driver function public static void main (String[] args) { int n = 10; System.out.println(sumOfSeries(n)); } } // This code is contributed by Nikita Tiwari. |
chevron_right
filter_none
Python3
# Python program to find the # sum of series # 1*2*3 + 2*3*4 + . . . # + n*(n+1)*(n+1) # Function to calculate sum # of series. def sumOfSeries(n): return (n * (n + 1) * (n + 2 ) * (n + 3)) / 4 #Driver code n = 10print(sumOfSeries(n)) # This code is contributed by "Abhishek Sharma 44" |
chevron_right
filter_none
C#
// Program to find the // sum of series // 1*2*3 + 2*3*4 + // . . . + n*(n+1)*(n+1) using System; class GFG { // Function to calculate // sum of series. static int sumOfSeries(int n) { return (n * (n + 1) * (n + 2) * (n + 3)) / 4; } // Driver function public static void Main () { int n = 10; Console.WriteLine(sumOfSeries(n)); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP Program to find the sum of series // 1*2*3 + 2*3*4 + . . . + n*(n+1)*(n+1) // Function to calculate sum of series. function sumOfSeries($n) { return ($n * ($n + 1) * ($n + 2) * ($n + 3)) / 4; } // Driver Code $n = 10; echo sumOfSeries($n); // This code is contributed by vt_m. ?> |
chevron_right
filter_none
Output:
4290
Time Complexity: 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
Recommended Posts:
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Program to find Sum of the series 1*3 + 3*5 + ....
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find Nth term in the given Series
- Program to find the N-th term of series 3, 5, 33, 35, 53…. | Set-2
- Program to find the Nth term of the series 0, 5, 18, 39, 67, 105, 150, 203, ...
- Program to find the Nth term of series 5, 12, 21, 32, 45......
- Program to find the Nth number of the series 2, 10, 24, 44, 70.....
- Program to find N-th term of series 1, 2, 11, 12, 21….
- Program to find Nth term of series 1, 6, 17, 34, 56, 86, 121, 162, .......
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m



