Find sum of factorials till N factorial (1! + 2! + 3! + ... + N!)

13 Jul, 2022
Given a positive integer N. The task is to compute the sum of factorial from 1! to N!, 1! + 2! + 3! + ... + N!.
Examples:
Input: N = 5Output: 153Explanation: 1! + 2! + 3! + 4! + 5! = 1 + 2 + 6 + 24 + 120 = 153.
Input: N = 1Output: 1
Naive Approach: The basic way to solve this problem is to find the factorial of all numbers till 1 to N and calculate their sum.Time Complexity: O(N^2) Auxiliary Space: O(1)  
Approach: An efficient approach is to calculate factorial and sum in the same loop making the time O(N). Traverse the numbers from 1 to N and for each number i:  
Multiply i with previous factorial (initially 1).
Add this new factorial to a collective sum
At the end, print this collective sum.
 
Below is the implementation of the above approach.
 
C++
Java
Python3
C#
Javascript
// C++ program to compute sum of series// 1! + 2! + 3! + ... + N!#include <iostream>using namespace std; // Function to return sum// of 1!, 2! upto N!int findFactSum(int N){    // Initializing the variables    int f = 1, Sum = 0;     // Calculate the factorial and sum    // in the same loop    for (int i = 1; i <= N; i++) {         f = f * i;        Sum += f;    }     // Return Sum as the final result.    return Sum;} // Driver Codeint main(){    int N = 5;     // Function call    cout << findFactSum(N);     return 0;}
// Java code to implement above approachclass GFG {     // Function to return sum    // of 1!, 2! upto N!    static int findFactSum(int N)    {         // Initializing the variables        int f = 1, Sum = 0;         // Calculate the factorial and sum        // in the same loop        for (int i = 1; i <= N; i++) {             f = f * i;            Sum += f;        }         // Return Sum as the final result.        return Sum;    }     // Driver code    public static void main(String[] args)    {        int N = 5;        System.out.print(findFactSum(N));    }} // This code is contributed ukasp.
# python program to compute sum of series# 1! + 2! + 3! + ... + N! # Function to return sum# of 1!, 2! upto N!def findFactSum(N):     # Initializing the variables    f = 1    Sum = 0     # Calculate the factorial and sum    # in the same loop    for i in range(1, N + 1):        f = f * i        Sum += f     # Return Sum as the final result.    return Sum # Driver Codeif __name__ == "__main__":    N = 5     # Function call    print(findFactSum(N))     # This code is contributed by rakeshsahni
// C# code to implement above approachusing System;class GFG{   // Function to return sum  // of 1!, 2! upto N!  static int findFactSum(int N)  {     // Initializing the variables    int f = 1, Sum = 0;     // Calculate the factorial and sum    // in the same loop    for (int i = 1; i <= N; i++) {       f = f * i;      Sum += f;    }     // Return Sum as the final result.    return Sum;  }   // Driver code  public static void Main()  {    int N = 5;    Console.Write(findFactSum(N));   }} // This code is contributed by Samim Hossain Mondal.
<script>    // JavaScript code for the above approach     // Function to return sum    // of 1!, 2! upto N!    function findFactSum(N)    {           // Initializing the variables      let f = 1, Sum = 0;       // Calculate the factorial and sum      // in the same loop      for (let i = 1; i <= N; i++) {         f = f * i;        Sum += f;      }       // Return Sum as the final result.      return Sum;    }     // Driver Code    let N = 5;     // Function call    document.write(findFactSum(N));   // This code is contributed by Potta Lokesh  </script>
  
153
 
 
Time Complexity: O(N) Auxiliary Space: O(1), since no extra space has been taken.
 
lokeshpotta20
samim2000
rakeshsahni
ukasp
Algo-Geek 2021
factorial
series
series-sum
Algo Geek
Mathematical
Pattern Searching
Mathematical
series
Pattern Searching
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.