Sum of the first N terms of the series 2, 6, 12, 20, 30.... - GeeksforGeeks

25 Feb, 2021
Given a number N, the task is to find the sum of first N terms of the below series: 
Sn = 2 + 6 + 12 + 20 + 30 ... upto n terms
Examples:  
Input: N = 2
Output: 8
2 + 6
= 8

Input: N = 4 
Output: 40
2 + 6+ 12 + 20
= 40
 
Approach: Let, the nth term be denoted by tn. This problem can easily be solved by splitting each term as follows :  
Sn = 2 + 6 + 12 + 20 + 30......
Sn = (1+1^2) + (2+2^2) + (3+3^2) + (4+4^2) +......
Sn = (1 + 2 + 3 + 4 + ...unto n terms) + (1^2 + 2^2 + 3^2 + 4^2 + ...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^2 + 2^2 + 3^2 + 4^2 + ...upto n terms)
Sn = n*(n + 1)/2 + n*(n + 1)*(2*n + 1)/6
Below is the implementation of above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find sum of first n terms#include <bits/stdc++.h>using namespace std; // Function to calculate the sumint calculateSum(int n){     return n * (n + 1) / 2 + n *            (n + 1) * (2 * n + 1) / 6;} // Driver codeint main(){    // number of terms to be    // included in the sum    int n = 3;     // find the Sn    cout << "Sum = " << calculateSum(n);     return 0;}
// Java program to find sum of first n terms import java.util.*;import java.lang.*;import java.io.*;class GFG{     // Function to calculate the sumstatic int calculateSum(int n){       return n * (n + 1) / 2 + n *             (n + 1) * (2 * n + 1) / 6;}   // Driver codepublic static void main(String args[]){    // number of terms to be    // included in the sum    int n = 3;       // find the Sn    System.out.print("Sum = " + calculateSum(n));   }}
# Python program to find sum of# first n terms # Function to calculate the sumdef calculateSum(n):    return (n * (n + 1) // 2 + n *           (n + 1) * (2 * n + 1) // 6) # Driver code # number of terms to be# included in the sumn = 3 # find the Sumprint("Sum = ", calculateSum(n)) # This code is contributed by# Sanjit_Prasad
// C# program to find sum of// first n termsusing System; class GFG{     // Function to calculate the sumstatic int calculateSum(int n){     return n * (n + 1) / 2 + n *               (n + 1) * (2 * n + 1) / 6;} // Driver codepublic static void Main(){    // number of terms to be    // included in the sum    int n = 3;     // find the Sn    Console.WriteLine("Sum = " +                       calculateSum(n));}} // This code is contributed by inder_verma
<?php// PHP program to find sum// of first n terms // Function to calculate the sumfunction calculateSum($n){    return $n * ($n + 1) / 2 + $n *                ($n + 1) * (2 * $n + 1) / 6;} // Driver code // number of terms to be// included in the sum$n = 3; // find the Snecho "Sum = " , calculateSum($n); // This code is contributed// by inder_verma?>
<script> // Javascript program to find sum of first n terms // Function to calculate the sumfunction calculateSum(n){     return n * (n + 1) / 2 + n *            (n + 1) * (2 * n + 1) / 6;} // Driver code     // number of terms to be    // included in the sum    let n = 3;     // find the Sn    document.write("Sum = " + calculateSum(n)); // This code is contributed by Mayank Tyagi </script>
Sum = 20
 
Sanjit_Prasad
tufan_gupta2000
inderDuMCA
shubham_singh
mayanktyagi1709
series
series-sum
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Program to multiply two matrices
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java