Skip to content

Commit 69540e5

Browse files
committed
new pi fuction
1 parent 255f9e6 commit 69540e5

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pi2.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import division
3+
import time
4+
# π/4=4arctan1/5-arctan1/239
5+
number = int(input('please in put the No: '))
6+
time1=time.time()
7+
number1 = number+10
8+
9+
# 算到小数点后number1位
10+
b = 10**number1
11+
12+
# 求含4/5的首项
13+
x1 = b*4//5
14+
# 求含1/239的首项
15+
x2 = b// -239
16+
17+
# 求第一大项
18+
he = x1+x2
19+
#设置下面循环的终点,即共计算n项
20+
number *= 2
21+
22+
#循环初值=3,末值2n,步长=2
23+
for i in range(3,number,2):
24+
# 求每个含1/5的项及符号
25+
x1 //= -25
26+
# 求每个含1/239的项及符号
27+
x2 //= -57121
28+
# 求两项之和
29+
x = (x1+x2) // i
30+
# 求总和
31+
he += x
32+
33+
# 求出π
34+
pai = he*4
35+
#舍掉后十位
36+
pai //= 10**10
37+
38+
############ 输出圆周率π的值
39+
paistring=str(pai)
40+
result=paistring[0]+str('.')+paistring[1:len(paistring)]
41+
print (result)
42+
43+
time2=time.time()
44+
print ("used time:"+str(time2 - time1) + 's')

0 commit comments

Comments
 (0)