我需要什么:
计算 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