请问如何用Python求出pi的近似值

Python017

请问如何用Python求出pi的近似值,第1张

#include<stdio.h>

#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= 0

n = 1

i = 1

while n >1e-5:

res += n

i += 1

n = 1 / (i * i)

#encoding=utf-8

sum = 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