我设法让我的代码正常工作,因此它会生成pi:
while True:
print("how many digits of pi would you like?")
def make_pi():
q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
for j in range(1000000):
if 4 * q + r - t < m * t:
yield m
q, r, t, k, m, x = 10 * q, 10 * (r - m * t), t, k, (10 * (3 * q + r)) // t - 10 * m, x
else:
q, r, t, k, m, x = q * k, (2 * q + r) * x, t * x, k + 1, (q * (7 * k + 2) + r * x) // (t * x), x + 2
digits = make_pi()
pi_list = []
my_array = []
for i in make_pi():
my_array.append(str(i))
number = int(input())+2
my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array[: number ])
print("here is the string:\n %s" % big_string)
然而无论我增加多少range
代码只输出小数点后最多2315位pi
how can I fix this?
答案 0 :(得分:0)
参数化make_pi生成器以接受位数怎么样?
这样的事情:
def make_pi(num_digits):
q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
for j in range(num_digits):
if 4 * q + r - t < m * t:
yield m
q, r, t, k, m, x = 10 * q, 10 * \
(r - m * t), t, k, (10 * (3 * q + r)) // t - 10 * m, x
else:
q, r, t, k, m, x = q * \
k, (2 * q + r) * x, t * x, k + \
1, (q * (7 * k + 2) + r * x) // (t * x), x + 2
num_digits = 10000
pi = "".join([str(d) for d in make_pi(num_digits)])
print("{0}.{1}".format(pi[:1], pi[1:]))