Python 中 pi 的 1000 位数字 [英] 1000 digits of pi in Python

查看:57
本文介绍了Python 中 pi 的 1000 位数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在思考这个问题,但我想不通.或许你可以帮助我.问题是我的代码无法在 Python 编码语言中输出 1000 位 pi.

这是我的代码:

def make_pi():q, r, t, k, m, x = 1, 0, 1, 1, 3, 3为真:如果 4 * q + r - t <米 * 吨:产量米q, r, t, k, m, x = (10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x)别的: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)数字 = make_pi()pi_list = []my_array = []对于我在范围内(1000):my_array.append(str("你好,我是数组中的一个元素\n"))big_string = "".join(my_array)打印这是一个大字符串:\n %s" % big_string

我知道可以修复这段代码,但我不确定要修复什么……print 声明说这里是一个大字符串,my_array.append(str("hello, im an element in an array \n)) 现在只是一个填充物.我知道所有代码是如何工作的,但就像我之前说的,我无法得到它射出那个代码.

解决方案

运行这个

def make_pi():q, r, t, k, m, x = 1, 0, 1, 1, 3, 3对于范围内的 j(1000):如果 4 * q + r - t <米 * 吨:产量米q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x别的: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+2my_array = []对于 make_pi() 中的 i:my_array.append(str(i))my_array = my_array[:1] + ['.'] + my_array[1:]big_string = "".join(my_array)打印这是一个大字符串:\n %s" % big_string

并从此处阅读有关 yield 运算符的信息:什么是产量"?关键字做什么?

答案如下:

<预> <代码> 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337

I have been thinking about this issue and I can't figure it out. Perhaps you can assist me. The problem is my code isn't working to output 1000 digits of pi in the Python coding language.

Here's my code:

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    while True:
        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 range(1000):
    my_array.append(str("hello, I'm an element in an array \n" ))
big_string = "".join(my_array)

print "here is a big string:\n %s" % big_string 

I know this code can be fixed to work, but I'm not sure what to fix... The print statement saying here is a big string and the my_array.append(str("hello, im an element in an array \n)) is just a filler for now. I know how all the code is used to work, but like I said before, I can't get it to shoot out that code.

解决方案

Run this

def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    for j in range(1000):
        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


my_array = []

for i in make_pi():
    my_array.append(str(i))

my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array)
print "here is a big string:\n %s" % big_string 

And read about yield operator from here: What does the "yield" keyword do?

Here is the answer:

3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337

这篇关于Python 中 pi 的 1000 位数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆