In the previous article, we have discussed Python Program to Find Sum of Series 5^2+10^2+15^2+…..N^2
Given a number N and the task is to find the sum of series (1/2!+2/3!+3/5!+…..N/(N+1)!) till the given number N in Python.
Examples:
Example1:
Input:
Given Number (Limit) = 6
Output:
The above series sum till the given number N{ 6 } = 0.9998015873015872
Example2:
Input:
Given Number (Limit) = 13
Output:
The above series sum till the given number N{ 13 } = 0.9999999999885293
Program to Find Sum of Series 1/2!+2/3!+3/5!+…..N/(N+1)!
Below are the ways to find the sum of series (1/2!+2/3!+3/5!+…..N/(N+1)!) till the given number N in Python:
Method #1: Using While Loop (Static Input)
Approach:
- Import math module using the import keyword.
- Give the number N (Limit) as static input and store it in a variable.
- Take a variable to say itr and initialize its value to 1.
- Take another variable to say rsltseries_summ and initialize its value to 0.0 (Floating point number).
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Calculate the factorial of itr+1 using the math.factorial() method and store it in another variable.
- Calculate the value of itr divided by the above result factorial and convert it into float using the float() function.
- Store it in another variable.
- Add the above result to the rsltseries_summ and store it in the same variable.
- Increment the above itr value by 1.
- Print the sum of series till the given number N.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the number N(limit) as static input and store it in a variable. gvn_numb = 6 # Take a variable to say itr and initialize its value to 1. itr = 1 # Take a variable to say rsltseries_summ and initialize its value to 0.0 # (Floating point number) rsltseries_summ = 0.0 # Loop until the above-declared variable itr value is less than or equal to the # given number using the while loop. while(itr <= gvn_numb): # Calculate the factorial of itr+1 using the math.factorial() method and # store it in another variable. factrl = math.factorial(itr+1) # Calculate the value of itr divided by the above result factorial and convert it # into float using the float() function. # Store it in another variable. p = float(itr/factrl) # Add the above result to the rsltseries_summ and store it in the same variable. rsltseries_summ += p # Increment the above itr value by 1. itr += 1 # Print the sum of series till the given number N. print( "The above series sum till the given number N{", gvn_numb, "} = ", rsltseries_summ)
Output:
The above series sum till the given number N{ 6 } = 0.9998015873015872
Method #2: Using While loop (User Input)
Approach:
- Import math module using the import keyword.
- Give the number N (Limit) as user input using the int(input()) function and store it in a variable.
- Take a variable to say itr and initialize its value to 1.
- Take another variable to say rsltseries_summ and initialize its value to 0.0 (Floating point number).
- Loop until the above-declared variable itr value is less than or equal to the given number using the while loop.
- Calculate the factorial of itr+1 using the math.factorial() method and store it in another variable.
- Calculate the value of itr divided by the above result factorial and convert it into float using the float() function.
- Store it in another variable.
- Add the above result to the rsltseries_summ and store it in the same variable.
- Increment the above itr value by 1.
- Print the sum of series till the given number N.
- The Exit of the Program.
Below is the implementation:
# Import math module using the import keyword. import math # Give the number N (Limit) as user input using the int(input()) function and # store it in a variable. gvn_numb = int(input("Enter some Random Number = ")) # Take a variable to say itr and initialize its value to 1. itr = 1 # Take a variable to say rsltseries_summ and initialize its value to 0.0 # (Floating point number) rsltseries_summ = 0.0 # Loop until the above-declared variable itr value is less than or equal to the # given number using the while loop. while(itr <= gvn_numb): # Calculate the factorial of itr+1 using the math.factorial() method and # store it in another variable. factrl = math.factorial(itr+1) # Calculate the value of itr divided by the above result factorial and convert it # into float using the float() function. # Store it in another variable. p = float(itr/factrl) # Add the above result to the rsltseries_summ and store it in the same variable. rsltseries_summ += p # Increment the above itr value by 1. itr += 1 # Print the sum of series till the given number N. print( "The above series sum till the given number N{", gvn_numb, "} = ", rsltseries_summ)
Output:
Enter some Random Number = 13 The above series sum till the given number N{ 13 } = 0.9999999999885293
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.