Sum of series till N-th term whose i-th term is i^k – (i-1)^k
Last Updated :
19 Jul, 2022
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 = ik + (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)3
i.e 13 – 03 + 23 – 13 + 33 – 23 + 43 – 33
Rewriting the same powers together, we get.
03 + 13 – 13 + 23 – 23 + 33 – 33 + 43
i.e 03 + 13 – 13 + 23 – 23 + 33 – 33 + 43
So the final result will be 43 ( i.e nk )
Hence the final formula stands at NK
Below is the naive way to calculate NK.
C++
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int calculateSum( int n, int k)
{
long long res = 1;
for ( int i = 0; i < k; i++) {
res = (res * n) % MOD;
}
return res;
}
int main()
{
int n = 4, k = 3;
cout << calculateSum(n, k);
return 0;
}
|
Java
class GFG {
static long calculateSum( int n, int k)
{
long res = 1 ;
long MOD = 1000000007 ;
for ( int i = 0 ; i < k; i++) {
res = (res * n) % MOD;
}
return res;
}
public static void main(String[] args)
{
int n = 4 ;
int k = 3 ;
System.out.print(calculateSum(n, k));
}
};
|
Python3
def calculateSum(n, k):
res = 1
MOD = 1000000007
for i in range ( 0 , k):
res = ( res * n ) % MOD
return res
n = 4
k = 3
print (calculateSum(n, k))
|
C#
using System;
class GFG {
static int calculateSum( int n, int k)
{
int res = 1;
int MOD = 1000000007;
for ( int i = 0; i < k; i++) {
res = (res * n) % MOD;
}
return res;
}
static public void Main()
{
int n = 4;
int k = 3;
Console.WriteLine(calculateSum(n, k));
}
}
|
PHP
<?php
function calculateSum( $n , $k )
{
$res = 1;
$MOD = 1000000007;
for ( $i =0 ; $i < $k ; $i ++)
{
$res =( $res * $n )% $MOD ;
}
return $res ;
}
$n = 4;
$k = 3;
echo calculateSum( $n , $k );
?>
|
Javascript
<script>
const MOD = 1000000007;
function calculateSum( n, k)
{
let res = 1;
for (let i = 0; i < k; i++)
{
res = (res * n) % MOD;
}
return res;
}
let n = 4, k = 3;
document.write( calculateSum(n, k));
</script>
|
Time Complexity: O(k)
Auxiliary Space: O(1)
NK can be calculated in O(log N) using Modular Exponentiation.
C++
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int calculateSum( int n, int k)
{
long long res = 1;
while (k > 0) {
if (k & 1)
res = (res * n) % MOD;
k = k / 2;
n = (n * n) % MOD;
}
return res;
}
int main()
{
int n = 4, k = 3;
cout << calculateSum(n, k);
return 0;
}
|
Java
class GFG {
static long calculateSum( int n, int k)
{
int res = 1 ;
int MOD = 1000000007 ;
while (k > 0 ) {
if ((k & 1 ) == 1 )
res = (res * n) % MOD;
k = k / 2 ;
n = (n * n) % MOD;
}
return res;
}
public static void main(String[] args)
{
int n = 4 , k = 3 ;
System.out.print(calculateSum(n, k));
}
};
|
Python3
def calculateSum(n, k):
res = 1
MOD = 1000000007
while k > 0 :
if ( k & 1 ) = = 1 :
res = ( res * n ) % MOD
k = k / / 2
n = ( n * n ) % MOD
return res
n = 4
k = 3
print (calculateSum(n, k))
|
C#
using System;
class GFG {
static int calculateSum( int n, int k)
{
int res = 1;
int MOD = 1000000007;
while (k > 0) {
if ((k & 1) == 1)
res = (res * n) % MOD;
k = k / 2;
n = (n * n) % MOD;
}
return res;
}
static public void Main()
{
int n = 4;
int k = 3;
Console.WriteLine(calculateSum(n, k));
}
}
|
PHP
<?php
function calculateSum( $n , $k )
{
$res = 1;
$MOD = 1000000007;
while ( $k > 0)
{
if ( $k & 1)
$res = ( $res * $n ) % $MOD ;
$k = $k / 2;
$n =( $n * $n ) % $MOD ;
}
return $res ;
}
$n = 4;
$k = 3;
echo calculateSum( $n , $k );
?>
|
Javascript
<script>
function calculateSum(n, k)
{
let res = 1;
let MOD = 1000000007;
while (k > 0)
{
if ((k & 1) == 1)
res = (res * n) % MOD;
k = parseInt(k / 2);
n = (n * n) % MOD;
}
return res;
}
let n = 4, k = 3;
document.write(calculateSum(n, k));
</script>
|
Time Complexity: O(log n)
Space Complexity: O(1) since using constant variables
Share your thoughts in the comments
Please Login to comment...