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:
- 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