Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active February 22, 2018 09:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/876f4ce53843535af91c778899b39e5e to your computer and use it in GitHub Desktop.
Save komasaru/876f4ce53843535af91c778899b39e5e to your computer and use it in GitHub Desktop.
Python script to compute Pi with Machin's formula.
#! /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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment