马青公式:
简单实现
import time
s_time = time.time()
#定义计算的位数
num = 100000
#多计算10位,以防出错
num1 = num + 10
#定义计算小数位数
b = 10**num1
#前面括号的第一项
x1 = 4*b//5
#后面括号的第一项
x2 = 1*b//(-239)
he = x1 + x2
#定义循环截止数
num = 2*num
for i in range(3,num,2):
x1 = x1//-25
x2 = x2//(-239**2)
x = (x1+ x2)//i
he = he + x
he = he *4
#把多计算的10位去掉
pai = he // 10**10
#转成字符串类型
str_pai = str(pai)
#字符串拼接成小数
result = str_pai[0] + '.' + str_pai[1:len(str_pai)]
print(result)
#写出到文件内
with open('pai.txt',mode = 'w') as f:
f.write(result)
e_time = time.time()
print("计算耗时:{:.100f}".format(round(e_time-s_time)))
封装方法
"马青公