Given n, we need to find of 1*1! + 2*2! + ……..+ n*n!

Examples:

Input: 1
Output: 1

Input: 3
Output: 23
1 * 1! + 2 * 2! + 3 * 3! = 1 + 4 + 18 = 23

We may assume that overflow does not happen.

A solution is to compute terms one by one and add to result.

An efficient solution is based on direct formula (n + 1)! – 1

How does this formula work?

We basically need to compute below sum.
&Sum;(i * i!) Where i varies from 1 to n
= &Sum;((i + 1 – 1) * i!)
= &Sum;(i + 1)! – &Sum;(i!)

&Sum;(i + 1)! = 2! + 3! + … (n+1)! where 1 <= i <= n —–(1)
&Sum;(i!) = 1! + 2! + 3! + … (n)! where 1 <= i <= n —–(2)

Subtracting second from first, we get (n+1)! – 1

```// CPP program to find sum of the series.
#include <bits/stdc++.h>
using namespace std;

int factorial(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}

// Function to calculate required series
int calculateSeries(int n)
{
return factorial(n + 1) - 1;
}

// Drivers code
int main()
{
int n = 3;
cout << calculateSeries(n);
return 0;
}
```

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 write to us at contribute@geeksforgeeks.org to report any issue with the above content.