Sum of the first N terms of the series 2,10, 30, 68,….
Given a number N, the task is to find the sum of first N terms of the below series:
Sn = 2 + 10 + 30 + 68 + … upto n terms
Examples:
Input: N = 2 Output: 12 2 + 10 = 12 Input: N = 4 Output: 40 2 + 10 + 30 + 68 = 110
Approach: Let, the nth term be denoted by tn.
This problem can easily be solved by splitting each term as follows :
Sn = 2 + 10 + 30 + 68 + ...... Sn = (1+1^3) + (2+2^3) + (3+3^3) + (4+4^3) +...... Sn = (1 + 2 + 3 + 4 + ...unto n terms) + (1^3 + 2^3 + 3^3 + 4^3 + ...upto n terms)
We observed that Sn can broken down into summation of two series.
Hence, the sum of first n terms is given as follows:
Sn = (1 + 2 + 3 + 4 + ...unto n terms) + (1^3 + 2^3 + 3^3 + 4^3 + ...upto n terms) Sn = n*(n + 1)/2 + (n*(n + 1)/2)^2
Below is the implementation of above approach:
C++
// C++ program to find sum of first n terms #include <bits/stdc++.h> using namespace std; // Function to calculate the sum int calculateSum(int n) { return n * (n + 1) / 2 + pow((n * (n + 1) / 2), 2); } // Driver code int main() { // number of terms to be // included in the sum int n = 3; // find the Sum cout << "Sum = " << calculateSum(n); return 0; } |
Java
// Java program to find sum of first n terms public class GFG { // Function to calculate the sum static int calculateSum(int n) { return n * (n + 1) / 2 + (int)Math.pow((n * (n + 1) / 2), 2); } // Driver code public static void main(String args[]) { // number of terms to be // included in the sum int n = 3; // find the Sum System.out.println("Sum = "+ calculateSum(n)); } // This Code is contributed by ANKITRAI1 } |
Python3
# Python program to find sum # of first n terms # Function to calculate the sum def calculateSum(n): return (n * (n + 1) // 2 + pow((n * (n + 1) // 2), 2)) # Driver code # number of terms to be # included in the sum n = 3 # find the Sum print("Sum = ", calculateSum(n)) # This code is contributed by # Sanjit_Prasad |
C#
// C# program to find sum of first n terms using System; class gfg { // Function to calculate the sum public void calculateSum(int n) { double r = (n * (n + 1) / 2 + Math.Pow((n * (n + 1) / 2), 2)); Console.WriteLine("Sum = " + r); } // Driver code public static int Main() { gfg g = new gfg(); // number of terms to be // included in the sum int n = 3; // find the Sum g.calculateSum(n); Console.Read(); return 0; } } |
PHP
<?php // PHP program to find sum // of first n terms // Function to calculate the sum function calculateSum($n) { return $n * ($n + 1) / 2 + pow(($n * ($n + 1) / 2), 2); } // Driver code // number of terms to be // included in the sum $n = 3; // find the Sum echo "Sum = " , calculateSum($n); // This code is contributed // by anuj_67 ?> |
Sum = 42
Recommended Posts:
- Find n terms of Fibonacci type series with given first two terms
- Sum of the first N terms of the series 5,12, 23, 38....
- Sum of first n terms of a given series 3, 6, 11, .....
- Sum of the first N terms of the series 2, 6, 12, 20, 30....
- Sum of the series 5+55+555+.. up to n terms
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Find the sum of n terms of the series 1,8,27,64 ....
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Find the sum of all the terms in the n-th row of the given series
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Find the sum of series 0.X + 0.XX + 0.XXX +... upto k 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.


