Problem: Write a Python program to find the sum of the series [1/1! + 1/2! + 1/3! +-+ 1/n!].


Input:  10
Output: 2.7182815255731922

Input:  50
Output: 2.7182818284590455

We can easily find the sum of this series using loop, list comprehension, and function in Python.

Using the for loop and list comprehension, we store the value of each term (i.e. 1/i!) into a list and add them all using the inbuilt sum() method in Python.

#function to find the factorial of a number
def factorial(n):
    for x in range(2, n+1):
        f = f*x
    return f
#input the value of n
n = int(input("Enter value of n: "))

#find the sum of the series
sum = sum([i/factorial(i) for i in range(1, n+1)])

#output the sum


Enter value of n: 50

To find the factorial of every denominator of the series, we use the factorial() method which we have defined at the starting of the program.

We can significantly improve the efficiency of the program by running loops in the reverse direction and using dynamic programming to find the factorial of the number.

Leave a Reply

1 × 5 =