# Sum of the first N terms of the series 5,12, 23, 38….

Given a number N, the task is to find the sum of first N terms of the below series:

Sn = 5 + 12 + 23 + 38 + … upto n terms

**Examples:**

Input: N = 2 Output: 17 5 + 12 = 17 Input: N = 4 Output: 80 5 + 12 + 23 + 38 = 78

**Approach:** Let, the nth term be denoted by tn.

This problem can easily with the help of a general formula for these type of series,

The series given above is a quadratic series. They are special because the difference of consecutive terms of this series will be in arithmetic progression.

There general formula is given by:

General Formula = a*(n^2) + b*n + c

Now, by putting first 3 terms of series in general formula we can get values of a, b and c.

Sn = 5 + 12 + 30 + 68 + ...... tn = 2 * (n^2) + n + 2 Sn = 2 * (n * (n+1) * (2 * n+1)/6) + n * (n+1)/2 + 2 * (n)

**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` `2 * (n * (n + 1) * (2 * n + 1) / 6) ` ` ` `+ n * (n + 1) / 2 + 2 * (n); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// number of terms to be included in sum ` ` ` `int` `n = 3; ` ` ` ` ` `// find the Sn ` ` ` `cout << ` `"Sum = "` `<< calculateSum(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of first n terms ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to calculate the sum ` ` ` `static` `int` `calculateSum(` `int` `n) ` `{ ` ` ` ` ` `return` `2` `* (n * (n + ` `1` `) * (` `2` `* n + ` `1` `) / ` `6` `) ` ` ` `+ n * (n + ` `1` `) / ` `2` `+ ` `2` `* (n); ` `} ` ` ` `// Driver code ` ` ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `// number of terms to be included in sum ` ` ` `int` `n = ` `3` `; ` ` ` ` ` `// find the Sn ` ` ` `System.out.print( ` `"Sum = "` `+ calculateSum(n)); ` ` ` `} ` `} ` `// This code is contributed ` `// by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python 3

`# Python program to find ` `# sum of first n terms ` ` ` `# Function to calculate the sum ` `def` `calculateSum(n) : ` ` ` ` ` `return` `(` `2` `*` `(n ` `*` `(n ` `+` `1` `) ` `*` ` ` `(` `2` `*` `n ` `+` `1` `) ` `/` `/` `6` `) ` `+` `n ` `*` ` ` `(n ` `+` `1` `) ` `/` `/` `2` `+` `2` `*` `(n)) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `# number of terms to be ` ` ` `# included in sum ` ` ` `n ` `=` `3` ` ` ` ` `# find the Sn ` ` ` `print` `(` `"Sum ="` `,calculateSum(n)) ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum ` `// of first n terms ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to calculate the sum ` `static` `int` `calculateSum(` `int` `n) ` `{ ` ` ` ` ` `return` `2 * (n * (n + 1) * (2 * n + 1) / 6) + ` ` ` `n * (n + 1) / 2 + 2 * (n); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () ` `{ ` ` ` `// number of terms to be ` ` ` `// included in sum ` ` ` `int` `n = 3; ` ` ` ` ` `// find the Sn ` ` ` `Console.WriteLine(` `"Sum = "` `+ calculateSum(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Shashank ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum ` `// of first n terms ` ` ` `// Function to calculate the sum ` `function` `calculateSum(` `$n` `) ` `{ ` ` ` ` ` `return` `2 * (` `$n` `* (` `$n` `+ 1) * ` ` ` `(2 * ` `$n` `+ 1) / 6) + ` ` ` `$n` `* (` `$n` `+ 1) / ` ` ` `2 + 2 * (` `$n` `); ` `} ` ` ` `// Driver code ` ` ` `// number of terms to ` `// be included in sum ` `$n` `= 3; ` ` ` `// find the Sn ` `echo` `"Sum = "` `. calculateSum(` `$n` `); ` ` ` `// This code is contributed ` `// by ChitraNayal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 40

## Recommended Posts:

- Find n terms of Fibonacci type series with given first two terms
- Sum of the first N terms of the series 2,10, 30, 68,....
- 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
- Sum of first n terms of a given series 3, 6, 11, .....
- Find the sum of n terms of the series 1,8,27,64 ....
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
- Sum of the series 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + - - - - - - upto n terms.
- Sum of series 2/3 - 4/5 + 6/7 - 8/9 + ------- upto n terms
- Find the sum of all the terms in the n-th row of the given series
- Sum of the series 0.7, 0.77, 0.777, ... upto n terms
- Sum of the series 0.6, 0.06, 0.006, 0.0006, ...to n terms
- Find the sum of first N terms of the series 2*3*5, 3*5*7, 4*7*9, ...
- Program to print the series 2, 1, 4, 3, 6, 5, .... 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.