Given a sequence whose nth term is
T(n) = n2 – (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 (109 + 7).
Input: n = 3
S(3) = T(1) + T(2) + T(3) = (12 – 02) + (22 – 12) + (32 – 22) = 1 + 3 + 5 = 9
Input: n = 10
Approach: If we try to find out some initial terms of the sequence by putting n = 1, 2, 3, … in T(n) = n2 – (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) = n2
Below is the implementation of above approach:
- Multiply large integers under large modulo
- Summation of GCD of all the pairs up to N
- Summation of floor of harmonic progression
- Find m-th summation of first n natural numbers.
- Difference between the summation of numbers whose frequency of all digits are same and different
- Maximize the summation of numbers in a maximum of K moves in range [L, R]
- Find (a^b)%m where 'b' is very large
- Sum of two large numbers
- GCD of two numbers when one of them can be very large
- Find (a^b)%m where 'a' is very large
- LCM of two large numbers
- Recursive sum of digit in n^x, where n and x are very large
- Divisible by 37 for large numbers
- Divisibility by 12 for a large number
- Remainder with 7 for large numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.