# Sum of series 1*1! + 2*2! + ……..+ n*n!

Given n, we need to find sum 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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple 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.
∑(i * i!) Where i varies from 1 to n
= ∑((i + 1 – 1) * i!)
= ∑((i+1) * i!) – ∑i!
= ∑(i + 1)! – ∑(i!)

∑(i + 1)! = 2! + 3! + … (n+1)! where 1 <= i <= n —–(1)
∑(i!) = 1! + 2! + 3! + … (n)! where 1 <= i <= n —–(2)

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

## C++

 `// CPP program to find sum of the series. ` `#include ` `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; ` `} `

## Java

 `// Java program to find sum of  ` `// the series. ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``static` `int` `factorial(``int` `n) ` `    ``{ ` `        ``int` `res = ``1``; ` `        ``for` `(``int` `i = ``2``; i <= n; i++) ` `            ``res = res * i; ` `        ``return` `res; ` `    ``} ` `     `  `    ``// Function to calculate required ` `    ``// series ` `    ``static` `int` `calculateSeries(``int` `n) ` `    ``{ ` `        ``return` `factorial(n + ``1``) - ``1``; ` `    ``} ` `     `  `    ``// Drivers code ` `    ``public` `static` `void` `main (String[] args) ` `    ``{ ` `        ``int` `n = ``3``; ` `        ``System.out.println(  ` `                       ``calculateSeries(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by anuj_67. `

## Python3

 `# Python program to find sum of  ` `# the series. ` ` `  `def` `factorial(n): ` `    ``res ``=` `1` `    ``for` `i ``in` `range``(``2``, n``+``1``): ` `        ``res ``=` `res ``*` `i ` `    ``return` `res ` ` `  `# Function to calculate required ` `# series ` `def` `calculateSeries(n): ` `    ``return` `factorial(n ``+` `1``) ``-` `1` ` `  `# Drivers code ` `n ``=` `3` `print``(calculateSeries(n)) ` ` `  `# This code is contributed by  ` `# Ansu Kumari. `

## C#

 `// C# program to find  ` `// sum of the series. ` `using` `System; ` ` `  `class` `GFG ` `{ ` `    ``static` `int` `factorial(``int` `n) ` `    ``{ ` `        ``int` `res = 1; ` `        ``for` `(``int` `i = 2; i <= n; i++) ` `            ``res = res * i; ` `        ``return` `res; ` `    ``} ` `     `  `    ``// Function to calculate  ` `    ``// required series ` `    ``static` `int` `calculateSeries(``int` `n) ` `    ``{ ` `        ``return` `factorial(n + 1) - 1; ` `    ``} ` `     `  `    ``// Driver code ` `    ``static` `public` `void` `Main () ` `    ``{ ` `        ``int` `n = 3; ` `        ``Console.WriteLine(  ` `                ``calculateSeries(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by ajit. `

## PHP

 ` `

Output:

```23
```

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.

Improved By : vt_m, jit_t, triabhishek95

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.