Python中的1000位圆周率
我一直在想这个问题,想不通。也许你可以帮助我。问题是我的代码无法以 Python 编码语言输出 1000 位 pi。
这是我的代码:
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
我知道可以修复此代码以使其工作,但我不确定要修复什么... print
语句说这里是一个大字符串,而 my_array.append (str("hello, im an element in an array \n))
现在只是一个填充符。我知道所有代码是如何工作的,但就像我之前说的,我无法理解射出该代码。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您不想实现自己的算法,可以使用mpmath。
参考
更新:代码支持 SymPy 的旧版和新版安装(请参阅评论)。*
If you don't want to implement your own algorithm, you can use mpmath.
Reference
Update: Code supports older and newer installations of SymPy (see comment).*
运行这个
并从这里阅读有关
yield
运算符的内容:“yield”关键字有什么作用?
答案如下:
Run this
And read about
yield
operator from here:What does the "yield" keyword do?
Here is the answer:
正如评论中所述,接受的答案是不正确的。
OP的代码似乎基于从Spigot算法的实现://mail.python.org/pipermail/edu-sig/2006-July/006810.html" rel="noreferrer">此处。
要根据OP的问题修复代码(尽管我重命名了变量和函数以匹配它们在原始源中的内容),一种解决方案可能是:
另外,这里有一个更快的*实现,显然也基于Spigot的算法:
我针对这个在线 Pi 数字生成器对两者进行了几次测试。
所有功劳均归功于 此 Gist,作者:深度查找。
* 基于对 10,000 个数字的测试,我得到了大约 7 秒,而相比之下,我得到了大约 1 秒。
The accepted answer is incorrect, as noted in comments.
The OP's code appears to be based on an implementation of Spigot's algorithm copied from here.
To fix the code per the OP's question (although I renamed the variables and functions to match what they were in the original source), one solution might be:
Also, here is a much faster* implementation, also apparently based on Spigot's algorithm:
I tested both a few times against this online Pi digit generator.
All credit to this Gist by deeplook.
* Based on testing 10,000 digits, where I got about 7 seconds compared to about 1 second.
对于最多 100 万位 pi,请使用 math_pi (注意:我是模块)
使用 pip 安装:
在 Python 中:
For up to 1 million digits of pi use math_pi (note: I am the author of the module)
Install with pip:
In Python:
来自 Fabrice Bellard 网站:Pi 计算算法。很抱歉如此简单的实现。 1000 已经足够快了(对我来说 0.1 秒),但 10000 就没那么快了 - 71 秒:-(
From Fabrice Bellard site: Pi Computation algorithm. Sorry for such a straightforward implementation. 1000 is fast enough (0.1s for me), but 10000 isn't such fast - 71s :-(
5-6年前我用下面的公式解决了这个问题。
类机器公式
维基百科:https://en.wikipedia.org /wiki/Machin-like_formula
对代码质量表示歉意。变量名可能毫无意义。
I was solved with bellow formula 5-6 years ago.
Machin-like formula
Wikipedia: https://en.wikipedia.org/wiki/Machin-like_formula
Sorry for the code quality. Variable names can be meaningless.
我不熟悉你的算法。它是BBP的实现吗?
无论如何,您的
make_pi
都是一个生成器。尝试在 for 循环中使用它:请注意,此循环是无限的:
make_pi()
永远不会抛出StopIteration
I'm not familiar with your algorithm. Is it an implementation of BBP?
In any case, your
make_pi
is a generator. Try using it in a for loop:Note that this loop is infinite:
make_pi()
never throwsStopIteration
在这里你可以检查你的程序是否输出正确的1000位数字:
http://spoj.com/CONSTANT
当然你也可以使用 diff 或 tc 但你必须从某处复制这 1000 个数字,然后提交程序并检查分数是否大于 999。
您可以尝试在那里打印更多数字,从而获得更多分数。也许你会喜欢它。
Here you can check whether your program outputs correct 1000 digits:
http://spoj.com/CONSTANT
Of course you can use diff or tc as well but you'd have to copy these 1000 digits from somewhere and there you just submit your program and check whether the score is bigger than 999.
You can try to print even more digits there and thus get more points. Perhaps you'd enjoy it.
这是你想要的吗?
Does this do what you want?
这是我在这里找到的另一种方式 --> Python pi 计算? 基于 Chudnovsky brothers 生成 Pi 的公式来近似 python,我已经对其进行了稍微修改我的程序。
我希望这会有所帮助,因为您可以生成您想要生成的任意位数的 pi。
Here is a different way I found here --> Python pi calculation? to approximate python based on the Chudnovsky brothers formula for generating Pi which I have sightly modified for my program.
I hope this helps as you can generate any number of digits of pi that you wish to generate.
wallis公式可以得到3.141592661439964,但需要更有效的方法来解决这个问题。
https://www.youtube.com/watch?v=EZSiQv_G9HM
现在我的代码
wallis formula can get to 3.141592661439964 but a more efficient way is needed to solve this problem.
https://www.youtube.com/watch?v=EZSiQv_G9HM
and now my code