Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!
# Python3 program to compute sum of series
# 1/1! + 1/2! + .. + 1/n!
# Function to find factorial 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 = 5
sum(n)
# This code is contributed by ujjwal matoiya
Output:
1.71667