The German mathematician Gottfried Leibniz developed the following method to approximate the value of π:
π/4 = 1 - 1/3 + 1/5 - 1/7 + . . .
Write a program that allows the user to specify the number of iterations used in this approximation and that displays the resulting value.
An example of the program input and output is shown below:
Enter the number of iterations: 5
The approximation of pi is 3.3396825396825403
iteration = int(input("Enter the number of iteration: "))
list01 = []
list02 = []
y = 1
for x in range(1, iteration+1):
number = (1.0/y)
list01.append(number)
y += 2.0
#print(number)
for i in range(1, iteration, 2):
neg = list01[i]*-1.0
list02.append(neg)
#print(neg)
comb = (sum(list01)) + (sum(list02)) + (sum(list02))
pi = comb*4.0
print("The approximation of pi is", pi)
With this code for:
1 iteration, output is 4.0 which matches the required output of 4
5 iteration, output is 3.339682539682539 which doesn't match the required output of 3.3396825396825403
10 iteration, output is 3.0418396189294015 which doesn't match the required output of 3.0418396189294032
999 iteration, output is 3.1425936543400352 which doesn't match the required output of 3.142593654340044