Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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.

C++




#include<bits/stdc++.h>
using namespace std;
 
// Function to find the factorial of a number
int findFact(int num) {
    // Base case: 0! and 1! are both 1
    if(num == 0 || num == 1)
        return 1;
 
    // Initialize result to 1 and multiply by each number from 2 to num
    long long result = 1;
    for(int i = 2; i <= num; i++) {
        result *= i;
    }
 
    // Return the factorial result
    return result;
}
 
// Function to find the sum of factorials from 1! to N!
int findFactSum(int N) {
    // Initialize sum to 0
    int sum = 0;
 
    // Iterate from 1 to N and add the factorial of each number to the sum
    for(int i = 1; i <= N; i++) {
        sum += findFact(i);
    }
 
    // Return the sum of factorials
    return sum;
}
 
int main() {
    // Example usage with N = 5
    int N = 5;
     
    // Display the sum of factorials from 1! to N!
    cout << findFactSum(N) << endl;
 
    return 0;
}


Java




public class Main {
    // Function to find the factorial of a number
    public static long findFact(int num) {
        // Base case: 0! and 1! are both 1
        if (num == 0 || num == 1)
            return 1;
 
        // Initialize result to 1 and multiply by each number from 2 to num
        long result = 1;
        for (int i = 2; i <= num; i++) {
            result *= i;
        }
 
        // Return the factorial result
        return result;
    }
 
    // Function to find the sum of factorials from 1! to N!
    public static long findFactSum(int N) {
        // Initialize sum to 0
        long sum = 0;
 
        // Iterate from 1 to N and add the factorial of each number to the sum
        for (int i = 1; i <= N; i++) {
            sum += findFact(i);
        }
 
        // Return the sum of factorials
        return sum;
    }
 
    public static void main(String[] args) {
        // Example usage with N = 5
        int N = 5;
 
        // Display the sum of factorials from 1! to N!
        System.out.println(findFactSum(N));
    }
}
//This code is contributed  by Monu.


C#




using System;
 
class Program
{
    // Function to find the factorial of a number
    static long FindFact(int num)
    {
        // Base case: 0! and 1! are both 1
        if (num == 0 || num == 1)
            return 1;
 
        // Initialize result to 1 and multiply by each number from 2 to num
        long result = 1;
        for (int i = 2; i <= num; i++)
        {
            result *= i;
        }
 
        // Return the factorial result
        return result;
    }
 
    // Function to find the sum of factorials from 1! to N!
    static long FindFactSum(int N)
    {
        // Initialize sum to 0
        long sum = 0;
 
        // Iterate from 1 to N and add the factorial of each number to the sum
        for (int i = 1; i <= N; i++)
        {
            sum += FindFact(i);
        }
 
        // Return the sum of factorials
        return sum;
    }
 
    static void Main(string[] args)
    {
        // Example usage with N = 5
        int N = 5;
 
        // Display the sum of factorials from 1! to N!
        Console.WriteLine(FindFactSum(N));
    }
}


Javascript




// Function to find the factorial of a number
function findFact(num) {
    // Base case: 0! and 1! are both 1
    if (num === 0 || num === 1)
        return 1;
 
    // Initialize result to 1 and multiply by each number from 2 to num
    let result = 1;
    for (let i = 2; i <= num; i++) {
        result *= i;
    }
 
    // Return the factorial result
    return result;
}
 
// Function to find the sum of factorials from 1! to N!
function findFactSum(N) {
    // Initialize sum to 0
    let sum = 0;
 
    // Iterate from 1 to N and add the factorial of each number to the sum
    for (let i = 1; i <= N; i++) {
        sum += findFact(i);
    }
 
    // Return the sum of factorials
    return sum;
}
 
// Main function to test above function
// Example usage with N = 5
let N = 5;
 
// Display the sum of factorials from 1! to N!
console.log(findFactSum(N));
 
// This Code is contributed by Yash Agarwal(yashagarwal2852002)


Python3




# Python code
 
# Function to find the factorial of a number
def find_fact(num):
    # Base case: 0! and 1! are both 1
    if num == 0 or num == 1:
        return 1
 
    # Initialize result to 1 and multiply by each number from 2 to num
    result = 1
    for i in range(2, num + 1):
        result *= i
 
    # Return the factorial result
    return result
 
# Function to find the sum of factorials from 1! to N!
def find_fact_sum(N):
    # Initialize sum to 0
    sum_result = 0
 
    # Iterate from 1 to N and add the factorial of each number to the sum
    for i in range(1, N + 1):
        sum_result += find_fact(i)
 
    # Return the sum of factorials
    return sum_result
 
# Example usage with N = 5
N = 5
 
# Display the sum of factorials from 1! to N!
print(find_fact_sum(N))
 
# This code is contributed by shivamgupta310570


Output

153

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.


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>


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


Output

153

Time Complexity: O(N) 
Auxiliary Space: O(1), since no extra space has been taken. 



Last Updated : 29 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads