Last active
December 7, 2017 10:27
-
-
Save idoushiki/707ac993c47ed6b25d4e5dc7f41e3468 to your computer and use it in GitHub Desktop.
This file contains 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
import math | |
from decimal import * | |
import numpy as np | |
def getp(prec=1000, v=True): | |
prec = prec+2 | |
N = 2*math.ceil(math.log2(prec)) | |
getcontext().prec = prec | |
a = Decimal(1.0) | |
b = Decimal(1.0) / Decimal(2.0).sqrt() | |
t = Decimal(0.25) | |
p = Decimal(1.0) | |
# N回 | |
for _ in range(1, N): | |
a_next = (a + b)/Decimal(2) | |
b_next = (a*b).sqrt() | |
t_next = t - p * (a - a_next)**2 | |
p_next = Decimal(2)*p | |
a = a_next | |
b = b_next | |
t = t_next | |
p = p_next | |
#円周率 | |
pi = (a + b)**2 / (Decimal(4)*t) | |
if v: | |
print("桁:",prec) | |
print("円周率:", pi) | |
return str(pi)[0:prec] | |
pi = getp(prec=1000, v=True) |
This file contains 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
import math | |
from decimal import * | |
prec=1000 | |
N = 2*math.ceil(math.log2(prec)) | |
getcontext().prec = prec | |
a = Decimal(1.0) | |
b = Decimal(1.0) / Decimal(2.0).sqrt() | |
t = Decimal(0.25) | |
p = Decimal(1.0) | |
# N回の試行を開始 | |
for _ in range(1, N): | |
a2 = (a + b)/Decimal(2) | |
b2 = (a*b).sqrt() | |
t2 = t - p * (a - a2)**2 | |
p2 = Decimal(2)*p | |
a = a2 | |
b = b2 | |
t = t2 | |
p = p2 | |
#円周率 | |
pi = (a + b)**2 / (Decimal(4)*t) | |
#実行時間を計算 | |
#結果 | |
print("桁:",prec) | |
print("円周率:", pi) | |
str(pi)[0:prec] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment