1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
| #! /usr/local/bin/python3.6
"""
Computation of Pi with Machin's formula
"""
import sys
import time
import traceback
class CalcPiMachin:
L = 1000 # Digits of computation
FNAME = "pi_machin.txt"
def __init__(self):
self.l1 = int(self.L / 8 + 1)
self.n = int(self.L / 1.39794 + 1)
def compute(self):
""" Computation of Pi """
try:
t0 = time.time()
s = [0 for _ in range(self.l1 + 2)]
a = [0 for _ in range(self.l1 + 2)]
b = [0 for _ in range(self.l1 + 2)]
q = [0 for _ in range(self.l1 + 2)]
a[0] = 16 * 5
b[0] = 4 * 239
for k in range(1, self.n + 1):
a = self.__long_div(a, 5 * 5)
b = self.__long_div(b, 239 * 239)
q = self.__long_sub(a, b)
q = self.__long_div(q, 2 * k - 1)
if k % 2 == 0:
s = self.__long_sub(s, q)
else:
s = self.__long_add(s, q)
t1 = time.time()
tt = t1 - t0
self.__display(tt, s)
except Exception as e:
raise
def __long_add(self, a, b):
""" Computation of long + long
:param list a: integral values
:param list b: integral values
:return list z: integral values
"""
try:
z = [0 for _ in range(self.n)]
cr = 0
for i in reversed(range(self.l1 + 2)):
z[i] = a[i] + b[i] + cr
if z[i] < 100000000:
cr = 0
else:
z[i] -= 100000000
cr = 1
return z
except Exception as e:
raise
def __long_sub(self, a, b):
""" Computation of long - long
:param list a: integral values
:param list b: integral values
:return list z: integral values
"""
try:
z = [0 for _ in range(self.n)]
br = 0
for i in reversed(range(self.l1 + 2)):
z[i] = a[i] - b[i] - br
if z[i] >= 0:
br = 0
else:
z[i] += 100000000
br = 1
return z
except Exception as e:
raise
def __long_div(self, a, b):
""" Computation of long / short
:param list a: integral values
:param list b: integral values
:return list z: integral values
"""
try:
z = [0 for _ in range(self.n)]
r = 0
for i in range(self.l1 + 2):
w = a[i]
z[i] = (w + r) // b
r = ((w + r) % b) * 100000000
return z
except Exception as e:
raise
def __display(self, tt, s):
""" Display
:param float tt: elapsed time
:param list s: result of calculation
"""
try:
print("** Pi Computation with the Machin formula method **")
print(" Digits = {:d}.".format(self.L))
print(" Time = {:f} seconds".format(tt))
out_file = open(self.FNAME, "w")
out_file.write("** Pi Computation with the Machin formula method **\n")
out_file.write(" Digits = {:d}.\n".format(self.L))
out_file.write(" Time = {:f} seconds.\n\n".format(tt))
out_file.write(" {:d}.\n".format(s[0]))
for i in range(1, self.l1):
if i % 10 == 1:
out_file.write("{:08d}:".format((i - 1) * 8 + 1))
out_file.write(" {:08d}".format(s[i]))
if i % 10 == 0:
out_file.write("\n")
out_file.close
except Exception as e:
raise
if __name__ == '__main__':
try:
obj = CalcPiMachin()
obj.compute()
except Exception as e:
traceback.print_exc()
sys.exit(1)
|