We are given an integer n and n-th term in a series as expressed below:
Tn = n2 - (n-1)2
We need to find Sn mod (109 + 7), where Sn is the sum of all of the terms of the given series and,
Sn = T1 + T2 + T3 + T4 + ...... + Tn
Input : 229137999 Output : 218194447 Input : 344936985 Output : 788019571
Let us do some calculations, before writing the program. Tn can be reduced to give 2n-1 . Let\’s see how:
Given, Tn = n2 - (n-1)2 Or, Tn = n2 - (1 + n2 - 2n) Or, Tn = n2 - 1 - n2 + 2n Or, Tn = 2n - 1.
Now, we need to find ∑Tn.
∑Tn = ∑(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,
∑Tn = (2*(n)*(n+1)/2)-n = n2
Now the value of n2 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
Please refer complete article on Find sum of Series with n-th term as n^2 – (n-1)^2 for more details!
- Python Program for Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Python Program for Print Number series without using any loop
- Python Program for n\'th multiple of a number in Fibonacci Series
- Python | Find fibonacci series upto n using lambda
- Python Program for Program to find area of a circle
- Python program to find the first day of given year
- Python Program to find sum of array
- Python Program to find whether a no is power of two
- Python program to find all possible pairs with given sum
- Python Program for Find sum of odd factors of a number
- Python Program for Find the perimeter of a cylinder
- Python Program for Find sum of even factors of a number
- Python Program to find transpose of a matrix
- Python program to find second maximum value in Dictionary
- Python program to find Cumulative sum of a list