用python计算圆周率

Posted w2538060594

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用python计算圆周率相关的知识,希望对你有一定的参考价值。

import time
def comput():
n =int(input(‘请输入要计算的长度:‘))
start_time = time.time()
w = n+10
b = 10**w
x1 = b*4//5
x2 = b// -239
he = x1+x2
n *= 2
for i in range(3,n,2):
x1 //= -25
x2 //= -57121
x = (x1+x2) // i
he += x
pai = he*4
pai //= 10**10
end_time = time.time()
run_time = str(end_time - start_time)
paistr=str(pai)
paistr=paistr[:1] + ‘.‘ + paistr[1:]
f=open(‘pai.txt‘,‘w‘)
f.write(paistr)
f.close()
print (‘运行时间:‘ + run_time )
#print (‘计算结果:‘,pai)
print (‘ ‘*1)
comput()

以上是关于用python计算圆周率的主要内容,如果未能解决你的问题,请参考以下文章

鬼才!用Python计算圆周率 π

鬼才!用Python计算圆周率 π

用python计算圆周率PI

Python用积分思想计算圆周率

今天用python完成的是带进度条的圆周率计算

现代计算机是如何计算圆周率的?