Program to Find the Sum of Series 1+X+X^22...+X^NN

Python Program to Find the Sum of Series 1+X+X^2/2…+X^N/N

In the previous article, we have discussed Python Program to Find the Sum of Series 1+X+X^3…+X^N

Given the numbers, N and X and the task is to find the sum of the given series (1+X+X^2/2…+X^N/N) for the given numbers in Python.

Examples:

Example1:

Input:

Given Number = 7
Given X value = 6

Output:

The total sum of the series till the given number N and x value { 7 6 } =  49743.05714285714

Example2:

Input:

Given Number = 4
Given X value = 3

Output:

The total sum of the series till the given number N and x value { 4 3 } =  37.75

Program to Find the Sum of Series 1+X+X^2/2…+X^N/N in Python

Below are the ways to find the sum of the given series (1+X+X^2/2…+X^N/N) for the given numbers in Python :

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Give the value of X as static input and store it in another variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 1.0 (to get in float format).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, calculate the value of X raised to the power k divided by k using the pow() function and store it in a variable say p.
  • Add the above value of p to the resltsum and store it in the same variable resltsum .
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
gvn_numb = 7
# Give the value of X as static input and store it in another variable.
gvn_x_val = 6
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 1.0 (to get in float format).
resltsum = 1.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
 # Inside the loop, calculate the value of X raised to the power k divided by k using the
    # pow() function and store it in a variable say p.
    p = pow(gvn_x_val, k)/k
  # Add the above value of p to the resltsum and store it in the same variable resltsum.
    resltsum += p
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number N and x value {", gvn_numb, gvn_x_val, "} = ", resltsum)

Output:

The total sum of the series till the given number N and x value { 7 6 } =  49743.05714285714

Method #2: Using While loop (User Input)

Approach:

  • Give the number N as user input using the int(input()) function and store it in a variable.
  • Give the value of X as user input using the int(input()) function and store it in another variable.
  • Take a variable say resltsum which gives the sum of the given series till N and initialize its value to 1.0 (to get in float format).
  • Take another variable say k and initialize its value with 1.
  • Loop until the value of k is less than or equal to the given number using the while loop.
  • Inside the loop, calculate the value of X raised to the power k divided by k using the pow() function and store it in a variable say p.
  • Add the above value of p to the resltsum and store it in the same variable resltsum .
  • Increment the value of k by 1 and store it in the same variable k.
  • Print the resltsum value which is the result of the series till the given Number N.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using the int(input()) function and
# store it in a variable.
gvn_numb = int(input("Enter some Random Number = "))
# Give the value of X as user input using the int(input()) function and
# store it in another variable.
gvn_x_val = int(input("Enter some Random Number = "))
# Take a variable say resltsum which gives the sum of the given series till N and
# initialize its value to 1.0 (to get in float format).
resltsum = 1.0
# Take another variable say k and initialize its value with 1.
k = 1
# Loop until the value of k is less than or equal to the given number using the while loop.
while(k <= gvn_numb):
 # Inside the loop, calculate the value of X raised to the power k divided by k using the
    # pow() function and store it in a variable say p.
    p = pow(gvn_x_val, k)/k
  # Add the above value of p to the resltsum and store it in the same variable resltsum.
    resltsum += p
 # Increment the value of k by 1 and store it in the same variable k.
    k += 1
# Print the resltsum value which is the result of the series till the given Number N.
print(
    "The total sum of the series till the given number N and x value {", gvn_numb, gvn_x_val, "} = ", resltsum)

Output:

Enter some Random Number = 4
Enter some Random Number = 3
The total sum of the series till the given number N and x value { 4 3 } = 37.75

If you are learning Python then the Python Programming Example is for you and gives you a thorough description of concepts for beginners, experienced programmers.