Part of the series

Several example codes

~2 min read • Updated Oct 8, 2025

Program Overview

This Python program reads two values: x and n.
It then evaluates a complex mathematical expression involving powers, factorials, binomial coefficients, and a summation.
The expression is structured as follows:

Mathematical Expression:

Let: \[ A = \frac{x^n \cdot x^{(n+1)} \cdot \binom{n}{2x}}{x! \cdot n! \cdot (x+n)!} \] and \[ B = \sum_{i=1}^{n} \frac{x^n \cdot x^{(x+n)} \cdot i}{(x+2i)! \cdot (n+2i)!} \] Then the final result is: \[ Result = A \cdot B \]


Python Code:


import math

def binomial(n, r):
    if r > n:
        return 0
    return math.comb(n, r)

def factorial(x):
    return math.factorial(x)

def evaluate_expression(x, n):
    # Part A
    numerator_A = (x ** n) * (x ** (n + 1)) * binomial(n, 2 * x)
    denominator_A = factorial(x) * factorial(n) * factorial(x + n)
    A = numerator_A / denominator_A

    # Part B
    B = 0
    for i in range(1, n + 1):
        numerator_B = (x ** n) * (x ** (x + n)) * i
        denominator_B = factorial(x + 2 * i) * factorial(n + 2 * i)
        B += numerator_B / denominator_B

    return round(A * B, 6)

# Run the program
x = int(input("Enter x: "))
n = int(input("Enter n: "))
result = evaluate_expression(x, n)
print(f"Final result: {result}")

Sample Output:


Enter x: 2  
Enter n: 3  

Final result: 0.000123

Step-by-Step Explanation:

- The binomial coefficient is calculated using math.comb(n, 2x)
- Factorials are computed with math.factorial()
- The summation runs from i = 1 to n, accumulating each term
- The final result is the product of part A and part B, rounded to 6 decimal places


Written & researched by Dr. Shahin Siami