Given a sequence whose **n ^{th}** term is

T(n) = n

^{2}– (n – 1)^{2}

The task is to evaluate the sum of first n terms i.e.

S(n) = T(1) + T(2) + T(3) + … + T(n)

Print **S(n) mod (10 ^{9} + 7)**.

**Examples:**

Input:n = 3

Output:9

S(3) = T(1) + T(2) + T(3) = (1^{2}– 0^{2}) + (2^{2}– 1^{2}) + (3^{2}– 2^{2}) = 1 + 3 + 5 = 9

Input:n = 10

Output:100

**Approach:** If we try to find out some initial terms of the sequence by putting **n = 1, 2, 3, …** in **T(n) = n ^{2} – (n – 1)^{2}**, we find the sequence

**1, 3, 5, …**

Hence, we find an A.P. where

**first term**is

**1**and

**d**(common difference between consecutive

terms) is

**2**.

The formula for the sum of n terms of A.P is

S(n) = n / 2 [ 2 * a + (n – 1) * d ]

where **a** is the first term.

So, putting **a = 1** and **d = 2**, we get

S(n) = n

^{2}

.

Below is the implementation of above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define ll long long int ` `#define MOD 1000000007 ` ` ` `// Function to return the sum ` `// of the given series ` `int` `sumOfSeries(` `int` `n) ` `{ ` ` ` `ll ans = (ll)` `pow` `(n % MOD, 2); ` ` ` ` ` `return` `(ans % MOD); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 10; ` ` ` `cout << sumOfSeries(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `public` `static` `final` `int` `MOD = ` `1000000007` `; ` ` ` `// Function to return the sum ` `// of the given series ` `static` `int` `sumOfSeries(` `int` `n) ` `{ ` ` ` `int` `ans = (` `int` `)Math.pow(n % MOD, ` `2` `); ` ` ` ` ` `return` `(ans % MOD); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `10` `; ` ` ` `System.out.println(sumOfSeries(n)); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of the approach ` `from` `math ` `import` `pow` ` ` `MOD ` `=` `1000000007` ` ` `# Function to return the sum ` `# of the given series ` `def` `sumOfSeries(n): ` ` ` `ans ` `=` `pow` `(n ` `%` `MOD, ` `2` `) ` ` ` ` ` `return` `(ans ` `%` `MOD) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `10` ` ` `print` `(` `int` `(sumOfSeries(n))) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `const` `int` `MOD = 1000000007; ` ` ` `// Function to return the sum ` `// of the given series ` `static` `int` `sumOfSeries(` `int` `n) ` `{ ` ` ` `int` `ans = (` `int` `)Math.Pow(n % MOD, 2); ` ` ` ` ` `return` `(ans % MOD); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 10; ` ` ` `Console.Write(sumOfSeries(n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` `$GLOBALS` `[` `'MOD'` `] = 1000000007; ` ` ` `// Function to return the sum ` `// of the given series ` `function` `sumOfSeries(` `$n` `) ` `{ ` ` ` `$ans` `= pow(` `$n` `% ` `$GLOBALS` `[` `'MOD'` `], 2); ` ` ` ` ` `return` `(` `$ans` `% ` `$GLOBALS` `[` `'MOD'` `]); ` `} ` ` ` `// Driver code ` `$n` `= 10; ` `echo` `sumOfSeries(` `$n` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

100

## Recommended Posts:

- Comparing X^Y and Y^X for very large values of X and Y
- Check whether a very large number of the given form is a multiple of 3.
- Recursive sum of digit in n^x, where n and x are very large
- GCD of two numbers when one of them can be very large
- Find (a^b)%m where 'a' is very large
- Find (a^b)%m where 'b' is very large
- Number of factors of very large number N modulo M where M is any prime number
- Print all K digit repeating numbers in a very large number
- How to store a very large number of more than 100 digits in C++
- Multiply large integers under large modulo
- Summation of GCD of all the pairs up to N
- Find m-th summation of first n natural numbers.
- Summation of floor of harmonic progression
- Digital Root (repeated digital sum) of the given large integer
- Given a large number, check if a subsequence of digits is divisible by 8
- Find smallest possible Number from a given large Number with same count of digits
- Digital Root of a given large integer using Recursion
- Check if a large number is divisible by 2, 3 and 5 or not
- Sum of all natural numbers from L to R ( for large values of L and R )
- Factorial of a large number

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.