利用Python的特殊功能,简单实现万位高精度圆周率计算。
啥也别说了,直接给代码:
# -*- coding: UTF-8 -*-
# calculating PI with a simple series
# Author: Idealguy,Shanghai,2018
# pi=2*(1+1/3+1/3*2/5+1/3*2/5+3/7+...)
#
n=10000+4
p=2*10**n
a=p//3; p+=a
i=2
while a>0:
a=a*i//(i*2+1); i+=1
p+=a
p//=10000
print ("PI=",p)
计算结果(on Python 3.6.3 )
>>> ==================== RESTART: D:\Python\math\ ====================
PI=
3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
982148086513282306647093844609550582231725359408128481117450284102701938521105559644622948954930381