python Estimate p p can be computed using the following seri
python
Estimate p) p can be computed using the following series:
m(i)=4(1-1/3+1/5-1/7 +.....+((-1)^(i+1))/(2i-1))
Write a function that returns m(i) for a given i and write a test program that dis- plays the following table:
i
m(i)
Solution
Please find the required program along with its output. Please see the comments against each line to understand the step.
import math # This will import math module
def m(i): # function that return value of m(i) for input i
sum = 0
for j in range(1,i+1): #iterate loop till i+1 times
sum = sum + (math.pow(-1,j+1)/((2*j)-1)) #summation of the given series
sum = 4 * sum
return float(sum) #return number
print (\"i\\t m(i)\") #print the output
print (\"1\\t\", round(m(1),4))
print (\"101\\t\", round(m(101),4))
print (\"201\\t\", round(m(201),4))
print (\"301\\t\", round(m(301),4))
print (\"401\\t\", round(m(401),4))
print (\"501\\t\", round(m(501),4))
print (\"601\\t\", round(m(601),4))
print (\"701\\t\", round(m(701),4))
print (\"801\\t\", round(m(801),4))
print (\"901\\t\", round(m(901),4))
-------------------------------------------------------
OUTPUT :
