Summing the sum series

Defined a function that calculates the twice of sum of first N natural numbers as sum(N). Your task is to modify the function to sumX(N, M, K) that calculates sum( K + sum( K + sum( K + …sum(K + N)…))), continuing for M terms. For a given N, M and K calculate the value of sumX(N, M, K).
Note: Since the answer can be very large, print the answer in modulo 10^9 + 7.

Examples:

Input: N = 1, M = 2, K = 3
Output: 552
For M = 2
sum(3 + sum(3 + 1)) = sum(3 + 20) = 552.



Input: N = 3, M =3, K = 2
Output: 1120422
For M = 3
sum(2 + sum(2 + sum(2 + 3))) = sum(2 + sum(2 + 30)) = sum(2 + 1056) = 1120422.

Approach:

  • Calculate value of sum(N) using the formula N*(N + 1).
  • Run a loop M times, each time adding K to the previous answer and applying sum(prev_ans + K), modulo 10^9 + 7 each time.
  • Print the value of sumX(N, M, K) in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to calculate the  
// terms of summing of sum series
  
#include <iostream>
  
using namespace std;
# define MOD 1000000007
  
// Function to calculate 
// twice of sum of first N natural numbers
long sum(long N){
      
    long val = N * (N+1);
    val = val % MOD;
      
    return val;
}
  
// Function to calculate the 
// terms of summing of sum series
int sumX(int N, int M, int K){
      
    for (int i = 0; i < M; i++) { 
        N = (int)sum(K + N); 
    
      
    N = N % MOD; 
    return N;
}
  
// Driver Code
int main()
{
    int N = 1, M = 2, K = 3;
    cout << sumX(N, M, K) << endl; 
     
    return 0;
}
  
// This code is contributed by Rituraj Jain

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to calculate the 
// terms of summing of sum series
  
import java.io.*;
import java.util.*;
import java.lang.*;
  
class GFG {
  
    static int MOD = 1000000007;
  
    // Function to calculate
    // twice of sum of first N natural numbers
    static long sum(long N)
    {
        long val = N * (N + 1);
  
        // taking modulo 10 ^ 9 + 7
        val = val % MOD;
  
        return val;
    }
  
    // Function to calculate the
    // terms of summing of sum series
    static int sumX(int N, int M, int K)
    {
        for (int i = 0; i < M; i++) {
            N = (int)sum(K + N);
        }
        N = N % MOD;
        return N;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int N = 1, M = 2, K = 3;
        System.out.println(sumX(N, M, K));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to calculate the  
# terms of summing of sum series 
    
MOD = 1000000007 
    
# Function to calculate 
# twice of sum of first N natural numbers 
def Sum(N): 
       
    val = N * (N + 1
    
    # taking modulo 10 ^ 9 + 7 
    val = val % MOD 
    
    return val 
    
# Function to calculate the 
# terms of summing of sum series 
def sumX(N, M, K): 
       
    for i in range(M):
        N = int(Sum(K + N)) 
           
    N = N % MOD 
    return
    
if __name__ == "__main__":
       
    N, M, K = 1, 2, 3
    print(sumX(N, M, K)) 
  
# This code is contributed by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to calculate the 
// terms of summing of sum series
  
using System;
class GFG {
  
    static int MOD = 1000000007;
  
    // Function to calculate
    // twice of sum of first N natural numbers
    static long sum(long N)
    {
        long val = N * (N + 1);
  
        // taking modulo 10 ^ 9 + 7
        val = val % MOD;
  
        return val;
    }
  
    // Function to calculate the
    // terms of summing of sum series
    static int sumX(int N, int M, int K)
    {
        for (int i = 0; i < M; i++) {
            N = (int)sum(K + N);
        }
        N = N % MOD;
        return N;
    }
  
    // Driver code
    public static void Main()
    {
        int N = 1, M = 2, K = 3;
        Console.WriteLine(sumX(N, M, K));
    }
}
  
// This code is contributed by anuj_67..

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to calculate the 
// terms of summing of sum series
  
// Function to calculate twice of 
// sum of first N natural numbers
function sum($N)
{
    $MOD = 1000000007;
    $val = $N * ($N + 1);
    $val = $val % $MOD;
      
    return $val;
}
  
// Function to calculate the terms 
// of summing of sum series
function sumX($N, $M, $K)
{
    $MOD = 1000000007;
    for ($i = 0; $i < $M; $i++) 
    
        $N = sum($K + $N); 
    
      
    $N = $N % $MOD
    return $N;
}
  
// Driver Code
$N = 1;
$M = 2;
$K = 3;
echo (sumX($N, $M, $K)); 
      
// This code is contributed 
// by Shivi_Aggarwal
?>

chevron_right


Output:

552


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.