#include<math.h>
main()
{
double t,pi
long int n,s
t=1.0
n=1
s=1
pi=0.0
while (fabs(t)>=1e-6)
{
pi=pi+t
n=n+2
s=-s
t=(float)(s)/(float)(n)
}
pi=pi*4
printf("pi=%lf\n",pi)
}
res= 0n = 1
i = 1
while n >1e-5:
res += n
i += 1
n = 1 / (i * i)
#encoding=utf-8sum = 0
i = 1
t = 1
while abs(t) >1e-5:
t = (-1)**(i+1)/(2*i-1)
sum += t
i += 1
print('%.5f' % (4*sum))
运行结果
python3 a.py
3.14161