# 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 ` `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`

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 ` `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`

Output:

```118

```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.