求Pi第n位数字的函数
我一直想找到一种算法来做到这一点。我不在乎它有多慢,只要它能返回 Pi 的第 n 位即可:
例如:
size_t piAt(long long int n)
{
}
最好不要使用无限级数。
如果有人有一个在 C 或 C++ 中执行此操作的函数或类,我真的很想看看它。
谢谢
I have always wanted to find an algorithm that did this. I do not care how slow it is, just as long as it can return the nth digit of Pi:
ex:
size_t piAt(long long int n)
{
}
Preferably, not using an infinite series.
If anyone has a function or class that does this, in C or C++ I'd really be interested in seeing it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是由 Fabrice Bellard 编写的 Simon Plouffe 的解决方案:
它有效:
http://bellard.org /pi/pi_n2/pi_n2.html
Here is a solution of Simon Plouffe coded by Fabrice Bellard:
And it works:
http://bellard.org/pi/pi_n2/pi_n2.html
这个出色的解决方案展示了如何计算 Nth 在 O(N) 时间和 O(log·N) 空间中计算 π 的数字,并且无需计算其之前的所有数字。
哦,它是十六进制的。
如果您不想这样做,您可以从 shell 轻松完成此操作:
This remarkable solution shows how to compute the Nth digit of π in O(N) time and O(log·N) space, and to do so without having to compute all the digits leading up to it.
Oh, and it’s in hex.
If you don’t want to do that, you can do this from the shell easily enough:
包括这个在内的几个解决方案实际上打印了从第 n 位数字开始的几个数字。
http:// 有一个很好的(并且比以前的答案快得多)解决方案Numbers.computation.free.fr/Constants/Algorithms/pidec.cpp
代码只需要修改“typedef int64 ModInt;”行到“typedef int64_t ModInt;”
这是在“2015 年初”的 MacBook Pro 上进行的。
Several of the solutions including this one actually print several digits starting with the nth digit.
There is a fine ( and strikingly faster than previous answers) solution at http://numbers.computation.free.fr/Constants/Algorithms/pidec.cpp
The code only needs modification of "typedef int64 ModInt;" line to "typedef int64_t ModInt;"
This on a "early 2015" macbook pro.