# Find the sum of the series 2, 5, 13, 35, 97…

Given a series and a number n, the task is to find the sum of its first n terms. Below is the series:

2, 5, 13, 35, 97, …

**Examples:**

Input: N = 2 Output: 7 The sum of first 2 terms of Series is 2 + 5 = 7 Input: N = 4 Output: 55 The sum of first 4 terms of Series is 2 + 5 + 13 + 35 = 55

**Approach: ** From this given series we find it is the sum of the Two **GP** series with common ratioes 2, 3 .

Sn = 2 + 5 + 13 + 35 + 97 … + upto nth term

Sn = (2^0 + 3^ 0) + (2^1 + 3^1) + (2^2 + 3^2) + (2^3 + 3^3)+ (2^4 + 3^4) …… + upto nth term

Sn = (2^0 + 2^1 + 2^2 + 2^3 + 2^4 … + upto nth term) + ( 3^0 + 3^1 + 3^2 + 3^3 …… + unto nth term )

Since, We know that the sum of n terms of the **GP** is given by the following formula:

Below is the implementation of the above approach:

## C++

`// C++ program for finding the sum ` `// of first N terms of the series. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// CalculateSum function returns the final sum ` `int` `calculateSum(` `int` `n) ` `{ ` ` ` `// r1 and r2 are common ratios ` ` ` `// of 1st and 2nd series ` ` ` `int` `r1 = 2, r2 = 3; ` ` ` ` ` `// a1 and a2 are common first terms ` ` ` `// of 1st and 2nd series ` ` ` `int` `a1 = 1, a2 = 1; ` ` ` ` ` `return` `a1 * (` `pow` `(r1, n) - 1) / (r1 - 1) ` ` ` `+ a2 * (` `pow` `(r2, n) - 1) / (r2 - 1); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `// function calling for 4 terms ` ` ` `cout << ` `"Sum = "` `<< calculateSum(n) ` ` ` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java program for finding the sum ` `//of first N terms of the series. ` ` ` `public` `class` `GFG { ` ` ` ` ` `//CalculateSum function returns the final sum ` ` ` `static` `int` `calculateSum(` `int` `n) ` ` ` `{ ` ` ` `// r1 and r2 are common ratios ` ` ` `// of 1st and 2nd series ` ` ` `int` `r1 = ` `2` `, r2 = ` `3` `; ` ` ` ` ` `// a1 and a2 are common first terms ` ` ` `// of 1st and 2nd series ` ` ` `int` `a1 = ` `1` `, a2 = ` `1` `; ` ` ` ` ` `return` `(` `int` `)(a1 * (Math.pow(r1, n) - ` `1` `) / (r1 - ` `1` `) ` ` ` `+ a2 * (Math.pow(r2, n) - ` `1` `) / (r2 - ` `1` `)); ` ` ` `} ` ` ` ` ` `//Driver code ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` ` ` `int` `n = ` `4` `; ` ` ` ` ` `// function calling for 4 terms ` ` ` `System.out.println(` `"Sum = "` `+calculateSum(n)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program for finding the sum ` `# of first N terms of the series. ` ` ` `# from math import everything ` `from` `math ` `import` `*` ` ` `# CalculateSum function returns the final sum ` `def` `calculateSum(n) : ` ` ` ` ` `# r1 and r2 are common ratios ` ` ` `# of 1st and 2nd series ` ` ` `r1, r2 ` `=` `2` `, ` `3` ` ` ` ` `# a1 and a2 are common first terms ` ` ` `# of 1st and 2nd series ` ` ` `a1, a2 ` `=` `1` `, ` `1` ` ` ` ` `return` `(a1 ` `*` `(` `pow` `(r1, n) ` `-` `1` `) ` `/` `/` `(r1 ` `-` `1` `) ` ` ` `+` `a2 ` `*` `(` `pow` `(r2, n) ` `-` `1` `) ` `/` `/` `(r2 ` `-` `1` `)) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `4` ` ` ` ` `# function calling for 4 terms ` ` ` `print` `(` `"SUM = "` `,calculateSum(n)) ` ` ` ` ` `# This code is contributed by ANKITRAI1 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for finding the sum ` `// of first N terms of the series. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// CalculateSum function ` `// returns the final sum ` `static` `int` `calculateSum(` `int` `n) ` `{ ` `// r1 and r2 are common ratios ` `// of 1st and 2nd series ` `int` `r1 = 2, r2 = 3; ` ` ` `// a1 and a2 are common first ` `// terms of 1st and 2nd series ` `int` `a1 = 1, a2 = 1; ` ` ` `return` `(` `int` `)(a1 * (Math.Pow(r1, n) - 1) / (r1 - 1) + ` ` ` `a2 * (Math.Pow(r2, n) - 1) / (r2 - 1)); ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `n = 4; ` ` ` ` ` `// function calling for 4 terms ` ` ` `Console.Write(` `"Sum = "` `+ ` ` ` `calculateSum(n)); ` `} ` `} ` ` ` `// This code is contributed by Raj ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for finding the sum ` `// of first N terms of the series. ` ` ` `// CalculateSum function returns ` `// the final sum ` `function` `calculateSum(` `$n` `) ` `{ ` ` ` `// r1 and r2 are common ratios ` ` ` `// of 1st and 2nd series ` ` ` `$r1` `= 2; ` ` ` `$r2` `= 3; ` ` ` ` ` `// a1 and a2 are common first ` ` ` `// terms of 1st and 2nd series ` ` ` `$a1` `= 1; ` ` ` `$a2` `= 1; ` ` ` ` ` `return` `$a1` `* (pow(` `$r1` `, ` `$n` `) - 1) / ` ` ` `(` `$r1` `- 1) + ` `$a2` `* ` ` ` `(pow(` `$r2` `, ` `$n` `) - 1) / ` ` ` `(` `$r2` `- 1); ` `} ` ` ` `// Driver code ` `$n` `= 4; ` ` ` `// function calling for 4 terms ` `echo` `"Sum = "` `, calculateSum(` `$n` `); ` ` ` `// This code is contributed by ash264 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Sum = 55

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the sum of series 3, 7, 13, 21, 31....
- Find sum of the series 1-2+3-4+5-6+7.......
- Find the sum of the series x(x+y) + x^2(x^2+y^2) +x^3(x^3+y^3)+ ... + x^n(x^n+y^n)
- Find nth term of the series 5 2 13 41
- Find Nth term of the series 4, 2, 2, 3, 6, ...
- Find the Nth term of the series 2, 15, 41, 80, 132...
- Find Nth term of the series 2, 3, 10, 15, 26....
- Program to find the sum of the series 1 + x + x^2+ x^3+ .. + x^n
- Find the Nth term in series 12, 35, 81, 173, 357, ...
- Find the Nth term of the series 2 + 6 + 13 + 23 + . . .
- Find the sum of first N terms of the series 2×3 + 4×4 + 6×5 + 8×6 + ...
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 2 + 2 + 3 + 3 + 3 + . . . + n
- Find n-th term of series 3, 9, 21, 41, 71...
- Find the nth term of the given series
- Find Nth number of the series 1, 6, 15, 28, 45, .....
- Find Nth term of series 1, 4, 15, 72, 420...
- Find the Nth term of the series 14, 28, 20, 40,.....
- Find n-th term in the series 9, 33, 73,129 ...
- Find Nth term of the series 3, 14, 39, 84...

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.