Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com
Posts

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

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

 

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

You may like these posts

Post a Comment

© 2025Python . The Best Codder All rights reserved. Distributed by