Expand description
Core integer algorithms: GCD, LCM, modular arithmetic, root-finding, prime utilities, combinatorics, digit operations, and special numbers.
These are the fundamental building blocks used across number theory,
combinatorics, and cryptography. Canonical home for all integer math —
algorithms.rs re-exports from here.
Functions§
- bell_
number n-th Bell number: total partitions of a set of n elements.- bezout_
coefficients - Bezout coefficients: returns
(x, y)such thata*x + b*y = gcd(a, b). - binomial
- Binomial coefficient
n choose k.k > nreturns 0. - catalan_
number n-th Catalan number:C(n) = (1/(n+1)) * C(2n, n).- checked_
binomial - Binomial coefficient
n choose k, returningNoneon overflow.k > nreturnsSome(0). - checked_
factorial n!asu128, returningNoneon overflow. Exact forn <= 34; returnsNoneforn >= 35.- chinese_
remainder - Chinese Remainder Theorem solver.
- digit_
count - Number of decimal digits in
n. - digit_
count_ base - Number of digits of
nin the givenbase(2-36).digit_count_base(0, 10)returns 1. - digit_
sum - Sum of decimal digits of
n. - divisor_
count - Number of divisors:
d(n)ortau(n). - divisor_
sum - Sum of divisors:
sigma(n). - double_
factorial - Double factorial:
n!! = n * (n-2) * (n-4) * ... * 1(or 2). - euler_
phi - Euler’s totient function phi(n): count of integers <= n that are coprime to n.
- extended_
gcd - Extended Euclidean algorithm: returns
(g, x, y)such thata*x + b*y = g = gcd(|a|, |b|).gis always non-negative. - factorial
n!asu128(exact up ton = 34; larger wraps silently).- fermat_
number n-th Fermat number:2^(2^n) + 1.- fibonacci
n-th Fibonacci number (F_0 = 0, F_1 = 1) asu128(exact up to n = 184).- from_
base - Parse a string in the given
base(2-36) tou64. - from_
digits - Reconstruct a number from decimal digits (most significant first).
- gcd
- Greatest common divisor (Euclidean algorithm).
- gcd_n
- Greatest common divisor of
nnumbers. - is_
abundant - Check if
nis an abundant number: sum of proper divisors >n. - is_
armstrong - Check if
nis an Armstrong (Narcissistic) number:n = sum of its digits each raised to the power of the number of digits. - is_
coprime - Check if
aandbare coprime (gcd == 1). - is_cube
- Check if
nis a perfect cube:n = k^3for some integerk. - is_
deficient - Check if
nis a deficient number: sum of proper divisors <n. - is_even
- Returns
trueifnis even. - is_
harshad - Check if
nis a Harshad (Niven) number: divisible by sum of its digits. - is_odd
- Returns
trueifnis odd. - is_
palindrome - Check if
nis a decimal palindrome. - is_
palindrome_ base - Check if
nis a palindrome in the givenbase(2-36). - is_
perfect_ number - Check if
nis a perfect number: sum of its proper divisors equalsn. - is_
perfect_ power - Check if
nis a perfect power:n = a^bfor somea >= 2, b >= 2. - is_
power_ of - Check if
nis a power ofbase. - is_
power_ of_ two trueifnis a power of two (1, 2, 4, …).0is not.- is_
prime - Trial-division primality test.
- is_
prime_ miller_ rabin - Miller-Rabin probabilistic primality test.
- is_
semiprime - Check if
nis a semiprime: product of exactly two primes (not necessarily distinct). - is_
smooth - Check if
nis a smooth number: all prime factors <=bound. - is_
square - Check if
nis a perfect square. - is_
squarefree - Check if
nis squarefree: no repeated prime factors. - is_
triangular - Check if
nis a triangular number:n = k(k+1)/2. - isqrt
- Integer square root:
floor(sqrt(n)). - isqrt_
rem - Integer square root with remainder: returns
(floor(sqrt(n)), n - floor(sqrt(n))^2). - lcm
- Least common multiple. Returns 0 for
lcm(0, x). - lcm_n
- Least common multiple of
nnumbers. - liouville
- Liouville function lambda(n):
(-1)^Omega(n)where Omega(n) = total number of prime factors with multiplicity. - log2_
ceil - Ceiling integer log base 2: smallest
ksuch that2^k >= n. Returns 0 forn == 0. - log2_
floor - Floor integer log base 2: largest
ksuch that2^k <= n. Returns 0 forn == 0. - log_
base - Integer log base
base: largestksuch thatbase^k <= n. Returns 0 forn == 0orbase <= 1. - lucas_
number n-th Lucas number (L_0 = 2, L_1 = 1, L_n = L_{n-1} + L_{n-2}).- mersenne_
number n-th Mersenne number:2^n - 1.- mobius
- Mobius function mu(n):
- mod_pow
- Modular exponentiation:
base^exp mod m(exponentiation by squaring). - modular_
inverse - Modular multiplicative inverse:
a^-1 mod m. - nearest_
power_ of_ two - Nearest power of two to
n. Ties round toward even (e.g. 3 -> 4, 5 -> 4). - next_
power_ of_ two - Smallest power of two
>= n. Returns 0 forn == 0. - next_
prime - Next prime after
n. - nth_
prime - The
n-th prime (1-indexed: 1st prime = 2). - partition_
number - Number of integer partitions of
n(partition function p(n)). - pascal_
triangle - Generate the first
nrows of Pascal’s triangle. - permutation_
count - Number of permutations:
P(n, r) = n! / (n - r)!. - popcount
- Population count: number of set bits (1-bits) in
n. - prev_
prime - Largest prime strictly less than
n. - prime_
count - Count of primes <=
n(prime-counting function pi(n)). - prime_
factorization - Prime factorization of
n: returns a vector of(prime, exponent)pairs. - prime_
factors - Distinct prime factors of
n. - primorial
- Primorial: product of primes <=
p. - radical
- Radical (square-free kernel): product of distinct prime factors.
- reverse_
digits - Reverse the decimal digits of
n. - reverse_
digits_ base - Reverse the digits of
nin the givenbase(2-36). - segmented_
sieve - Segmented sieve: all primes in
[lo, hi]. - sieve_
of_ eratosthenes - All primes
<= n. - stirling_
first - Unsigned Stirling numbers of the first kind:
s(n, k). - stirling_
second - Stirling numbers of the second kind:
S(n, k). - subfactorial
- Subfactorial (derangement number):
!n = n! * sum(k=0..n) (-1)^k / k!. - sum_
of_ squares - Sum of squares without overflow:
a^2 + b^2computed inu128. - to_base
- Convert
nto a string in the givenbase(2-36). - to_
digits - Convert
nto a vector of decimal digits (most significant first). - tribonacci
n-th Tribonacci number (T_0 = 0, T_1 = 0, T_2 = 1, T_n = T_{n-1} + T_{n-2} + T_{n-3}).