compute-pi

Crates.iocompute-pi
lib.rscompute-pi
version1.0.0
sourcesrc
created_at2024-03-14 17:47:36.321692
updated_at2024-08-24 18:44:40.089849
descriptionA Rust library designed for computing pi to millions of digits.
homepage
repositoryhttps://github.com/sekika/compute-pi
max_upload_size
id1173611
size12,825
Katsutoshi Seki (関 勝寿) (sekika)

documentation

README

compute-pi

Compute-pi calculates the value of pi to an arbitrary number of digits using the Gauss–Legendre algorithm. It computes 1 million digits within a couple of seconds on your PC.

Installation

You can include this crate in your Cargo.toml file as follows:

[dependencies]
compute-pi = "1.0"

Usage

To use the compute_pi_str function in your Rust code, add the following to your crate root:

use compute_pi::compute_pi_str;

fn main() {
    // Specify the number of digits of Pi you want to compute
    let digits = 100;

    // Compute Pi
    let pi = compute_pi_str(digits);

    // Print calculated decimal
    println!("Pi to {} decimal places: {}", digits, pi);
}

Command Line Usage

You can also use the compute-pi command from the command line. After installing the crate with

cargo install compute-pi

run the following command:

compute-pi <digits>

Replace <digits> with the number of digits of Pi you want to compute. For example:

compute-pi 100

This will print the value of Pi to 100 decimal places.

Performance

On a MacBook Air (Apple M1, 16 GB), pi to 1 million digits was computed in 1.5 seconds, and to 320 million digits in 24 minutes, but the calculation did not complete within 10 hours for 330 million digits. Similarly, on a Mac mini (Apple M1, 16 GB), pi to 320 million digits was computed, but the calculation did not complete within 10 hours for 330 million digits. It is presumed that the calculation is taking a long time due to memory swapping, as it does not end in a panic due to memory allocation failure but instead continues indefinitely. Since both machines yielded the same result, it is considered that 320 million digits is the maximum number of digits that can be computed using compute-pi with 16 GB of memory. Another limitation is that the digit cannot exceed 1,292,913,983 due to the precision of rug::Float being defined as u32. The computed result of pi to 320 million digits using compute-pi was confirmed to match exactly with the results obtained using y-cruncher and the Chudnovsky algorithm.

License

This crate is licensed under the MIT license.

Commit count: 13

cargo fmt