Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/carlolepelaars/pi-digits
Compute nth digit of pi in Python
https://github.com/carlolepelaars/pi-digits
mathematics
Last synced: about 20 hours ago
JSON representation
Compute nth digit of pi in Python
- Host: GitHub
- URL: https://github.com/carlolepelaars/pi-digits
- Owner: CarloLepelaars
- License: mit
- Created: 2023-11-18T15:40:20.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-18T21:05:49.000Z (about 1 year ago)
- Last Synced: 2023-11-18T22:21:42.621Z (about 1 year ago)
- Topics: mathematics
- Language: Python
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pi-digits
Compute nth digit of Pi using an asymptotic formula from [Plouffe (2022)](https://arxiv.org/abs/2201.12601).Implemented in the following programming languages:
- Python (Python 3.11+)## Explanation
For $n \geq 3$, the nth digit of $\pi$ is obtained by first calculating $\pi_n$.
$$\pi_n = \left( \frac{2 (-1)^{n+1} (2n)!}{2^{2n} B_{2n} (1 - 2^{-2n}) (1 - 3^{-2n}) (1 - 5^{-2n}) (1 - 7^{-2n}) (1 - 11^{-2n})} \right)^{\frac{1}{2n}}$$
where $B_{2n}$ is the [Bernoulli number](https://en.wikipedia.org/wiki/Bernoulli_number) of $2n$.
Then the nth digit of $\pi$ is given by:
$$d_n = \text{int} \left( 10 \text{frac} \left( 10^{n-1} \pi_{n-1} \right) \right)$$
The Bernoulli number can be obtained using the [Zeta function](https://en.wikipedia.org/wiki/Riemann_zeta_function) as follows:
$$B_{2n} = -2n * \zeta(1 - 2n)$$
## Usage
Make sure to have `mpmath` installed.
`pip install mpmath`
```python
from pi_python import pi_nth_digit
pi_nth_digit(10) # 5
```To run tests:
```bash
pip install pytest
test_pi_python.py -s
```## Credits
This asymptotic formula for calculating the nth digit of pi was discovered by [Simon Plouffe in 2022](https://arxiv.org/abs/2201.12601). The paper also discusses a way to calculate the nth digit of pi using [Euler numbers](https://en.wikipedia.org/wiki/Euler_numbers).
Big thanks to [Martin Bauer](https://twitter.com/martinmbauer/status/1614571838721622022?s=20&t=IznMtorWVeNbjlX-A5obNw) for the inspiration and his illustration of this formula.