Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!

15 Jun, 2022
Given a positive integer n, write a function to compute the sum of the series 1/1! + 1/2! + .. + 1/n!A Simple Solution is to initialize the sum as 0, then run a loop and call the factorial function inside the loop. Following is the implementation of a simple solution.  
C++
Java
Python3
C#
PHP
Javascript
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n!#include <iostream>using namespace std; //  Utility function to findint factorial(int n){    int res = 1;    for (int i=2; i<=n; i++)       res *= i;    return res;} // A Simple Function to return value of 1/1! + 1/2! + .. + 1/n!double sum(int n){    double sum = 0;    for (int i = 1; i <= n; i++)        sum += 1.0/factorial(i);    return sum;} // Driver program to test above functionsint main(){    int n = 5;    cout << sum(n);    return 0;}
// A simple Java program to compute// sum of series 1/1! + 1/2! + .. + 1/n!import java.io.*; class GFG {         // Utility function to find    static int factorial(int n)    {        int res = 1;        for (int i = 2; i <= n; i++)        res *= i;        return res;    }         // A Simple Function to return value    // of 1/1! + 1/2! + .. + 1/n!    static double sum(int n)    {        double sum = 0;        for (int i = 1; i <= n; i++)            sum += 1.0/factorial(i);        return sum;    }     // Driver program    public static void main (String[] args)    {        int n = 5;        System.out.println(sum(n));    }} // This code is contributed by Ajit.
# Python3 program to compute sum of series# 1/1! + 1/2! + .. + 1/n! # Function to find factorial of a numberdef factorial(n):    res = 1    for i in range(2, n + 1):            res *= i    return res         # A Simple Function to return value# of 1/1! + 1/2! + .. + 1/n!def sum(n):    s = 0.0         for i in range(1, n + 1):        s += 1.0 / factorial(i)    print(s) # Driver program to test above functionsn = 5sum(n) # This code is contributed by Danish Raza
// A simple C# program to compute sum// of series 1/1! + 1/2! + .. + 1/n!using System; class GFG {         // Utility function to find    static int factorial(int n)    {        int res = 1;        for (int i = 2; i <= n; i++)            res *= i;                     return res;    }         // A Simple Function to return value    // of 1/1! + 1/2! + .. + 1/n!    static double sum(int n)    {        double sum = 0;        for (int i = 1; i <= n; i++)            sum += 1.0/factorial(i);                     return sum;    }     // Driver program    public static void Main ()    {        int n = 5;                 Console.WriteLine(sum(n));    }} // This code is contributed by Sam007.
<?php// A simple PHP program to compute// sum of series 1/1! + 1/2! + .. + 1/n! // Utility function to findfunction factorial($n){    $res = 1;    for ($i = 2; $i <= $n; $i++)        $res *= $i;    return $res;} // A Simple Function to return// value of 1/1! + 1/2! + .. + 1/n!function sum($n){    $sum = 0;    for ($i = 1; $i <= $n; $i++)        $sum += 1.0 / factorial($i);    return $sum;} // Driver Code$n = 5;echo(sum($n)); // This code is contributed by Ajit.?>
<script> //Javascript program to compute// sum of series 1/1! + 1/2! + .. + 1/n! // Utility function to findfunction factorial(n){    let res = 1;    for (let i = 2;  i <= n; i++)        res *= i;    return res;} // A Simple Function to return// value of 1/1! + 1/2! + .. + 1/n!function sum(n){    let sum = 0;    for (let i = 1; i <= n; i++)        sum += 1.0 / factorial(i);    return sum;} // Driver Code let n = 5;document.write(sum(n).toFixed(5)); // This code is contributed by sravan kumar </script>
Output: 
1.71667
Time complexity: O(n * n)
Auxiliary Space: O(1)An Efficient Solution can find the sum in O(n) time. The idea is to calculate factorial in the same loop as the sum. Following is the implementation of this idea.  
C++
Java
Python3
C#
PHP
Javascript
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n!#include <iostream>using namespace std; // An Efficient Function to return value of 1/1! + 1/2! + .. + 1/n!double sum(int n){    double sum = 0;    int fact = 1;    for (int i = 1; i <= n; i++)    {       fact *= i;         // Update factorial       sum += 1.0/fact;   // Update series sum    }    return sum;} // Driver program to test above functionsint main(){    int n = 5;    cout << sum(n);    return 0;}
// A simple Java program to compute// sum of series 1/1! + 1/2! + .. + 1/n!import java.io.*; class GFG {         // An Efficient Function to return    // value of 1/1! + 1/2! + .. + 1/n!    static double sum(int n)    {        double sum = 0;        int fact = 1;        for (int i = 1; i <= n; i++)        {            // Update factorial            fact *= i;                         // Update series sum            sum += 1.0/fact;        }        return sum;    }     // Driver program    public static void main (String[] args)    {        int n = 5;        System.out.println(sum(n));    }} // This code is contributed by Ajit.
# Python3 program to compute sum of series# 1/1! + 1/2! + .. + 1/n! # Function to return value of# 1/1! + 1/2! + .. + 1/n!def sum(n):    sum = 0    fact = 1     for i in range(1, n + 1):         # Update factorial        fact *= i         # Update series sum        sum += 1.0/fact     print(sum) # Driver program to test above functionsn = 5sum(n) # This code is contributed by Danish Raza
// A simple C# program to compute sum// of series 1/1! + 1/2! + .. + 1/n!using System; class GFG {         // An Efficient Function to return    // value of 1/1! + 1/2! + .. + 1/n!    static double sum(int n)    {        double sum = 0;        int fact = 1;                 for (int i = 1; i <= n; i++)        {                         // Update factorial            fact *= i;                         // Update series sum            sum += 1.0 / fact;        }        return sum;    }     // Driver program    public static void Main ()    {        int n = 5;                 Console.WriteLine(sum(n));    }} // This code is contributed by Sam007.
<?php// A simple PHP program to// compute sum of series// 1/1! + 1/2! + .. + 1/n! // An Efficient Function to// return value of 1/1! +// 1/2! + .. + 1/n!function sum($n){    $sum = 0;    $fact = 1;    for ($i = 1; $i <= $n; $i++)    {        // Update factorial        $fact *= $i;                    // Update series sum        $sum += 1.0 / $fact;    }    return $sum;} // Driver Code$n = 5;echo sum($n); // This code is contributed by vt_m.?>
<script>     // A simple Javascript program to compute sum    // of series 1/1! + 1/2! + .. + 1/n!         // An Efficient Function to return    // value of 1/1! + 1/2! + .. + 1/n!    function sum(n)    {        let sum = 0;        let fact = 1;                  for (let i = 1; i <= n; i++)        {                          // Update factorial            fact *= i;                          // Update series sum            sum += 1.0 / fact;        }        return sum.toFixed(5);    }         let n = 5;                document.write(sum(n));     </script>
Output: 
1.71667
Time complexity: O(n) since using a single loop
Auxiliary Space: O(1)This article is contributed by Rahul Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
jit_t
vt_m
sravankumar8128
divyeshrabadiya07
surindertarika1234
polymatir3j
factorial
Mathematical
Mathematical
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operators in C / C++
Find minimum number of coins that make a given value
Minimum number of jumps to reach end
Algorithm to solve Rubik's Cube
The Knight's tour problem | Backtracking-1
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number
Program to print prime numbers from 1 to N.
Program to find sum of elements in a given array