We are given an integer n and n-th term in a series as expressed below:

T_{n}= n^{2}- (n-1)^{2}

We need to find S_{n} mod (10^{9} + 7), where S_{n} is the sum of all of the terms of the given series and,

S_{n}= T_{1}+ T_{2}+ T_{3}+ T_{4}+ ...... + T_{n}

Examples:

Input : 229137999 Output : 218194447 Input : 344936985 Output : 788019571

Let us do some calculations, before writing the program. T_{n} can be reduced to give 2n-1 . Let’s see how:

Given, T_{n}= n^{2}- (n-1)^{2}Or, T_{n}= n^{2}- (1 + n^{2}- 2n) Or, T_{n}= n^{2}- 1 - n^{2}+ 2n Or, T_{n}= 2n - 1.

Now, we need to find ∑T_{n}.

∑T_{n} = ∑(2n – 1)

We can simplify the above formula as,

∑(2n – 1) = 2*∑n – ∑1

Or, ∑(2n – 1) = 2*∑n – n.

Where, ∑n is the sum of first n natural numbers.

We know the sum of n natural number = n(n+1)/2.

Therefore, putting this value in the above equation we will get,

∑T_{n} = (2*(n)*(n+1)/2)-n = n^{2}

Now the value of n^{2} can be very large. So instead of direct squaring n and taking mod of the result. We will use the property of modular multiplication for calculating squares:

(a*b)%k = ((a%k)*(b%k))%k

Below is the implementation of above idea:

## CPP

`// CPP program to find sum of given ` `// series. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define mod 1000000007 ` ` ` `// Function to find sum of series ` `// with nth term as n^2 - (n-1)^2 ` `long` `long` `findSum(` `long` `long` `n) ` `{ ` ` ` `return` `((n%mod)*(n%mod))%mod; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `long` `long` `n = 229137999; ` ` ` `cout << findSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of given ` `// series. ` ` ` `public` `class` `FINDSUM { ` ` ` ` ` `static` `long` `mod = ` `1000000007` `; ` ` ` ` ` `public` `static` `long` `findSum(` `long` `n) ` ` ` `{ ` ` ` `return` `((n % mod) * (n % mod)) % mod; ` ` ` `} ` ` ` ` ` `public` `static` `void` `main(String[] args) { ` ` ` `long` `n = ` `229137999` `; ` ` ` `System.out.print (findSum(n)); ` ` ` `} ` `} ` ` ` `// Contributed by _omg ` |

*chevron_right*

*filter_none*

## Python 3

`# Python program to find sum of given ` `# series. ` ` ` `mod ` `=` `1000000007` `def` `findSum(n): ` ` ` `return` `((n ` `%` `mod) ` `*` `(n ` `%` `mod)) ` `%` `mod ` ` ` ` ` `# main() ` `n ` `=` `229137999` `print` `(findSum(n)) ` ` ` `# Contributed by _omg ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of given ` `// series. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `long` `mod = 1000000007; ` ` ` ` ` `static` `long` `findSum(` `long` `n) ` ` ` `{ ` ` ` `return` `((n % mod) * (n % mod)) % mod; ` ` ` `} ` ` ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `long` `n = 229137999; ` ` ` `Console.Write (findSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by _omg ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find ` `// sum of givenseries. ` ` ` `$mod` `= 1000000007; ` ` ` `// Function to find sum of series ` `// with nth term as n^2 - (n-1)^2 ` `function` `findSum(` `$n` `) ` `{ ` ` ` `global` `$mod` `; ` ` ` `return` `((` `$n` `% ` `$mod` `) * ` ` ` `(` `$n` `% ` `$mod` `)) % ` `$mod` `; ` `} ` ` ` `// Driver code ` `$n` `= 229137999; ` `echo` `(findSum(` `$n` `)); ` ` ` `// This code is contributed by Ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

218194447

## Recommended Posts:

- Find Nth term of the series where each term differs by 6 and 2 alternately
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Program for sum of cosh(x) series upto Nth term
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......
- Program to find Nth term in the series 0, 0, 2, 1, 4, 2, 6, 3, 8,...
- Program to find Nth term of series 9, 23, 45, 75, 113...
- Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
- Find Nth term of the series 5, 13, 25, 41, 61...
- Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
- Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
- Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
- Find Nth term of the series 3, 14, 39, 84...
- Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
- Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
- Find Nth term of the series 1, 8, 54, 384...

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.