Expand description
Number theory: primality, factorization, divisors, modular arithmetic. Number theory: primality testing, integer factorization, divisors, modular arithmetic, quadratic residues, discrete logarithms, prime counting, continued fractions and classical integer sequences.
Unified API — every function accepts arbitrary-precision integers via
impl Into<BigInt>. Small values that fit in i64/u64 automatically
take an optimised machine-word fast path; larger values use BigInt
arithmetic.
§Primality
isprime is deterministic for n < 3.3·10²⁴ (Miller–Rabin with
the first 13 prime bases) and uses the Baillie–PSW test (strong Fermat
base 2 + strong Lucas) beyond that. BPSW has no known
counterexamples and none exist below 2⁶⁴ (exhaustively verified), but
it is not proven deterministic for arbitrary n. is_probable_prime
exposes a plain randomized Miller–Rabin test with a caller-chosen round
count.
§Factorization
factorint combines trial division by all primes below 2¹⁶, a
perfect-power check, Pollard–Brent rho (machine words for n < 2⁶⁴),
and Lenstra’s elliptic-curve method (Montgomery curves, stage 1) for
larger composites. Every factor returned is prime (verified with
isprime); the algorithm never returns a composite as a “prime” —
for pathological inputs it may simply take a long time.
§Examples
use symplex::ntheory::{factorint, isprime, gcd, nextprime};
use num_bigint::BigInt;
// Same function works for i64 …
assert!(isprime(104729));
// … and for BigInt
let m61 = BigInt::from(2u64.pow(61) - 1);
assert!(isprime(m61));
// 2^64 + 1 = 274177 × 67280421310721
let f = factorint(BigInt::from(2u128.pow(64) + 1));
assert_eq!(f[0].0, BigInt::from(274177u64));
assert_eq!(f[1].0, BigInt::from(67280421310721u64));Structs§
- Partition
Iter - Lazy iterator over the integer partitions of
nin reverse lexicographic order (each partition is a non-increasingVec<u64>).
Functions§
- bell
- Bell number
Bₙ: the number of set partitions of ann-element set (1, 1, 2, 5, 15, 52, 203, …). - bernoulli
- Bernoulli number
Bₙas an exact rational, with the conventionB₁ = −1/2. ReturnsNonefor negativenorntoo large to fit inusize. - binomial
- Binomial coefficient
C(n, k)for integern(any sign) andk ≥ 0. - binomial_
coefficients - The binomial coefficients of
(x + y)ⁿkeyed by exponent pair (SymPybinomial_coefficients, which returns the dictionary{(k₁, k₂): C(n, k₁)}withk₁ + k₂ = n). Returned as pairs sorted byk₁. - binomial_
coefficients_ list - Row
nof Pascal’s triangle,[C(n,0), C(n,1), …, C(n,n)](SymPybinomial_coefficients_list). - carmichael_
lambda - Carmichael’s function λ(n): the exponent of the multiplicative group
(ℤ/nℤ)×, i.e. the smallestmwithaᵐ ≡ 1 (mod n)for allacoprime ton. Returns 0 forn ≤ 0. - catalan
- Catalan number
Cₙ = (2n)! / ((n+1)! n!)(1, 1, 2, 5, 14, 42, …). - continued_
fraction - Simple continued fraction
[a₀; a₁, a₂, …]of a rational number. - continued_
fraction_ convergents - Convergents
hₙ/kₙof a continued fraction[a₀; a₁, a₂, …]. - continued_
fraction_ periodic - Periodic continued fraction of
√dfor a non-negative integerd: returns(non_periodic, periodic)so that√d = [a₀; (a₁, …, aₖ) repeating]. - continued_
fraction_ reduce - The rational number with the finite simple continued fraction
[a₀; a₁, …, aₙ](SymPycontinued_fraction_reduceon a plain list). - continued_
fraction_ reduce_ periodic - The quadratic irrational with the periodic continued fraction
[pre₀; pre₁, …, (period₀, …, periodₖ) repeating](SymPycontinued_fraction_reduce([a₀, …, [b₀, …]])), returned as the integer triple(p, q, d)meaning(p + √d) / q. - continued_
fraction_ reduce_ periodic_ ex continued_fraction_reduce_periodicas a symbolic expression(p + √d)/qinctx, which canonicalises it (√8/2becomes√2).- crt
- Chinese Remainder Theorem (BigInt version).
- crt_i64
- Chinese Remainder Theorem — convenience wrapper for
i64slices. - derangements
- Number of derangements
!n(permutations with no fixed point):1, 0, 1, 2, 9, 44, 265, …. - digits
- Digits of
|n|in the given base, most significant first.digits(0, b)is[0]. - discrete_
log - Discrete logarithm: the smallest
x ≥ 0withaˣ ≡ b (mod n). - divisor_
count - Returns the number of positive divisors of
|n|. - divisor_
sigma - Divisor function
σₖ(n) = Σ_{d | n} dᵏover the positive divisors of|n|.σ₀counts divisors,σ₁sums them. Returns0forn = 0. - divisor_
sum - Returns the sum of all positive divisors of
|n|. - divisors
- Returns all positive divisors of
|n|in ascending order. - egyptian_
fraction - Greedy (Fibonacci–Sylvester) Egyptian fraction: denominators
d₁ < d₂ < …of distinct unit fractions summing tor. - euler_
number - Euler (secant) number
Eₙ:E₀ = 1, E₂ = −1, E₄ = 5, E₆ = −61, …; odd indices are zero. ReturnsNonefor negativen. - factorint
- Factorize an integer into prime factors.
- factorint_
bounded - Bounded partial factorisation for radical extraction.
- fibonacci
- Fibonacci number
Fₙ(F₀ = 0, F₁ = 1), extended to negative indices byF₋ₙ = (−1)ⁿ⁺¹ Fₙ. Fast doubling,O(log n)big-integer multiplications. - gcd
- Greatest common divisor of two integers.
- gcd_
many - Greatest common divisor of a list of integers (always
≥ 0). - gcdex
- Extended GCD:
(g, x, y)witha·x + b·y = g = gcd(a, b) ≥ 0. - harmonic
- Harmonic number
Hₙ = 1 + 1/2 + … + 1/nas an exact rational (H₀ = 0). NegativenreturnsNone. - igcd
gcd_manyfor any integer type convertible toBigInt(i64,u64,i128,BigInt, …).- ilcm
lcm_manyfor any integer type convertible toBigInt.- iroot
- Integer
k-th root: the largestrwithrᵏ ≤ n. - is_
abundant - Is
nabundant (σ(n) > 2n)? Requiresn ≥ 1. - is_
amicable - Are
aandban amicable pair (SymPyis_amicable)? That is,a ≠ b, both positive, and each is the sum of the proper divisors of the other:σ(a) − a = bandσ(b) − b = a. - is_
carmichael - Is
na Carmichael number (SymPyis_carmichael)? Korselt’s criterion:nis composite, odd, square-free, and(p − 1) | (n − 1)for every primep | n. The smallest is561 = 3·11·17. - is_
coprime - Returns
trueifaandbare coprime (gcd = 1). - is_
deficient - Is
ndeficient (σ(n) < 2n)? Requiresn ≥ 1. - is_
mersenne_ prime - Lucas–Lehmer test: is the Mersenne number
2ᵖ − 1prime? - is_
nthpow_ residue - Does
xⁿ ≡ a (mod m)have a solution? (SymPyis_nthpow_residue.) - is_
palindromic - Is
|n|a palindrome in the given base? Bases below 2 returnfalse. - is_
perfect - Is
nperfect (σ(n) = 2n)? Requiresn ≥ 1. - is_
perfect_ power - Is
na perfect powerbᵏwithk ≥ 2? - is_
primitive_ root - Is
ga primitive root modulon(a generator of(ℤ/nℤ)×)? - is_
probable_ prime - Randomized Miller–Rabin probable-prime test with
roundsrandom bases (plus base 2). - is_
quad_ residue - Is
aa quadratic residue modulon(i.e. doesx² ≡ a (mod n)have a solution)? Works for any modulusn ≥ 1by solving the congruence. - is_
square - Returns
trueifnis a perfect square. - isprime
- Primality test for any integer.
- isqrt
- Integer square root: the largest
ssuch thats² ≤ n. - jacobi_
symbol - Jacobi symbol
(a/n)for odd positiven. - kronecker_
symbol - Kronecker symbol
(a/n), defined for all integersn. - lcm
- Least common multiple of two integers.
- lcm_
many - Least common multiple of a list of integers (always
≥ 0). - legendre_
symbol - Legendre symbol
(a/p)for odd primep. - lucas
- Lucas number
Lₙ(L₀ = 2, L₁ = 1), extended to negative indices byL₋ₙ = (−1)ⁿ Lₙ. - mobius
- Möbius function μ(n).
- mod_
inverse - Modular inverse of
amodulon, if it exists. - mod_pow
- Modular exponentiation:
base^exp mod modulus. - multiplicative_
order - Alias for
n_order. - multiplicity
- The multiplicity of
pinn: the largestkwithpᵏ | n(SymPymultiplicity). Works on absolute values; returns0when|p| ≤ 1orn = 0(SymPy raises there — the multiplicity of0is infinite and±1divides everything). - n_order
- Multiplicative order of
amodulon: the smallestk ≥ 1withaᵏ ≡ 1 (mod n). ReturnsNoneifn < 1orgcd(a, n) ≠ 1. - nextprime
- Returns the smallest prime strictly greater than
n. - npartitions
- Alias for
partition_count: the number of integer partitionsp(n). - nthroot_
mod - Every solution
x ∈ [0, m)ofxⁿ ≡ a (mod m)(SymPynthroot_mod). - partitions
- All integer partitions of
n, lazily, in reverse lexicographic order ([n]first,[1, 1, …, 1]last). - perfect_
power - Perfect-power decomposition:
Some((b, k))withbᵏ = n,k ≥ 2maximal andbnot itself a perfect power. Negativenare handled with oddk. ReturnsNonefor|n| < 2and for non-powers. - polynomial_
congruence - Roots in
[0, m)of an integer polynomial modulom(SymPypolynomial_congruence). - prevprime
- Returns the largest prime strictly less than
n, orNoneif no such prime exists (i.e.n ≤ 2). - prime
- The
n-th prime (1-indexed:prime(1) = 2). - primenu
ν(n): the number of distinct prime factors of|n|(SymPyprimenu).0forn ∈ {−1, 0, 1}.- primeomega
Ω(n): the number of prime factors of|n|counted with multiplicity (SymPyprimeomega).0forn ∈ {−1, 0, 1}.- primepi
- Prime-counting function
π(n): the number of primes≤ n. - primerange
- All primes in the half-open range
[a, b), in increasing order. - primes_
up_ to - Returns all primes up to (and including)
limitusing the Sieve of Eratosthenes. - primitive_
root - Smallest primitive root modulo
n, if the group(ℤ/nℤ)×is cyclic (n ∈ {1, 2, 4, pᵏ, 2pᵏ});Noneotherwise. - primorial
- The product of the first
nprimes,pₙ#(SymPyprimorial(n), i.e.nth=True).primorial(0) = 1(SymPy rejectsn < 1). - primorial_
up_ to - The product of all primes
≤ n(SymPyprimorial(n, nth=False)).1forn < 2. - quadratic_
residues - The quadratic residues modulo
n: the sorted distinct values ofx² mod nfor0 ≤ x < n(SymPyquadratic_residues). Includes0. - rational_
lcm_ of_ denominators - Least common multiple of the denominators of a list of rationals — the factor that clears all denominators at once.
- sqrt_
mod - A square root of
amodulon, if one exists: the smallest non-negative solution ofx² ≡ a (mod n). - sqrt_
mod_ all - All solutions
x ∈ [0, n)ofx² ≡ a (mod n), sorted. - totient
- Euler’s totient function φ(n).