Pi Calculation in Rust
This is a Rust CLI tool for calculating Pi digits using various algorithms. It can compute to a million digits of Pi in less than 1 second (see below).
Supported algorithms are listed as follows:
| Algorithm | Note |
|---|---|
| Leibniz | Slowest |
| Bailey-Borwein-Plouffe (BBP) | |
| Spigot Gosper | |
| Newton 9th Order Convergence | Page 9 of the paper |
| Borwein's Formula | |
| Brent-Salamin | |
| Gauss-Legendre | Only slightly different from Brent-Salamin. |
| Machin-like Formula | Using arctan to calculate Pi. |
| Chudnovsky | |
| Chudnovsky Binary Splitting | World record-breaking algorithm. |
| Chudnovsky Binary Splitting Parallelized | Fastest. |
Installation
cargo install --git https://github.com/BreezeWhite/calc_pi
Usage
CLI for calculating millions of Pi digits within seconds. Various algorithms are supported.
Usage: calc_pi [OPTIONS] <COMMAND>
Commands:
leibniz Leibniz formula
bbp Bailey–Borwein–Plouffe formula
spg Spigot Gosper algorithm
newton Newton method. 9th order convergence
bn Borwein algorithm nonic (9th) convergence version
bs Brent–Salamin algorithm
gl Gauss–Legendre algorithm
ag Machin-like formulas (arctan)
chu Chudnovsky algorithm
cb Chudnovsky algorithm with binary splitting
cbp Chudnovsky algorithm with binary splitting and multi-thread
help Print this message or the help of the given subcommand(s)
Options:
-p, --prec <PREC> Precision of Pi to calculate in digits [default: 1000]
--measure-time Measure and show the runtime
--output-to <OUTPUT_TO> Path of file to output and store the calculated Pi digits
-h, --help Print help
-V, --version Print version
# Calculate Pi with Chudnovsky binary splitting algorithm to 1,000,000 digits.
calc_pi -p 1000000 cbp
# Show the runtime.
calc_pi -p 1000000 --measure-time cbp
# Output the result to file
calc_pi -p 1000000 --output-to pi.txt cbp
Recommended Reading
- Practical implementation of PI Algorithms: This paper provides a comprehensive overview of various algorithms for calculating Pi, including their implementation details and performance comparisons.
- Craig Wood's article on Chudnovsky algorithm: A detailed explanation of the Chudnovsky algorithm, including its mathematical background and implementation.
Performance
Test Environment
- OS: Ubuntu 20.04
- CPU: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
- RAM: 16GB
- Rust Version: 1.86.0
- Cargo Version: 1.86.0
| Algorithm | 9 digits | 1K digits | 10K digits | 100K digits | 1M digits | 10M digits |
|---|---|---|---|---|---|---|
| Leibniz | 28.568 s | - | - | - | - | - |
| Bailey-Borwein-Plouffe | - | 2.5 ms | 660.3 ms | 208.9 s | - | - |
| Spigot Gosper | - | 3.1 ms | 867.6 ms | 250.217 s | - | - |
| Newton | - | 5.2 ms | 43.7 ms | 1.421 s | 30.475 s | - |
| Borwein Nonic | - | 3.6 ms | 159.6 ms | 5.123 s | 68.424 s | - |
| Brent-Salamin | - | - | 3.2 ms | 70.8 ms | 1.231 s | 21.364 s |
| Gauss-Legendre | - | - | 3.5 ms | 70.6 ms | 1.229 s | 21.121 s |
| Machin-like Formula | - | 1.9 ms | 14.9 ms | 465.4 ms | 8.673 s | - |
| Chudnovsky | - | - | 5.1 ms | 369.2 ms | 38.22 s | - |
| Chudnovsky Binary Splitting | - | - | 2.0 ms | 23.5 ms | 434.1 ms | 7.9 s |
| Chudnovsky Binary Splitting Parallelized | - | - | - | 19.9 ms | 293.7 ms | 4.628 s |
| Algorithm | 100M digits |
|---|---|
| Chudnovsky Binary Splitting | 109.58 s |
| Chudnovsky Binary Splitting Parallelized | 48.739 s |
| y-cruncher (single-thread) | 21.908 s |
| y-cruncher (multi-thread) | 5.354 s |
Notes
- Parallel version of Chudnovsky Binary Splitting uses half of the CPU cores available on the system.
- Use
Integerfor variables when possible. This boosts performance significantly.
Reference
- https://www.hvks.com/Numerical/Downloads/HVE%20Practical%20implementation%20of%20PI%20Algorithms.pdf
- https://www.craig-wood.com/nick/articles/pi-chudnovsky
- https://github.com/Pencilcaseman/iron_pi
- https://yamakuramun.info/2024/05/26/686/
- https://qiita.com/yonaka15/items/992b3306106c150f36c6
- https://github.com/elkasztano/piday25
- https://www.numberworld.org/y-cruncher/
Metadata
Release files for calc-pi 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| calc_pi-0.1.0-py3-none-manylinux_2_34_x86_64.whl | Python 3 | none | Linux glibc 2.34+ x86-64 | Details for calc_pi-0.1.0-py3-none-manylinux_2_34_x86_64.whl |
| calc_pi-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl | CPython 3.13 | CPython 3.13 | Linux glibc 2.34+ x86-64 | Details for calc_pi-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl |
| calc_pi-0.1.0-cp312-cp312-win_amd64.whl | CPython 3.12 | CPython 3.12 | Windows x86-64 | Details for calc_pi-0.1.0-cp312-cp312-win_amd64.whl |
Total release size: 1.5 MB
Release files / calc_pi-0.1.0-py3-none-manylinux_2_34_x86_64.whl
| Download URL | calc_pi-0.1.0-py3-none-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 581.1 kB |
| Tags | Linux glibc 2.34+ x86-64 Python 3 |
|
SHA-256 checksum How to use checksums |
41402cd05d680faf71d1d25071b8ab036c5eae3eb496f7521b6a2c4372c05255
|
|
BLAKE2b-256 checksum How to use checksums |
46dfb69a0e08db5a8e4a570b0517a2b36ac86aba3c7967556c4933896d730052
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.1
|
Release files / calc_pi-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl
| Download URL | calc_pi-0.1.0-cp313-cp313-manylinux_2_34_x86_64.whl |
|---|---|
| Size | 480.9 kB |
| Tags | CPython 3.13 Linux glibc 2.34+ x86-64 |
|
SHA-256 checksum How to use checksums |
a4b9b653a27293833f918b33b2f574c5dd73f082c24dfb616062e252fed8ded7
|
|
BLAKE2b-256 checksum How to use checksums |
734b6491d08b3720d1dc42996a42bf3b623cc8460469ddf4721da1d5cffb0e64
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.1
|
Release files / calc_pi-0.1.0-cp312-cp312-win_amd64.whl
| Download URL | calc_pi-0.1.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 470.5 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
ed16b52736bffdcb95766676a6d34ec00189cbfe399a762cfff33860731ed62c
|
|
BLAKE2b-256 checksum How to use checksums |
f53b08ad265f79c315a3dd879bafbe04bc40d64ec388a2e3ca0082fd2de858b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
maturin/1.9.1
|
Security for calc-pi 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.