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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
| #! /usr/local/bin/python3.6
"""
Computation of Pi with Euler's formula
"""
import math
import sys
import time
import traceback
class CalcPiEuler:
L = 10000 # Digits of computation
FNAME = "pi_eular.txt"
def __init__(self):
self.l1 = int(self.L / 8) + 1
n = int(self.L / math.log10(7)) + 1
self.n = int(n / 2) + 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] = 20 * 7
b[0] = 8 * 79
b = self.__long_div(b, 3)
for k in range(1, self.n + 1):
a = self.__long_div(a, 7 * 7)
b = self.__long_mul(b, 3 * 3)
b = self.__long_div(b, 79 * 79)
q = self.__long_add(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
:param list b
:return list z
"""
try:
z = [0 for _ in range(self.l1 + 2)]
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
:param list b
:return list z
"""
try:
z = [0 for _ in range(self.l1 + 2)]
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_mul(self, a, b):
""" Computation of long * short
:param list a
:param list b
:return list z
"""
try:
z = [0 for _ in range(self.l1 + 2)]
cr = 0
for i in reversed(range(self.l1 + 2)):
w = a[i]
z[i] = (w * b + cr) % 100000000
cr = int((w * b + cr) / 100000000)
return z
except Exception as e:
raise
def __long_div(self, a, b):
""" Computation of long / short
:param list a
:param list b
:return list z
"""
try:
z = [0 for _ in range(self.l1 + 2)]
r = 0
for i in range(self.l1 + 2):
w = a[i]
z[i] = int((w + r) / b)
r = ((w + r) % b) * 100000000
return z
except Exception as e:
raise
def __display(self, tt, s):
""" Display
:param float tt
:param list s
"""
try:
print("** Pi Computation with the Eular 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 Eular 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 = CalcPiEuler()
obj.compute()
except Exception as e:
traceback.print_exc()
sys.exit(1)
|