Sum of series till N-th term whose i-th term is i^k – (i-1)^k
Given value of N and K. The task is to find the sum of series till N-th term whose i-th term is given by Ti = i k + ( i – 1 )k. Since the sum of the series can be very large, compute its sum modulo 1000000007.
Example:
Input : n = 5, k = 2 Output: 25 first 5 term of the series : T1 = ( 1 )2 + ( 1 - 1 )2 = 1 T2 = ( 2 )2 + ( 2 - 1 )2 = 3 T3 = ( 3 )2 + ( 3 - 1 )2 = 5 T4 = ( 4 )2 + ( 4 - 1 )2 = 7 T4 = ( 5 )2 + ( 5 - 1 )2 = 9 Sum of the series = 1 + 3 + 5 + 7 + 9 = 25 Input: n = 4, k = 3 Output : 64 First 4 term of the series: T2 = ( 1 )3 + ( 1 - 1 )3 = 1 T3 = ( 2 )3 + ( 2 - 1 )3 = 7 T4 = ( 3 )3 + ( 3 - 1 )3 = 19 T4 = ( 4 )3 + ( 4 - 1 )3 = 37 Sum of the series = 1 + 7 + 19 + 37 = 64
Naive Approach A simple solution is to generate all terms up to n and add them. The time complexity will be O(N). Since N is very large, the approach will result in TLE.
Better solution
Let’s have a look at the series pattern.
Consider N = 4, K = 3
Then the series will be :
13 – (1 – 1)3 + 23 – (2 – 1)3 + 13 – (3 – 1)3 + 43 – (4 – 1)3i.e 13 – 03 + 23 – 13 + 33 – 23 + 43 – 33
Rewriting the same powers together, we get.
03 + 13 – 13 + 23 – 23 + 33 – 33 + 43i.e
03 +13 –13 +23 –23 +33 –33 + 43So the final result will be 43 ( i.e nk )
Hence the final formula stands at NK
Below is the naive way to calculate NK.
CPP
// CPP Code to find sum of series #include <bits/stdc++.h> using namespace std; #define MOD 1000000007 // function to calculate sum of series int calculateSum(int n, int k) { // Initialize result long long res = 1; // loop to calculate n^k for (int i = 0; i < k; i++) { res = (res * n) % MOD; } return res; } // Driver code int main() { int n = 4, k = 3; // function calling cout << calculateSum(n, k); return 0; } |
Java
// JAVA code to find // Sum of series class GFG { // function to calculate sum of series static long calculateSum(int n, int k) { // Initialize res and MOD long res = 1; long MOD = 1000000007; // loop to calculate n^k % MOD for (int i = 0; i < k; i++) { res = (res * n) % MOD; } return res; } // Driver code public static void main(String[] args) { int n = 4; int k = 3; System.out.print(calculateSum(n, k)); } }; |
Python3
# Python 3 code to find # the sum of series # function to calculate sum of series def calculateSum(n, k): # initialize res res = 1 # initialize MOD MOD = 1000000007 # loop to calculate n ^ k % MOD for i in range ( 0, k): res = ( res * n ) % MOD return res # Driver code n = 4k = 3 # function calling print(calculateSum(n, k)) |
C#
// C# code to find the // sum of the series using System; class GFG { // function to calculate sum of the series static int calculateSum(int n, int k) { // initialize res int res = 1; // Initialize MOD int MOD = 1000000007; // loop to calculate n^k % MOD for (int i = 0; i < k; i++) { res = (res * n) % MOD; } return res; } // Driver Code static public void Main() { int n = 4; int k = 3; // function calling Console.WriteLine(calculateSum(n, k)); } } |
PHP
// PHP code to find // the sum of series <?php // function to calculate sum of the series function calculateSum($n, $k) { // Initialize res $res = 1; // Initialize MOD $MOD = 1000000007; // loop to calculate n^k % MOD for( $i=0 ; $i < $k ; $i++) { $res =( $res * $n )% $MOD; } return $res; } // Driver Code $n = 4; $k = 3; // function callling echo calculateSum($n, $k); ?> |
Time complexity: O(K)
NK can be calculated in O(log N) using Modular Exponentiation.
CPP
// CPP Code to find sum of series #include <bits/stdc++.h> using namespace std; #define MOD 1000000007 // function to calculate sum of series int calculateSum(int n, int k) { // initialize res long long res = 1; // loop to calculate n^k % MOD // using modular Arithmetic while (k > 0) { // if k is odd // multiply it with res if (k & 1) res = (res * n) % MOD; // k must be even now // change k to k/2 k = k / 2; // change n to n^2 n = (n * n) % MOD; } return res; } // Driver code int main() { int n = 4, k = 3; cout << calculateSum(n, k); return 0; } |
Java
// JAVA code to find sum of series class GFG { // Function to calculate sum of series static long calculateSum(int n, int k) { // initialize res and MOD int res = 1; int MOD = 1000000007; // loop to calculate n^k % MOD // using modular Arithmetic while (k > 0) { // if k is odd // multiply n with res if ((k & 1) == 1) res = (res * n) % MOD; // k must be even now // change k to k / 2 k = k / 2; // change n to n^2 n = (n * n) % MOD; } return res; } // Driver code public static void main(String[] args) { int n = 4, k = 3; System.out.print(calculateSum(n, k)); } }; |
Python3
# Python code to calculate sum of series # function to calculate sum of series def calculateSum(n, k): # Initialize res res = 1 # initialize MOD MOD = 1000000007 # loop to calculate n ^ k % MOD # using Modular airthmatic while k > 0 : # if k is odd # Multiply n with res if ( k & 1 ) == 1 : res = ( res * n ) % MOD # k must be even now # change k to k / 2 k = k // 2 # change n to n ^ 2 n =( n * n ) % MOD return res # Driver code n = 4k = 3 print(calculateSum(n, k)) |
C#
// C# code to calculate // sum of series using System; class GFG { // Function to calculate sum of series static int calculateSum(int n, int k) { int res = 1; // Initialize res int MOD = 1000000007; // Loop to calculate n^k % MOD while (k > 0) { // If y is odd // multiply res with n if ((k & 1) == 1) res = (res * n) % MOD; // k must be even now // change k to k/2 k = k / 2; // change n to n^2 n = (n * n) % MOD; } return res; } // Driver Code static public void Main() { int n = 4; int k = 3; // Function calling Console.WriteLine(calculateSum(n, k)); } } |
PHP
// PHP code to calculate // Sum of series <?php // Function to calculate sum of series function calculateSum($n, $k) { // Initialize res $res = 1; // Initialize MOD $MOD = 1000000007; // Loop to calculate n^k % MOD // using Modular arithmetic while ($k > 0) { // If y is odd // multiply with result if ($k & 1) $res = ( $res * $n ) % $MOD; // k must be even now // Change k to k/2 $k = $k / 2; // Change n to n^2 $n =( $n * $n ) % $MOD; } return $res; } // Driver Code $n = 4; $k = 3; // Function calling echo calculateSum($n, $k); ?> |
Time Complexity: O(log n)
Recommended Posts:
- Sum of the series 1, 2, 4, 3, 5, 7, 9, 6, 8, 10, 11, 13.. till N-th term
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- N-th term in the series 1, 11, 55, 239, 991,....
- n-th term of series 1, 17, 98, 354......
- n-th term in series 2, 12, 36, 80, 150....
- Sum of first n term of Series 3, 5, 9, 17, 33....
- Find n-th term of series 1, 3, 6, 10, 15, 21...
- Find n-th term in series 1 2 2 3 3 3 4 4 4 4....
- Finding n-th term of series 3, 13, 42, 108, 235…
- Find Nth term of the series 1, 5, 32, 288 ...
- Find Nth term of the series 1, 6, 18, 40, 75, ....
- Find n-th term of series 2, 10, 30, 68, 130 ...
- Find Nth term of the series 0, 6, 0, 12, 0, 90...
- Find the Nth term of the series 1 + 2 + 6 + 15 + 31 + 56 + ...
- Find sum of Series with n-th term as n^2 - (n-1)^2
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.



