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
I've gotten the first two checks correct but as soon as I enter double digits it begins to get the wrong numbers at the very end of the chain. Could anyone help me?
number=float(input("Enter the number iterations: "))
pi=0
div=1
while(div<=number):
positive_value = 4/(2*div-1)
pi += positive_value
div=div+2
div=2
while(div<=number):
negative_value=4/(2*div-1)
pi -=negative_value
div=div+2
print("The approximation of pi is %.16f "%pi)