Part of the series

Several example codes

~1 min read • Updated Oct 12, 2025

Program Overview

This Python program reads two values x and n from the user and computes the sum of the following series:
Γ(x, n) = x¹/1! + x²/2! + x³/3! + ... + xⁿ/n!
This is a truncated exponential series used to approximate e^x, excluding the zeroth term.


Python Code:


def factorial(k: int) -> int:
    result = 1
    for i in range(2, k + 1):
        result *= i
    return result

def gamma_series(x: float, n: int) -> float:
    total = 0.0
    for k in range(1, n + 1):
        total += (x ** k) / factorial(k)
    return total

# Read inputs from user
x = float(input("Enter value for x: "))
n = int(input("Enter number of terms n: "))

result = gamma_series(x, n)
print(f"Γ(x, n) = {result:.6f}")

Sample Output (input: x = 2, n = 4):


Γ(x, n) = 5.000000

Written & researched by Dr. Shahin Siami