A powerful, fast Rust implementation of Euler's Totient function φ(n), with optional parallel processing support to handle large numbers more efficiently.
- Euler's Totient function calculations for arbitrary large integers
- Choose between Euclidean or Binary GCD algorithms
- Fast totient using prime factorization (if known or factorable)
- Optional parallel computation mode during brute-force counting
- Compatible with modern Rust (2021 Edition)
- Clean CLI interface
For a positive integer n, Euler's Totient function φ(n) counts how many integers k ≤ n are coprime with n (no common factors besides 1).
Build with:
cargo build --releaseThis project includes an optional parallel backend for the brute-force totient count.
Parallel counting significantly speeds up computations on multi-core machines ardalis.com, developer.hashicorp.com.
To enable parallel computations, build with:
cargo build --release --features parallelor run directly:
cargo run --release --features parallel -- <number> <algorithm>This activates a multithreaded engine (using Rayon) that divides the counting across CPU cores.
Tip: Parallel build is highly recommended for larger inputs as it cuts down execution time drastically (sometimes up to 2-5x faster depending on hardware and input size).
cargo run --release [--features parallel] -- <number> <algorithm>where <algorithm> is one of:
euclidean- brute-force counting using Euclidean GCDbinary- brute-force counting using the Binary GCD algorithmfactor- use prime factorization formula instead of counting (fastest if factors known)
Example:
cargo run --release --features parallel -- 12345 binaryor
cargo run --release -- 987654 factorThe parallel feature speeds up brute-force totient counting by dividing the range [1, n-1] into parts and processing them simultaneously on all available CPU cores. This approach is similar to how other build or compute tools use task-based concurrency to reduce total execution time ardalis.com, developer.hashicorp.com.
If you work with large integers or want to save time, enabling --features parallel is highly recommended.
| Cargo feature | Description | Default? |
|---|---|---|
parallel |
Enables Rayon parallel multithreaded counting | No |
- 🚀 Enable the
parallelfeature at build time (--features parallel) to significantly accelerate totient calculations - Supports brute-force (Euclidean or Binary GCD)
- Supports fast factorization-based method
- Handles arbitrary precision integers
MIT OR Apache-2.0
Enjoy fast number theory in Rust!