num_prime/lib.rs
1// #![doc = include_str!("../README.md")]
2
3//! This crate provides utilities for prime related functionalities and some basic number theoretic functions:
4//! - Primality testing
5//! - [Primality check][nt_funcs::is_prime]
6//! - [Deterministic primality check of `u64` integers][nt_funcs::is_prime64] (using a very fast hashing algorithm)
7//! - [Fermat probable prime test][PrimalityUtils::is_prp]
8//! - [Miller-rabin probable prime test][PrimalityUtils::is_sprp]
9//! - ([strong][PrimalityUtils::is_slprp]/[extra strong][PrimalityUtils::is_eslprp]) [Lucas probable prime test][PrimalityUtils::is_lprp]
10//! - [Baillie-PSW test][PrimalityTestConfig::bpsw]
11//! - [Sophie Germain safe prime test][nt_funcs::is_safe_prime]
12//! - Primes generation and indexing
13//! - [A naive implementation of the sieve of Eratosthenes][buffer::NaiveBuffer]
14//! - [Unified API to support other prime generation backends][PrimeBuffer]
15//! - [Generate random (safe) primes][traits::RandPrime]
16//! - Find [previous prime][nt_funcs::prev_prime] / [next prime][nt_funcs::next_prime]
17//! - [Integer factorization][nt_funcs::factors]
18//! - [Trial division][factor::trial_division]
19//! - [Pollard's rho algorithm][factor::pollard_rho]
20//! - [Shanks's square forms factorization (SQUFOF)][factor::squfof]
21//! - [Hart's one line algorithm][factor::one_line]
22//! - [Fast factorization of `u64` integers][nt_funcs::factorize64]
23//! - Number theoretic functions
24//! - [Prime Pi function][nt_funcs::prime_pi], its [estimation](nt_funcs::prime_pi_est), and its [bounds](nt_funcs::prime_pi_bounds)
25//! - [Nth Prime][nt_funcs::nth_prime], its [estimation](nt_funcs::nth_prime_est), and its [bounds](nt_funcs::nth_prime_bounds)
26//! - [Moebius function][nt_funcs::moebius]
27//!
28//! # Usage
29//! Most number theoretic functions can be found in [nt_funcs] module, while some
30//! of them are implemented as member function of [num_modular::ModularOps] or [PrimalityUtils].
31//!
32//! Example code for primality testing and integer factorization:
33//! ```rust
34//! use num_prime::{PrimalityTestConfig, FactorizationConfig};
35//! use num_prime::nt_funcs::{is_prime, factorize, factors};
36//!
37//! let p = 2u128.pow(89) - 1; // a prime number
38//! assert!(is_prime(&p, None).probably()); // use default primality check config
39//! assert!(is_prime(&p, Some(PrimalityTestConfig::bpsw())).probably()); // BPSW test
40//!
41//! let c = 2u128.pow(83) - 1; // a composite number
42//! assert!(!is_prime(&c, None).probably());
43//! let fac = factorize(c); // infallible factorization with default configuration
44//! assert_eq!(fac.len(), 2); // 2^83-1 = 167 * 57912614113275649087721
45//!
46//! let config = FactorizationConfig::strict();
47//! let (fac, rem) = factors(c, Some(config)); // fallible factorization with customized configuration
48//! assert!(fac.len() == 2 && rem.is_none());
49//! ```
50//!
51//! # Backends
52//! This crate is built with modular integer type and prime generation backends.
53//! Most functions support generic input types, and support for `num-bigint` is
54//! also available (it's an optional feature). To make a new integer type supported
55//! by this crate, the type has to implement [detail::PrimalityBase] and [detail::PrimalityRefBase].
56//! For prime generation, there's a builtin implementation (see [buffer] module),
57//! but you can also use other backends (such as `primal`) as long as it implements [PrimeBuffer].
58//!
59//! # Optional Features
60//! - `big-int` (default): Enable this feature to support `num-bigint::BigUint` as integer inputs.
61//! - `big-table` (default): Enable this feature to allow compiling large precomputed tables which
62//! could improve the speed of various functions with the cost of larger memory footprint.
63//!
64
65pub mod buffer;
66pub mod factor;
67pub mod nt_funcs;
68
69mod integer;
70mod mint;
71mod primality;
72mod rand;
73mod tables;
74mod traits;
75
76pub use traits::*;
77pub mod detail {
78 //! Implementation details for this crate.
79 //!
80 //! The structs and traits in this module are exposed for public use, although they are no
81 //! designed for such usage. User-friendly is not a goal and backward-compatilibity is not
82 //! strictly maintained here. Some traits in this module can be used to extend `num-prime`
83 //! with new backends.
84 pub use super::mint::{Mint, SmallMint};
85 pub use super::primality::{LucasUtils, PrimalityBase, PrimalityRefBase};
86 pub use super::tables::SMALL_PRIMES;
87}