No description
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-07-20 16:31:21 +07:00
src init with some implementations 2026-07-20 16:31:21 +07:00
Cargo.lock init with some implementations 2026-07-20 16:31:21 +07:00
Cargo.toml init with some implementations 2026-07-20 16:31:21 +07:00
README.md init with some implementations 2026-07-20 16:31:21 +07:00

number-crunch — High-Performance Mathematical Constant Computation

A parallel Rust CLI that computes digits of mathematical constants (π, e) to arbitrary precision. Uses GMP (via rug) for arbitrary-precision integer arithmetic and rayon for multi-threaded parallelism.

  • π: Chudnovsky algorithm with binary splitting — O(N log³ N)
  • e: Binary splitting on exponential series — O(N log² N)

Outputs digits in base 10 or base 16 (hex), with or without the integer prefix.

Quick Start

Prerequisites

  • Rust 1.80+ (edition 2024)
  • GMP (via gmp-mpfr-sys — built automatically by rug, but needs libgmp-dev or equivalent)
# macOS
brew install gmp

# Debian/Ubuntu
sudo apt install libgmp-dev

# Fedora
sudo dnf install gmp-devel

Build & Run

cargo build --release

# Compute π
number-crunch pi -n 100                  # stdout
number-crunch pi -n 100 -o pi.txt        # file
number-crunch pi -n 100 --full           # with "3." prefix

# Compute e
number-crunch e -n 100                   # stdout
number-crunch e -n 100 -o e.txt          # file
number-crunch e -n 100 --full            # with "2." prefix

# Hex output
number-crunch pi -n 32 -b 16 -o pi_hex.txt

# Use 4 threads
number-crunch pi -n 1000000 -o pi_1M.txt -t 4

# Verbose progress
number-crunch pi -n 1000000 -o pi_1M.txt --log

Usage

number-crunch [OPTIONS] -n <DIGITS> <CONSTANT>

Arguments:
  <CONSTANT>  pi | e

Options:
  -n <DIGITS>                    Number of digits to compute
  -o <OUTPUT>                    Output file path (prints to stdout if omitted)
  -b <BASE>                      Output base: 10 or 16 [default: 10]
  -t <THREADS>                   Number of threads (default: auto-detect)
      --full                     Include the integer part ("3." or "2.")
      --max-memory <MAX_MEMORY>  Max memory in GB (advisory)
      --log                      Print progress during computation
  -h, --help                     Print help

Algorithms

π — Chudnovsky Algorithm

The Chudnovsky algorithm computes π via a rapidly converging Ramanujan-type series:

π = 426880 · √10005 / K

K = Σ_{k=0}^{∞} (-1)^k · (6k)! · (13591409 + 545140134k)
     / ((3k)! · (k!)^3 · 640320^(3k))

Each term contributes ~14.18 decimal digits.

Complexity: O(N log³ N) with FFT multiplication.

e — Exponential Series

Computes e via binary splitting of the Taylor series:

e = Σ_{k=0}^{∞} 1/k!

The binary splitting computes P/Q = e 1, then e = (P + Q) / Q.

Complexity: O(N log² N) with FFT multiplication — faster than π.

Binary Splitting (Both Constants)

Instead of summing terms one by one, binary splitting organizes the computation into a tree:

  1. Split the term range [l, r) into two halves.
  2. Recurse on each half (parallel at the top level).
  3. Merge the results using the constant-specific formulas.

This enables trivial parallelization via rayon.

Final Assembly

After binary splitting produces the intermediate values, the final result is assembled using pure integer arithmetic — no floating-point rounding issues.

Architecture

src/
├── main.rs              CLI argument parsing, threading, orchestration
├── constant/
│   ├── mod.rs           Re-exports
│   ├── pi.rs            Chudnovsky algorithm + binary splitting
│   └── e.rs             Exponential series binary splitting
├── convert.rs           Hex and decimal output extraction
└── output.rs            File writing

Performance

Approximate timings on Apple Silicon M-series (8 threads):

Digits π e
1,000 ~4 ms ~1 ms
10,000 ~5 ms ~2 ms
100,000 ~23 ms ~8 ms
1,000,000 ~235 ms ~60 ms
10,000,000 ~4.8 s ~1.2 s

References

License

MIT