Skip to content
Related Articles

Related Articles

Improve Article

Program to find sum of the given sequence

  • Last Updated : 20 Apr, 2021

Given two numbers N  and K  . The task is to find the sum of the sequence given below.
 

(1*2*3*…*k) + (2*3*…*k*(k+1)) + (3*4*..*(k+1)*(k+2)) +…..+((n-k+1)*(n-k+2)*…*(n-k+k)).

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Since the output can be large, print the answer under modulo 10^9+7.
Examples
 

Input : N = 3, K = 2
Output : 8

Input : N = 4, K = 2
Output : 20

 



Let us take the given example and try to reduce it to a general formula.
In the given example for n = 3 and k=2
 

Sum = 1*2 + 2*3 

We know that: 
$1\times2=2!\times 0!$\\ $2\times3=3!\times1!$\\
So each term is of the form: 

    $$\frac{x!}{\left(x-k\right)!}$$

If we multiply and divide by k!  , it becomes 

    $$k!\cdot\frac{x!}{\left(x-k\right)!\cdot k!}$$

Which is nothing but, 
$k!\times$ ${N}\choose{k}$\\
Therefore, 
sum = $k\times \sum_{x=k}^{n}$ $ {x}\choose{k}$ =$ {n+1}\choose{k+1}$ $\times k! $\\
But since n is so large we can not calculate it directly, we have to simplify the above expression.
On Simplifying we get, 

    $$\frac{\left(n\right) \cdot \left(n-1\right) \cdot \left(n-2\right) \dots \left(n-k+1\right)}{k+1}$$

Below is the implementation of the above idea: 
 

C++




// CPP program to find the sum of the
// given sequence
 
#include <bits/stdc++.h>
using namespace std;
 
const long long MOD = 1000000007;
 
// function to find moudulo inverse
// under 10^9+7
long long modInv(long long x)
{
    long long n = MOD - 2;
    long long result = 1;
    while (n) {
        if (n & 1)
            result = result * x % MOD;
        x = x * x % MOD;
        n = n / 2;
    }
     
    return result;
}
 
// Function to find the sum of the
// given sequence
long long getSum(long long n, long long k)
{
    long long ans = 1;
     
    for (long long i = n + 1; i > n - k; i--)
        ans = ans * i % MOD;
    ans = ans * modInv(k + 1) % MOD;
     
    return ans;
}
 
// Driver code
int main()
{
    long long n = 3, k = 2;
     
    cout<<getSum(n,k);
     
    return 0;
}

Java




// Java program to find the sum of the
// given sequence
 
class GFG {
 
    static long MOD = 1000000007;
 
// function to find moudulo inverse
// under 10^9+7
    static long modInv(long x) {
        long n = MOD - 2;
        long result = 1;
        while (n > 0) {
            if ((n & 1) > 0) {
                result = result * x % MOD;
            }
            x = x * x % MOD;
            n = n / 2;
        }
 
        return result;
    }
 
// Function to find the sum of the
// given sequence
    static long getSum(long n, long k) {
        long ans = 1;
 
        for (long i = n + 1; i > n - k; i--) {
            ans = ans * i % MOD;
        }
        ans = ans * modInv(k + 1) % MOD;
 
        return ans;
    }
 
// Driver code
    public static void main(String[] args) {
        long n = 3, k = 2;
        System.out.println(getSum(n, k));
    }
}

Python3




# Python3 program to find the sum
# of the given sequence
 
MOD = 1000000007;
 
# function to find moudulo inverse
# under 10^9+7
def modInv(x):
 
    n = MOD - 2;
    result = 1;
    while (n):
        if (n&1):
            result = result * x % MOD;
        x = x * x % MOD;
        n = int(n / 2);
     
    return result;
 
# Function to find the sum of
# the given sequence
def getSum(n, k):
 
    ans = 1;
     
    for i in range(n + 1, n - k, -1):
        ans = ans * i % MOD;
    ans = ans * modInv(k + 1) % MOD;
     
    return ans;
 
# Driver code
n = 3;
k = 2;
     
print(getSum(n,k));
     
# This code is contributed by mits

C#




// C# program to find the sum of the
// given sequence
using System;
 
  
 
// function to find moudulo inverse
// under 10^9+7
class gfg
{
    public long MOD = 1000000007;
 public long modInv(long x)
 {
    long n = MOD - 2;
    long result = 1;
    while (n >0) {
        if ((n & 1) > 0)
            result = result * x % MOD;
        x = x * x % MOD;
        n = n / 2;
    }
     
    return result;
}
 
// Function to find the sum of the
// given sequence
  public long getSum(long n, long k)
  {
    long ans = 1;
     
    for (long i = n + 1; i > n - k; i--)
        ans = ans * i % MOD;
    ans = ans * modInv(k + 1) % MOD;
     
    return ans;
 }
}
 
// Driver code
class geek
{
  public static int Main()
 {
     gfg g = new gfg();
    long n = 3, k = 2;
     
    Console.WriteLine(g.getSum(n,k));
     
    return 0;
 }
}
//This code is contributed by SoumikMondal

PHP




<?php
// PHP program to find the sum of
// the given sequence
 
// function to find moudulo inverse
// under 10^9+7
function modInv($x)
{
    $MOD = 1000000007;
    $n = $MOD - 2;
    $result = 1;
    while ($n)
    {
        if ($n & 1)
            $result = $result * $x % $MOD;
        $x = $x * $x % $MOD;
        $n = $n / 2;
    }
     
    return $result;
}
 
// Function to find the sum of the
// given sequence
function getSum($n, $k)
{
    $MOD = 1000000007;
    $ans = 1;
     
    for ($i = $n + 1; $i > $n - $k; $i--)
        $ans = $ans * $i % $MOD;
    $ans = $ans * modInv($k + 1) % $MOD;
     
    return $ans;
}
 
// Driver code
$n = 3; $k = 2;
     
echo getSum($n, $k);
 
// This code is contributed
// by Akanksha Rai
?>

Javascript




<script>
 
// Javascript program to find the sum of the
// given sequence
 
var MOD = 100000007;
 
// function to find moudulo inverse
// under 10^9+7
function modInv(x)
{
    var n = MOD - 2;
    var result = 1;
    while (n) {
        if (n & 1)
            result = result * x % MOD;
        x = x * x % MOD;
        n = n / 2;
    }
     
    return result;
}
 
// Function to find the sum of the
// given sequence
function getSum(n, k)
{
    var ans = 1;
     
    for (var i = n + 1; i > n - k; i--)
        ans = ans * i % MOD;
    ans = ans * modInv(k + 1) % MOD;
     
    return ans;
}
 
// Driver code
var n = 3, k = 2;
 
document.write( getSum(n,k));
 
// This code is contributed by noob2000.
</script>
Output: 
8

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :