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)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 + 1313 + 2323 + 3333 + 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.

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 = 4
k = 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 = 4
k = 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)



My Personal Notes arrow_drop_up


I am a selfmotivated and passionate programmer who loves to code and solve programming challenges

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.