Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!
Given a positive integer n, write a function to compute sum of the series 1/1! + 1/2! + .. + 1/n!
A Simple Solution solution is to initialize sum as 0, then run a loop and call factorial function inside the loop.
Following is the implementation of simple solution.
C++
// A simple C++ program to compute sum of series 1/1! + 1/2! + .. + 1/n! #include <iostream> using namespace std; // Utility function to find 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! 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 functions int main() { int n = 5; cout << sum(n); return 0; } |
chevron_right
filter_none
Java
// 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. |
chevron_right
filter_none
Python3
# Python3 program to compute sum of series # 1/1! + 1/2! + .. + 1/n! # Function to find factorail of a number def 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 functions n = 5sum(n) # This code is contributed by Danish Raza |
chevron_right
filter_none
C#
// 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. |
chevron_right
filter_none
PHP
<?php // A simple PHP program to compute // sum of series 1/1! + 1/2! + .. + 1/n! // Utility function to find function 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. ?> |
chevron_right
filter_none
Output:
1.71667
The time complexity of above solution is O(n * n!) which is huge.
An Efficient Solution can find the sum in O(n) time. The idea is to calculate factorial in same loop as sum. Following is the implementation of this idea.
C++
// 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 functions int main() { int n = 5; cout << sum(n); return 0; } |
chevron_right
filter_none
Java
// 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. |
chevron_right
filter_none
Python3
# 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 functions n = 5sum(n) # This code is contributed by Danish Raza |
chevron_right
filter_none
C#
// 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. |
chevron_right
filter_none
PHP
<?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. ?> |
chevron_right
filter_none
Output:
1.71667
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.
Recommended Posts:
- Efficient program to calculate e^x
- Program to compute division upto n decimal places
- Efficient program to print the number of factors of n numbers
- Write an Efficient C Program to Reverse Bits of a Number
- Efficient program to print all prime factors of a given number
- Program for sum of cos(x) series
- Program to print the Sum of series -1 + 2 + 11 + 26 + 47 +.....
- Program for sum of geometric series
- Program to find Sum of a Series a^1/1! + a^2/2! + a^3/3! + a^4/4! +…….+ a^n/n!
- Program for sum of arithmetic series
- Program to find the sum of a Series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + ... + (n*n)
- Program to find sum of series 1*2*3 + 2*3*4+ 3*4*5 + . . . + n*(n+1)*(n+2)
- Program to find the sum of the series (1/a + 2/a^2 + 3/a^3 + ... + n/a^n)
- Program to find the sum of a Series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n
- Program to find sum of series 1 + 1/2 + 1/3 + 1/4 + .. + 1/n



