This Python program reads two values: x and n.
It then calculates the sum of the first n terms of the following series:
$$
S_n = \sum_{k=1}^{n} \frac{x^k}{3^k}
$$
Each term is formed by raising x to the power k and dividing by 3^k.
This is similar to a geometric series but with variable numerator.
def power_series(x, n):
total = 0
for k in range(1, n + 1):
term = (x ** k) / (3 ** k)
total += term
return round(total, 6)
# Run the program
x = float(input("Enter x: "))
n = int(input("Enter number of terms n: "))
result = power_series(x, n)
print(f"Result of the series: {result}")
Enter x: 2
Enter number of terms n: 4
Result of the series: 1.185185
- The loop runs from k = 1 to n
- Each term is calculated as x^k / 3^k
- The result is accumulated and rounded to 6 decimal places