Skip to content

Instantly share code, notes, and snippets.

@idoushiki
Last active December 7, 2017 10:27
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 idoushiki/707ac993c47ed6b25d4e5dc7f41e3468 to your computer and use it in GitHub Desktop.
Save idoushiki/707ac993c47ed6b25d4e5dc7f41e3468 to your computer and use it in GitHub Desktop.
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)
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