Last active
October 24, 2018 13:51
-
-
Save Varbin/f7c2984d4045e57803ad84139259f790 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal, getcontext | |
from time import clock | |
from math import pi | |
from multiprocessing import Pool, cpu_count | |
import sys | |
if len(sys.argv) < 2: | |
n = 10000 | |
else: | |
try: | |
n = int(sys.argv[1]) | |
except: | |
print("Fehler: Argument ist keine gültige Zahl.", file=sys.stderr) | |
sys.exit( -1) | |
f = lambda x: 7.5 * x **-.5 | |
getcontext().prec=n | |
le = 0 | |
#s = [Decimal(1)/Decimal(3) for i in range(n)] | |
e = Decimal(0) | |
def pi_p(q): | |
v,b, pb = q | |
s = [Decimal(1)/Decimal(3) for i in range(b-v)] | |
#n = b-v | |
l = clock() | |
for k in range(v, b): | |
#print(k) | |
s[v-k] = (1/Decimal(16)**k * | |
(Decimal(4)/(8*k+1) - | |
Decimal(2)/(8*k+4) - | |
Decimal(1)/(8*k+5) - | |
Decimal(1)/(8*k+6))) | |
if pb: | |
if (not (k % int(n/100/25+1))) or k == 99: | |
p = round(k/n*100, 1) | |
print(" ["+"#"*int(p//2)+" "*(50-int(p//2))+"] "+str(p)+"%", end=" ") | |
if k < 99: | |
print(end="\r") | |
continue | |
elif k == 99: | |
g = sum(f(i) for i in range(100,n+1)) / (n-100) | |
vk = (20/(clock()-l)) * g | |
eta = int((n-k)/vk) | |
m, se = divmod(eta, 60) | |
if not m: | |
h = 0 | |
else: | |
h, m = divmod(m, 60) | |
print("ETA: %d:%02d:%02d" % (h, m, se), end="\r") | |
return sum(s) | |
#pi_p | |
if __name__ == "__main__": | |
ls = clock() | |
c = cpu_count() | |
args = [] | |
p, r = divmod(n, c) | |
for i in range(c): | |
arg = [i*n, (i+1)*n ,i==0] | |
args.append(arg) | |
with Pool() as p: | |
mpi = sum(p.map(pi_p, args)) | |
assert round(pi, 4) == float(round(mpi, 4)) | |
print() | |
print("GS:",clock()-ls) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment