This Python program reads two values from the user:
- x: the base input 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{x^k}{f(k)} \cdot s(k)$$
Where:
- f(k): the factorial used in the denominator (e.g. 2!, 4!, 8!, etc.)
- s(k): the sign of the term (positive or negative)
- The pattern of powers, factorials, and signs is manually defined in the code
import math
def custom_series(x, n):
total = 0
factorials = [1, 2, 4, 2, 8] # Extendable pattern
signs = [1, 1, 1, -1, 1] # Alternating signs
for k in range(n):
power = k + 1
fact = factorials[k] if k < len(factorials) else 1
sign = signs[k] if k < len(signs) else 1
term = sign * (x ** power) / math.factorial(fact)
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}")
Enter value for x: 2
Enter number of terms: 5
Series sum: 4.933333
- The user inputs x and n
- Each term is calculated using a specific power, factorial, and sign
- The term is added to the running total
- The final result is rounded to 6 decimal places and displayed