How to Calculate (1 + 1/1! + 1/2! + …… 1/n!) in Python


The example will teach us how to calculate the sum of a series expressing 1 + 1/1! + 1/2! + …. 1/n! with the given n in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import math

n = int(input("Input the term: "))
sum1 = 0
while n >= 0:
    sum1 = sum1 + (1 / math.factorial(n))
    n = n - 1
print("The sum is: ", round(sum1, 4))

Output:

Input the term: 6
The sum is: 2.7181

Input the term: 25
The sum is: 2.7183
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement