Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms

22 Jun, 2022
Given a value n, the task is to find sum of the series (1*2) + (2*3) + (3*4) + ......+ n termsExamples:  
Input: n = 2
Output: 8
Explanation:
(1*2) + (2*3)
= 2 + 6
= 8

Input: n = 3
Output: 20
Explanation:
(1*2) + (2*3) + (2*4)
= 2 + 6 + 12
= 20
 
Simple Solution One by one add elements recursively.Below is the implementation  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return sumint sum(int n){    if (n == 1) {        return 2;    }    else {        return (n * (n + 1) + sum(n - 1));    }} // Driver codeint main(){     int n = 2;    cout << sum(n);}
// Java implementation of the approach class Solution {     // Function to return a the required result    static int sum(int n)    {        if (n == 1) {            return 2;        }        else {            return (n * (n + 1) + sum(n - 1));        }    }    // Driver code    public static void main(String args[])    {        int n = 2;        System.out.println(sum(n));    }}
# Python3 implementation of the approach # Function to return sumdef sum(n):     if (n == 1):        return 2;    else:        return (n * (n + 1) + sum(n - 1)); # Driver code n = 2;print(sum(n)); # This code is contributed by mits
// Csharp implementation of the approach using System; class Solution {     // Function to return a the required result    static int sum(int n)    {        if (n == 1) {            return 2;        }        else {            return (n * (n + 1) + sum(n - 1));        }    }    // Driver code    public static void Main()    {        int n = 2;        Console.WrieLine(sum(n));    }}
<?php// PHP implementation of the approach // Function to return sumfunction sum($n){    if ($n == 1)    {        return 2;    }    else    {        return ($n * ($n + 1) +                  sum($n - 1));    }} // Driver code$n = 2;echo sum($n); // This code is contributed by mits?>
<script> // JavaScript implementation of the approach // Function to return sumfunction sum(n){    if (n == 1) {        return 2;    }    else {        return (n * (n + 1) + sum(n - 1));    }} // Driver coden = 2;document.write(sum(n)); // This code is contributed by rutvik_56. </script>
8
 
Time Complexity: O(n)
Auxiliary Space: O(n)Efficient Solution We can solve this problem using direct formula. Sum can be written as below ∑(n * (n+1)) ∑(n*n + n) = ∑(n*n) + ∑(n)We can apply the formulas for sum squares of natural number and sum of natural numbers.= n(n+1)(2n+1)/6 + n*(n+1)/2 = n * (n + 1) * (n + 2) / 3 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return sumint sum(int n){    return n * (n + 1) * (n + 2) / 3;} // Driver codeint main(){    int n = 2;    cout << sum(n);}
// Java implementation of the approachclass GFG{     // Function to return sumstatic int sum(int n){    return n * (n + 1) * (n + 2) / 3;} // Driver codepublic static void main(String[] args){    int n = 2;    System.out.println(sum(n));}} // This code is contributed by Code_Mech
# Python3 implementation of the approach. # Function to return sumdef Sum(n):     return n * (n + 1) * (n + 2) // 3 # Driver codeif __name__ == "__main__":     n = 2;    print(Sum(n)) # This code is contributed# by Rituraj Jain
// C# implementation of the approachusing System;     class GFG{      // Function to return sumstatic int sum(int n){    return n * (n + 1) * (n + 2) / 3;}  // Driver codepublic static void Main(String[] args){    int n = 2;    Console.WriteLine(sum(n));}}// This code contributed by Rajput-Ji
<?php// PHP implementation of the approach // Function to return sumfunction sum($n){    return $n * ($n + 1) * ($n + 2) / 3;} // Driver code$n = 2;echo sum($n); // This code is contributed// by 29AjayKumar?>
<script>// Javascript implementation of the approach // Function to return sumfunction sum(n){    return n * (n + 1) * (n + 2) / 3;} // Driver codevar n = 2;document.write(sum(n)); // This code is contributed by noob2000.</script>
8
 
 Time Complexity: O(1)Auxiliary Space: O(1) 
Mithun Kumar
rituraj_jain
Code_Mech
Rajput-Ji
29AjayKumar
rutvik_56
noob2000
subham348
series-sum
Mathematical
Recursion
Mathematical
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Program to print prime numbers from 1 to N.
Merge two sorted arrays with O(1) extra space
Segment Tree | Set 1 (Sum of given range)
Fizz Buzz Implementation
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Backtracking | Introduction
Print all subsequences of a string