Sum of the series 1.2.3 + 2.3.4 + … + n(n+1)(n+2)
Find the sum up to n terms of the series: 1.2.3 + 2.3.4 + … + n(n+1)(n+2). In this 1.2.3 represent the first term and 2.3.4 represent the second term .
Examples :
Input : 2 Output : 30 1.2.3 + 2.3.4 = 6 + 24 = 30 Input : 3 Output : 90
Simple Approach We run a loop for i = 1 to n, and find the sum of (i)*(i+1)*(i+2).
And at the end display the sum .
C++
// CPP program to find sum of the series // 1.2.3 + 2.3.4 + 3.4.5 + ... #include <bits/stdc++.h> using namespace std; int sumofseries(int n) { int res = 0; for (int i = 1; i <= n; i++) res += (i) * (i + 1) * (i + 2); return res; } // Driver Code int main() { cout << sumofseries(3) << endl; return 0; } |
Java
// Java program to find sum of the series // 1.2.3 + 2.3.4 + 3.4.5 + ... import java.io.*; import java.math.*; class GFG { static int sumofseries(int n) { int res = 0; for (int i = 1; i <= n; i++) res += (i) * (i + 1) * (i + 2); return res; } // Driver Code public static void main(String[] args) { System.out.println(sumofseries(3)); } } |
Python3
# Python 3 program to find sum of the series # 1.2.3 + 2.3.4 + 3.4.5 + ... def sumofseries(n): res = 0 for i in range(1, n+1): res += (i) * (i + 1) * (i + 2) return res # Driver Program print(sumofseries(3)) # This code is contributed # by Smitha Dinesh Semwal |
C#
// Java program to find sum of the series // 1.2.3 + 2.3.4 + 3.4.5 + ... using System; class GFG { static int sumofseries(int n) { int res = 0; for (int i = 1; i <= n; i++) res += (i) * (i + 1) * (i + 2); return res; } // Driver Code public static void Main() { Console.WriteLine(sumofseries(3)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find // sum of the series // 1.2.3 + 2.3.4 + 3.4.5 + ... function sumofseries($n) { $res = 0; for ($i = 1; $i <= $n; $i++) $res += ($i) * ($i + 1) * ($i + 2); return $res; } // Driver Code echo sumofseries(3); //This code is contributed by anuj_67. ?> |
90
Complexity : O(N)
Efficient Approach
Using Efficient Approach we know that we have to find = summation of( (n)*(n+1)*(n+2) )
Sn = summation[ (n)*(n+1)*(n+2) ]
Sn = summation [n3 + 2*n2 + n2 + 2*n]We know sum of cubes of natural numbers is (n*(n+1))/2)2, sum of squares of natural numbers is n * (n + 1) * (2n + 1) / 6 and sum of first n natural numbers is n(n+1)/2
Sn = ((n*(n+1))/2)2 + 3((n)*(n+1)*(2*n+1)/6) + 2*((n)*(n+1)/2)
So by evaluating the above we get,
Sn = (n*(n+1)*(n+2)*(n+3)/4)
Hence it has a O(1) complexity.
C++
// Efficient CPP program to // find sum of the series // 1.2.3 + 2.3.4 + 3.4.5 + ... #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 Code int main() { cout << sumofseries(3) << endl; return 0; } |
Java
// Efficient Java program to // find sum of the series // 1.2.3 + 2.3.4 + 3.4.5 + .. import java.io.*; import java.math.*; class GFG { static int sumofseries(int n) { return (n * (n + 1) * (n + 2) * (n + 3) / 4); } // Driver Code public static void main(String[] args) { System.out.println(sumofseries(3)); } } |
Python3
# Efficient CPP program to find sum of the # series 1.2.3 + 2.3.4 + 3.4.5 + ... # function to calculate sum of series def sumofseries(n): return int(n * (n + 1) * (n + 2) * (n + 3) / 4) # Driver program print(sumofseries(3)) # This code is contributed # by Smitha Dinesh Semwal |
C#
// Efficient C# program to // find sum of the series // 1.2.3 + 2.3.4 + 3.4.5 + .. using System; class GFG { static int sumofseries(int n) { return (n * (n + 1) * (n + 2) * (n + 3) / 4); } // Driver Code public static void Main() { Console.WriteLine(sumofseries(3)); } } // This code is contributed by anuj_67. |
PHP
<?php // Efficient CPP program // to find sum of the // series 1.2.3 + 2.3.4 // + 3.4.5 + ... // function to calculate // sum of series function sumofseries($n) { return ($n * ($n + 1) * ($n + 2) * ($n + 3) / 4); } // Driver Code echo sumofseries(3); // This code is contributed by anuj_67. ?> |
90
Time Complexity : O(1)
Recommended Posts:
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2
- Sum of Series (n^2-1^2) + 2(n^2-2^2) +....n(n^2-n^2)
- Value of the series (1^3 + 2^3 + 3^3 + ... + n^3) mod 4 for a given n
- Sum of series (n/1) + (n/2) + (n/3) + (n/4) +.......+ (n/n)
- Sum of the series 1 + (1+2) + (1+2+3) + (1+2+3+4) + ...... + (1+2+3+4+...+n)
- Sum of the Series 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n
- Sum of the series 2 + (2+4) + (2+4+6) + (2+4+6+8) + …… + (2+4+6+8+….+2n)
- Sum of the series 3, 20, 63, 144, ......
- Sum of series 1*1*2! + 2*2*3! + ……..+ n*n*(n+1)!
- Sum of series 1*1! + 2*2! + ……..+ n*n!
- Sum of the series 2^0 + 2^1 + 2^2 +.....+ 2^n
- Sum of the series Kn + ( K(n-1) * (K-1)1 ) + ( K(n-2) * (K-1)2 ) + ....... (K-1)n
- Sum of the series 1 + (1+3) + (1+3+5) + (1+3+5+7) + …… + (1+3+5+7+…+(2n-1))
- Sum of the Series 1/(1*2) + 1/(2*3) + 1/(3*4) + 1/(4*5) + . . . . .
- Sum of the series 5+55+555+.. up to n terms
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



