Find sum of factorials till N factorial (1! + 2! + 3! + … + N!)
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 = 5
Output: 153
Explanation: 1! + 2! + 3! + 4! + 5! = 1 + 2 + 6 + 24 + 120 = 153.Input: N = 1
Output: 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++
// 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 Code int main() { int N = 5; // Function call cout << findFactSum(N); return 0; } |
Java
// Java code to implement above approach 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(String[] args) { int N = 5 ; System.out.print(findFactSum(N)); } } // This code is contributed ukasp. |
Python3
# 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 Code if __name__ = = "__main__" : N = 5 # Function call print (findFactSum(N)) # This code is contributed by rakeshsahni |
C#
// C# code to implement above approach using 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. |
Javascript
<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> |
Output:
153
Time Complexity: O(N)
Auxiliary Space: O(1), since no extra space has been taken.
Please Login to comment...