import math
import time
import decimal
time_start = time.time()
D = decimal.Decimal
decimal.getcontext().prec = 4002
a = 1
b = 1/D(2).sqrt()
t = 1/4
p = 1
w = 0
while w<2:
a1 = (a+b)/2
b1 = D(a*b).sqrt()
t1 = D(t)-p*((a-(a+b)/2)**2)
p1 = 2*p
a = a1
b = b1
t = t1
p = p1
w = w+1
pi = D(((a1 + b1) ** 2) / (4 * t1))
print(pi)
print(w)
time_end = time.time()
print(f'迭代{w}次计算圆周率:\n{(pi)}')
print(f'计算完成。时间(秒):{time_end-time_start}')