You can approximate pi by using the following summation: pi=4(1−1/3+1/5−1/7+1/9−1/11+⋯+(−1)^i+1/2i-1)
Write a program that displays the pi value for i = 10000, 20000, … and 100000. Program Output
3.1414926535900345
3.1415426535898248
3.141559320256462
3.1415676535897985
3.1415726535897814
3.141575986923102
3.141578367875482
3.1415801535897496
3.1415815424786238
3.1415826535897198
n = 10000
for i in range(1, 11):
s = 0
for i in range(1, n):
s += ((-1) ** (i + 1)) / (2 * i - 1)
print(f'i = {n} pi =', 4 * s)
n += 10000
Comments
Leave a comment