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

Given a value n, the task is to find sum of the series (1*2) + (2*3) + (3*4) + ……+ n terms

Examples:

```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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Simple Solution One by one add elements recursively.

Below is the implementation

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return sum ` `int` `sum(``int` `n) ` `{ ` `    ``if` `(n == 1) { ` `        ``return` `2; ` `    ``} ` `    ``else` `{ ` `        ``return` `(n * (n + 1) + sum(n - 1)); ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``int` `n = 2; ` `    ``cout << sum(n); ` `} `

## Java

 `// 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

 `# Python3 implementation of the approach ` ` `  `# Function to return sum ` `def` `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 `

## C#

 `// 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

 ` `

Output:

```8
```

Time Complexity: O(n)

Efficient Solution We can solve this problem using direct formula.
Sum can be written as below
&Sum;(n * (n+1))
&Sum;(n*n + n)
= &Sum;(n*n) + &Sum;(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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return sum ` `int` `sum(``int` `n) ` `{ ` `    ``return` `n * (n + 1) * (n + 2) / 3; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 2; ` `    ``cout << sum(n); ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` `     `  `// Function to return sum ` `static` `int` `sum(``int` `n) ` `{ ` `    ``return` `n * (n + ``1``) * (n + ``2``) / ``3``; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``2``; ` `    ``System.out.println(sum(n)); ` `} ` `} ` ` `  `// This code is contributed by Code_Mech `

## Python3

 `# Python3 implementation of the approach.  ` ` `  `# Function to return sum  ` `def` `Sum``(n):  ` ` `  `    ``return` `n ``*` `(n ``+` `1``) ``*` `(n ``+` `2``) ``/``/` `3` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``:  ` ` `  `    ``n ``=` `2``;  ` `    ``print``(``Sum``(n))  ` ` `  `# This code is contributed  ` `# by Rituraj Jain `

## C#

 `// C# implementation of the approach ` `using` `System; ` `     `  `class` `GFG ` `{ ` `      `  `// Function to return sum ` `static` `int` `sum(``int` `n) ` `{ ` `    ``return` `n * (n + 1) * (n + 2) / 3; ` `} ` `  `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `n = 2; ` `    ``Console.WriteLine(sum(n)); ` `} ` `} ` `// This code contributed by Rajput-Ji `

## PHP

 ` `

Output:

```8
```

Time Complexity: O(1)

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.