Monday, January 13, 2025
Google search engine
HomeData Modelling & AISum of series formed by difference between product and sum of N...

Sum of series formed by difference between product and sum of N natural numbers

Given a natural number N, the task is to find the sum of the series up to Nth term where the ith term denotes the difference between the product of first i natural numbers and sum of first i natural numbers, i.e.,

{ 1 – 1 } + { (1 * 2) – (2 + 1) } + { (1 * 2 * 3) – (3 + 2 + 1) } + { (1 * 2 * 3 * 4) – (4 + 3 + 2 + 1) } + ……… 

Examples:  

Input:
Output: -1 
Explanation: { 1 – 1 } + { (1 * 2) – (2 + 1) } = { 0 } + { -1 } = -1

Input:
Output: 13 
Explanation: 
{ 0 } + { (1 * 2) – (2 + 1) } + { (1 * 2 * 3) – (3 + 2 + 1) } + { (1 * 2 * 3 * 4) – (4 + 3 + 2 + 1) } 
= { 0 } + { -1 } + { 0 } + { 14 } 
= 0 – 1 + 0 + 14 
= 13

Iterative Approach: 
Follow the steps below to solve the problem:  

  • Initialize three variables: 
    • currProd: Stores the product of all natural numbers upto current term.
    • currSum: Stores the sum of all natural numbers upto current term.
    • sum: Stores the sum of the series upto current term
  • Initialize currSum = 1, currSum = 1, and sum = 0 (Since, 1 – 1 = 0) to store their respective values for the first term.
  • Iterate over the range [2, N] and update the following for every ith iteration: 
    • currSum = currSum + i
    • currProd = currProd * i
    • sum = currProd – currSum
  • Return the final value of sum.

Below is the implementation of the above approach:  

C++




// C++ program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to
// calculate the
// sum upto Nth term
int seriesSum(int n)
{
    // Stores the sum
    // of the series
    int sum = 0;
 
    // Stores the
    // product of
    // natural numbers
    // upto the
    // current term
    int currProd = 1;
 
    // Stores the sum
    // of natural numbers
    // upto the
    // upto current term
    int currSum = 1;
 
    // Generate the remaining
    // terms and calculate sum
    for (int i = 2; i <= n; i++) {
        currProd *= i;
        currSum += i;
 
        // Update the sum
        sum += currProd
               - currSum;
    }
 
    // Return the sum
    return sum;
}
 
// Driver Program
int main()
{
    int N = 5;
    cout << seriesSum(N) << " ";
}


Java




// Java program to implement the
// above approach
import java.lang.*;
 
class GFG{
 
// Function to calculate the
// sum upto Nth term
static int seriesSum(int n)
{
     
    // Stores the sum
    // of the series
    int sum = 0;
 
    // Stores the product of
    // natural numbers upto
    // the current term
    int currProd = 1;
 
    // Stores the sum of natural
    // numbers upto the upto
    // current term
    int currSum = 1;
 
    // Generate the remaining
    // terms and calculate sum
    for(int i = 2; i <= n; i++)
    {
       currProd *= i;
       currSum += i;
        
       // Update the sum
       sum += currProd - currSum;
    }
 
    // Return the sum
    return sum;
}
 
// Driver code
public static void main(String[] args)
{
    int N = 5;
     
    System.out.print(seriesSum(N));
}
}
 
// This code is contributed by rock_cool


Python3




# Python3 program to implement
# the above approach
 
# Function to calculate the
# sum upto Nth term
def seriesSum(n):
 
    # Stores the sum
    # of the series
    sum1 = 0;
 
    # Stores the product of
    # natural numbers upto the
    # current term
    currProd = 1;
 
    # Stores the sum of natural numbers
    # upto the upto current term
    currSum = 1;
 
    # Generate the remaining
    # terms and calculate sum
    for i in range(2, n + 1):
        currProd *= i;
        currSum += i;
 
        # Update the sum
        sum1 += currProd - currSum;
     
    # Return the sum
    return sum1;
 
# Driver Code
N = 5;
print(seriesSum(N), end = " ");
 
# This code is contributed by Code_Mech


C#




// C# program to implement the
// above approach
using System;
class GFG{
 
// Function to calculate the
// sum upto Nth term
static int seriesSum(int n)
{
     
    // Stores the sum
    // of the series
    int sum = 0;
 
    // Stores the product of
    // natural numbers upto
    // the current term
    int currProd = 1;
 
    // Stores the sum of natural
    // numbers upto the upto
    // current term
    int currSum = 1;
 
    // Generate the remaining
    // terms and calculate sum
    for(int i = 2; i <= n; i++)
    {
        currProd *= i;
        currSum += i;
             
        // Update the sum
        sum += currProd - currSum;
    }
 
    // Return the sum
    return sum;
}
 
// Driver code
public static void Main()
{
    int N = 5;
     
    Console.Write(seriesSum(N));
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
 
    // Javascript program to implement
    // the above approach
     
    // Function to
    // calculate the
    // sum upto Nth term
    function seriesSum(n)
    {
        // Stores the sum
        // of the series
        let sum = 0;
 
        // Stores the
        // product of
        // natural numbers
        // upto the
        // current term
        let currProd = 1;
 
        // Stores the sum
        // of natural numbers
        // upto the
        // upto current term
        let currSum = 1;
 
        // Generate the remaining
        // terms and calculate sum
        for (let i = 2; i <= n; i++) {
            currProd *= i;
            currSum += i;
 
            // Update the sum
            sum += currProd
                   - currSum;
        }
 
        // Return the sum
        return sum;
    }
 
    let N = 5;
    document.write(seriesSum(N) + " ");
     
</script>


Output: 

118

 

Time Complexity: O(N) 
Auxiliary Space: O(1)

Recursive Approach:  

  • Recursively calculate the sum by calling the function updating K (denotes the current term).
  • Update precomputed prevSum by adding multi * K – add + K
  • Recursively compute for all values of K updating prevSum, multi and add at every iteration.
  • Finally, return the value of prevSum.

Below is the implementation of the above approach:  

C++




// C++ program to calculate the
// sum upto the Nth term of the
// given series
 
#include <bits/stdc++.h>
using namespace std;
 
// Recursive Function to calculate the
// sum upto Nth term
int seriesSumUtil(int k, int n,
                  int prevSum,
                  int multi, int add)
{
    // If N-th term is calculated
    if (k == n + 1) {
        return prevSum;
    }
 
    // Update multi to store
    // product upto K
    multi = multi * k;
 
    // Update add to store
    // sum upto K
    add = add + k;
 
    // Update prevSum to store sum
    // upto K
    prevSum = prevSum + multi - add;
 
    // Proceed to next K
    return seriesSumUtil(k + 1, n, prevSum,
                         multi, add);
}
 
// Function to calculate
// and return the
// Sum upto Nth term
int seriesSum(int n)
{
    if (n == 1)
        return 0;
    int prevSum = 0;
    int multi = 1;
    int add = 1;
 
    // Recursive Function
    return seriesSumUtil(2, n, prevSum,
                         multi, add);
}
 
// Driver Program
int main()
{
    int N = 5;
    cout << seriesSum(N) << " ";
}


Java




// Java program to calculate the
// sum upto the Nth term of the
// given series
class GFG{
     
// Recursive Function to calculate the
// sum upto Nth term
static int seriesSumUtil(int k, int n,
                         int prevSum,
                         int multi, int add)
{
     
    // If N-th term is calculated
    if (k == n + 1)
    {
        return prevSum;
    }
 
    // Update multi to store
    // product upto K
    multi = multi * k;
 
    // Update add to store
    // sum upto K
    add = add + k;
 
    // Update prevSum to store sum
    // upto K
    prevSum = prevSum + multi - add;
 
    // Proceed to next K
    return seriesSumUtil(k + 1, n, prevSum,
                         multi, add);
}
 
// Function to calculate and
// return the Sum upto Nth term
static int seriesSum(int n)
{
    if (n == 1)
        return 0;
         
    int prevSum = 0;
    int multi = 1;
    int add = 1;
 
    // Recursive Function
    return seriesSumUtil(2, n, prevSum,
                         multi, add);
}
 
// Driver code
public static void main(String []args)
{
    int N = 5;
    System.out.println(seriesSum(N));
}
}
 
// This code is contributed by Ritik Bansal


Python3




# Python3 program to calculate the
# sum upto the Nth term of the
# given series
 
# Recursive Function to calculate the
# sum upto Nth term
def seriesSumUtil(k, n, prevSum, multi, add):
   
    # If N-th term is calculated
    if (k == n + 1):
        return prevSum;
 
    # Update multi to store
    # product upto K
    multi = multi * k;
 
    # Update add to store
    # sum upto K
    add = add + k;
 
    # Update prevSum to store sum
    # upto K
    prevSum = prevSum + multi - add;
 
    # Proceed to next K
    return seriesSumUtil(k + 1, n,
                         prevSum, multi, add);
 
# Function to calculate and
# return the Sum upto Nth term
def seriesSum(n):
    if (n == 1):
        return 0;
 
    prevSum = 0;
    multi = 1;
    add = 1;
 
    # Recursive Function
    return seriesSumUtil(2, n, prevSum,
                         multi, add);
 
# Driver code
if __name__ == '__main__':
    N = 5;
    print(seriesSum(N));
 
# This code is contributed by Princi Singh


C#




// C# program to calculate the
// sum upto the Nth term of the
// given series
using System;
class GFG{
     
// Recursive Function to calculate the
// sum upto Nth term
static int seriesSumUtil(int k, int n,
                         int prevSum,
                         int multi, int add)
{
     
    // If N-th term is calculated
    if (k == n + 1)
    {
        return prevSum;
    }
 
    // Update multi to store
    // product upto K
    multi = multi * k;
 
    // Update add to store
    // sum upto K
    add = add + k;
 
    // Update prevSum to store sum
    // upto K
    prevSum = prevSum + multi - add;
 
    // Proceed to next K
    return seriesSumUtil(k + 1, n, prevSum,
                         multi, add);
}
 
// Function to calculate and
// return the Sum upto Nth term
static int seriesSum(int n)
{
    if (n == 1)
        return 0;
         
    int prevSum = 0;
    int multi = 1;
    int add = 1;
 
    // Recursive Function
    return seriesSumUtil(2, n, prevSum,
                         multi, add);
}
 
// Driver code
public static void Main(String []args)
{
    int N = 5;
    Console.WriteLine(seriesSum(N));
}
}
 
// This code is contributed by Rohit_ranjan


Javascript




<script>
 
// Javascript program to calculate the
// sum upto the Nth term of the
// given series
 
// Recursive Function to calculate the
// sum upto Nth term
function seriesSumUtil(k, n, prevSum, multi, add)
{
     
    // If N-th term is calculated
    if (k == n + 1)
    {
        return prevSum;
    }
 
    // Update multi to store
    // product upto K
    multi = multi * k;
 
    // Update add to store
    // sum upto K
    add = add + k;
 
    // Update prevSum to store sum
    // upto K
    prevSum = prevSum + multi - add;
 
    // Proceed to next K
    return seriesSumUtil(k + 1, n, prevSum,
                         multi, add);
}
 
// Function to calculate and
// return the Sum upto Nth term
function seriesSum(n)
{
    if (n == 1)
        return 0;
 
    let prevSum = 0;
    let multi = 1;
    let add = 1;
 
    // Recursive Function
    return seriesSumUtil(2, n, prevSum,
                         multi, add);
}
 
// Driver code
let N = 5;
document.write(seriesSum(N));
 
// This code is contributed by rameshtravel07
 
</script>


Output: 

118

 

Time Complexity: O(N) 
Auxiliary Space: O(N), for recursive stack space.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments