Python Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!

In this article, we will learn how to calculate the sum of the mathematical series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!, where each term is the ratio of a number to its factorial.

Problem statement − Given an integer input n, we need to find the sum of the series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!

Understanding the Series

This series has an interesting mathematical property − as n approaches infinity, the sum converges to the mathematical constant e (approximately 2.718). Each term follows the pattern i/i! where i ranges from 1 to n.

Approach

We calculate the factorial incrementally within the same loop to achieve O(n) time complexity. Instead of computing each factorial from scratch, we multiply the previous factorial by the current number.

Implementation

def sumOfSeries(num):
    result = 0
    factorial = 1
    
    for i in range(1, num + 1):
        factorial *= i  # Calculate factorial incrementally
        result += i / factorial  # Add current term to sum
    
    return result

# Test with different values
n = 10
print(f"Sum for n={n}: {sumOfSeries(n)}")

n = 100
print(f"Sum for n={n}: {sumOfSeries(n)}")
Sum for n=10: 2.7182815255731922
Sum for n=100: 2.7182818284590455

Step-by-Step Calculation

Let's trace through the first few terms to understand how the calculation works ?

def sumOfSeriesDetailed(num):
    result = 0
    factorial = 1
    
    print("Term\tValue\t\tRunning Sum")
    print("-" * 35)
    
    for i in range(1, min(num + 1, 6)):  # Show first 5 terms
        factorial *= i
        term_value = i / factorial
        result += term_value
        print(f"{i}/{i}!\t{term_value:.6f}\t{result:.6f}")
    
    # Calculate remaining terms without printing
    for i in range(6, num + 1):
        factorial *= i
        result += i / factorial
    
    return result

print("Detailed calculation for first 5 terms:")
final_sum = sumOfSeriesDetailed(10)
print(f"\nFinal sum for n=10: {final_sum}")
Detailed calculation for first 5 terms:
Term	Value		Running Sum
-----------------------------------
1/1!	1.000000	1.000000
2/2!	1.000000	2.000000
3/3!	0.500000	2.500000
4/4!	0.166667	2.666667
5/5!	0.041667	2.708333

Final sum for n=10: 2.7182815255731922

Alternative Approach Using math.factorial

While less efficient, you can also use Python's built-in factorial function ?

import math

def sumOfSeriesBuiltIn(num):
    result = 0
    for i in range(1, num + 1):
        result += i / math.factorial(i)
    return result

n = 10
print(f"Using built-in factorial: {sumOfSeriesBuiltIn(n)}")
Using built-in factorial: 2.7182815255731922

Convergence to e

As we increase n, the sum approaches the mathematical constant e ?

import math

def demonstrateConvergence():
    print("n\tSum\t\tDifference from e")
    print("-" * 35)
    
    for n in [5, 10, 20, 50, 100]:
        series_sum = sumOfSeries(n)
        diff = abs(math.e - series_sum)
        print(f"{n}\t{series_sum:.6f}\t{diff:.8f}")

demonstrateConvergence()
n	Sum		Difference from e
-----------------------------------
5	2.708333	0.00994849
10	2.718282	0.00000043
20	2.718282	0.00000000
50	2.718282	0.00000000
100	2.718282	0.00000000

Conclusion

This series efficiently calculates an approximation of the mathematical constant e. By computing factorials incrementally, we achieve O(n) time complexity and observe rapid convergence to e as n increases.

Updated on: 2026-03-25T06:26:32+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements