How to Calculate (1 + x^2/2 + x^3/3 + … x^n/n) in Python


The example will help us learn how to calculate the sum of a series (1 + X^2/2 + X^3/3 + … X^n/n) with the given terms and value of x in Python.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

n = int(input("Input the number of terms: "))
x = int(input("Input the number of x: "))
sum1 = 1
while n > 1:
    sum1 = sum1 + (x ** n / n)
    n = n - 1
print("The sum is: ", round(sum1, 2))

Output:

Input the number of terms: 4
Input the number of x: 1
The sum is: 2.08

Input the number of terms: 10
Input the number of x: 2
The sum is: 236.31
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments