بخشی از مجموعه

Several example codes

~2 دقیقه مطالعه • بروزرسانی ۱۹ مهر ۱۴۰۴

Program Overview

This Python program reads two values from the user: - x: the base value - n: the number of terms to compute
It then calculates the sum of the first n terms of the following custom series:
$$\sum_{k=1}^{n} \frac{T_k}{k!}$$ Where:
- Term 1: $$T_1 = x + \frac{x}{2}$$ - Term 2: $$T_2 = x^3 - \frac{x}{4}$$ - Term 3: $$T_3 = x^5 + \frac{x}{8}$$ The pattern alternates between addition and subtraction of fractional components, combined with increasing powers of x.


Python Code:


import math

def custom_series(x, n):
    total = 0
    for k in range(1, n + 1):
        if k == 1:
            term = (x + x / 2) / math.factorial(k)
        elif k == 2:
            term = (x**3 - x / 4) / math.factorial(k)
        elif k == 3:
            term = (x**5 + x / 8) / math.factorial(k)
        else:
            # Extend pattern here if defined
            term = 0
        total += term
    return round(total, 6)

# Run the program
x = float(input("Enter value for x: "))
n = int(input("Enter number of terms: "))
result = custom_series(x, n)
print(f"Series sum: {result}")

Sample Output:


Enter value for x: 2  
Enter number of terms: 3  

Series sum: 8.933333

Step-by-Step Explanation:

- The user inputs x and n
- Each term is computed based on its specific formula and divided by k!
- The sum accumulates in total
- The final result is rounded to 6 decimal places and displayed



نوشته و پژوهش شده توسط دکتر شاهین صیامی