Expand description
This crate provides utilities for prime related functionalities and some basic number theoretic functions:
- Primality testing
- Primes generation and indexing
- Integer factorization
- Number theoretic functions
- Prime Pi function and its bounds
- Nth Prime and its bounds
- Moebius function
Usage
Most number theoretic functions can be found in nt_funcs module, while some of them are implemented as member function of num_modular::ModularOps or PrimalityUtils.
Example code for primality testing and integer factorization:
use num_prime::PrimalityTestConfig;
use num_prime::nt_funcs::{is_prime, factors};
let p = 2u128.pow(89) - 1; // a prime number
assert!(is_prime(&p, None).probably()); // use default primality check config
assert!(is_prime(&p, Some(PrimalityTestConfig::bpsw())).probably()); // BPSW test
let c = 2u128.pow(83) - 1; // a composite number
assert!(!is_prime(&c, None).probably());
let fac = factors(c, None); // use default factorization config
assert!(matches!(fac, Ok(f) if f.len() == 2)); // 2^83-1 = 167 * 57912614113275649087721Backends
This crate is built with modular integer type and prime generation backends.
Most functions support generic input types, and support for num-bigint is
also available (it’s an optional feature). To make a new integer type supported
by this crate, the type has to implement detail::PrimalityBase and detail::PrimalityRefBase.
For prime generation, there’s a builtin implementation (see buffer module),
but you can also use other backends (such as primal) as long as it implements PrimeBuffer.
Features
big-int: Enable this feature to supportnum-bigint::BigUintas integer inputs.big-table: Enable this feature to allow compiling large precomputed tables which could improve the performance of various functions.
Modules
Implementations and extensions for PrimeBuffer, which represents a container of primes
Some traits that can be used to extend num-prime with new backends.
Implementations for various factorization algorithms
Standalone number theoretic functions
Structs
Represents a configuration for integer factorization
Represents a configuration for a primality test
Enums
This enum describes the result of primality checks
Traits
This trait support unified bit testing for (unsigned) integers
Extension on num_integer::Roots to support perfect power check on integers
This trait implements various primality testing algorithms
This trait represents a general data structure that stores primes.
Supports random generation of primes