File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change
1
+ from multiprocessing import Process , Queue
2
+ import os , time , random
3
+
4
+ def write (q ):
5
+ for value in ['A' , 'B' , 'C' ]:
6
+ print 'Put %s to queue...' % value
7
+ q .put (value )
8
+ time .sleep (random .random ())
9
+
10
+ def read (q ):
11
+ while True :
12
+ value = q .get (True )
13
+ print 'Get %s from queue.' % value
14
+
15
+ if __name__ == '__main__' :
16
+ q = Queue ()
17
+ pw = Process (target = write , args = (q ,))
18
+ pr = Process (target = read , args = (q ,))
19
+ pw .start ()
20
+ pr .start ()
21
+ pw .join ()
22
+ pr .terminate ()
Original file line number Diff line number Diff line change
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
+ fo = open ("pi.txt" ,"w" )
42
+ fo .write (result )
43
+ fo .close ()
44
+ print (result )
45
+
46
+ time2 = time .time ()
47
+ print ("used time:" + str (time2 - time1 ) + 's' )
You can’t perform that action at this time.
0 commit comments