python - 计算 pi 的小数第 N 位

标签 python algorithm pi

<分区>

我需要什么:

计算 Pi 的第 N 个小数 位的函数。示例:

>>> pi_digit(1)
1
>>> pi_digit(2)
4
>>> pi_digit(3)
1
>>> pi_digit(4)
5

我尝试了什么:

搜索时,发现 code .它实际上做了我需要的,除了一件事 - 它计算所有 N 位数字,而我只需要最后一个。这有可能以有效的方式完成吗? (如果我只是为最后一个字符切片字符串,它就不会那么快)。提前致谢。

为什么我需要它:

我打算将该脚本(将数字附加到文本文件)放到我的服务器上并保留 20-30 天,然后检查输出。

最佳答案

见:

Pi 数字的无界 Spigot 算法

杰里米·吉本斯

The program under discussion is written in Haskell [5], a lazy functional programming language. As a secondary point of this paper, we hope to convince the reader that such languages are excellent vehicles for expressing mathematical computations, certainly when compared with other general-purpose programming languages such as Java, C, and Pascal, and arguably even when compared with computer algebra systems such as Mathematica.

https://www.cs.ox.ac.uk/jeremy.gibbons/publications/spigot.pdf

可以用pidigits来实现

https://github.com/transmogrifier/pidigits

pidigits is avalaible through Python Package Index (PyPI) using pip.

>>> pip install --upgrade pidigits

关于python - 计算 pi 的小数第 N 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43161155/

相关文章:

python - 如何先按值排序字典,然后按键排序

linux - 在矩形内生成随机点(均匀地)?

algorithm - 分析算法-递归方程(汉诺塔)

python - 传送旅行者,随着时间的推移优化利润问题

c - 使用OpenMP任务指令计算PI

python - 如何在 pylab 中的 nan 上插入行?

python - 在python中导入一个数组

c++ - 实现 Bailey-Borwein-Plouffe

Python:从具有相同 x 和 y 坐标的 X、Y、Z 文件中删除所有重复点的方法

c++ - 使用 for 循环计算 Pi