numbers_rus
Number-theory primitives and exact arithmetic for Rust — built for competitive programming, teaching, and recreational math.
A curated, well-documented toolkit for the discrete-math corner of numerics. Narrow on purpose: you get fast, correct implementations of the primitives that actually show up in contests, classrooms, and Project Euler, without decoding a meta-crate's trait hierarchy first.
Three pillars:
- Number theory. Deterministic Miller-Rabin primality for the full
u64range, the Sieve of Eratosthenes, wheel-factored prime factorization, GCD/LCM via Stein's binary algorithm, modular exponentiation and inversion, Euler's totient, the Chinese Remainder Theorem, and divisor functions. - Exact arithmetic. Generic
Rational<T>andComplex<T>with the operator overloads you'd expect, a trimmedPolynomial<T>with Horner evaluation and formal differentiation, and typedEquation<T>objects that don't silently hide zero-valued answers. - Descriptive statistics.
mean,median,variance,std_dev,quartiles,iqr,mode— everything over&[T], returning numbers instead ofStrings.
Is this for you?
Reach for numbers_rus when you're:
- Solving Project Euler, Codeforces, Advent of Code, or other contest-style problems in Rust and tired of re-rolling primality and modular arithmetic.
- Teaching or studying discrete math, intro number theory, or crypto fundamentals and want a readable Rust companion to the textbook.
- Prototyping a toy RSA, Diffie-Hellman, Shamir secret sharing, or other classroom-scale crypto demo.
- Building a small CAS, a constant-folder, or a units library that needs exact rationals, complex numbers, or polynomial evaluation.
- Exploring recreational math — perfect numbers, amicable pairs, aliquot sequences, OEIS — and want the primitives out of the box.
Reach for something else when you need:
- Linear algebra, matrices, SIMD numerics →
nalgebra,ndarray,faer. - DataFrames or tabular data →
polars. - Production statistics (distributions, hypothesis tests, regression) →
statrs. - Arbitrary-precision or constant-time cryptography →
num-bigint+rug,rsa,k256.numbers_rusisu64-ceiling and not hardened against timing attacks. - Floating-point-heavy scientific computing → the
nalgebra/ndarrayecosystem pluslibm.
Install
[]
= "1.0"
Optional features:
= { = "1.0", = ["serde"] }
Five-minute tour
use ;
use Rational;
use Complex;
use ;
use stats;
// ─── Number theory ──────────────────────────────────────────────────────────
assert!;
assert!; // Mersenne M_61
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert!;
assert_eq!;
assert_eq!;
// ─── Exact arithmetic ───────────────────────────────────────────────────────
let r = new + new;
assert_eq!;
let z = new * new;
assert_eq!;
let p = new; // 3x³ - 2x + 5
assert_eq!;
assert_eq!;
// ─── Equations that don't lie about zero ────────────────────────────────────
let mut eq = new;
assert_eq!; // zero is a real answer, not "not computed"
// ─── Stats ──────────────────────────────────────────────────────────────────
let xs = ;
assert_eq!;
assert_eq!;
Module map
| Module | What's inside |
|---|---|
integers::primes |
is_prime, sieve, next_prime, nth_prime, prime_factorize |
integers::arith |
gcd, lcm, extended_gcd, mod_pow, mod_inverse, chinese_remainder, euler_totient |
integers::properties |
is_even, is_odd, is_perfect_square, is_perfect_cube, perfect_power, divisors, divisor_count, divisor_sum, is_perfect_number |
integers::sequences |
factorial, fibonacci, triangular |
rational::Rational |
Generic auto-reducing rational over a signed integer |
complex::Complex |
Generic complex number |
equation::Equation |
Typed binary equation with an Op enum |
equation::Polynomial |
Single-variable polynomial with Horner eval and differentiation |
stats |
mean, median, variance, std_dev, range, quartiles, iqr, mode, sum, product |
Examples
Testing
Property tests (in tests/properties.rs, driven by proptest) verify:
gcd(a, b) · lcm(a, b) = a · b- Miller-Rabin ≡ trial division on
[0, 20 000] - Prime factorization round-trips
- Modular inverses actually invert
Rationalequality under rescaling- Horner evaluation ≡ the naive power-sum form
- The Fibonacci recurrence
Migrating from 0.2
1.0 is a substantial reshape. The complete rename table and removal list is in CHANGELOG.md. The ones you're most likely to hit:
| Before (0.2) | After (1.0) |
|---|---|
solve::equation::Equation::get_sol |
equation::Equation::solve |
solve::equation::Equation::new(a, b, '+') |
equation::Equation::new(a, b, Op::Add) |
solve::complex_float_equations, solve::rational_integer_equation, … |
equation::Equation<T> (one generic type) |
integers::base::is_prime |
integers::primes::is_prime (now Miller-Rabin) |
integers::base::factorial (recursive) |
integers::sequences::factorial → Option<u128> |
integers::base::fibonacci (recursive) |
integers::sequences::fibonacci → Option<u128> |
integers::base::is_perfect_power (buggy) |
integers::properties::perfect_power |
numbers::complex_floats::Complex |
complex::Complex<f64> |
integers::complex_integers::Complex |
complex::Complex<i64> |
rational::rational_integer::Rational, rational::rational_float::Rational |
rational::Rational<T> |
single::single_vector::vector_mean |
stats::mean (returns f64) |
single::single_vector::vector_variance |
stats::variance (returns f64, not String) |
vector::vector::vector_add |
use Vec iterators: `a.iter().zip(&b).map( |
structures::dataframe::DataFrame |
removed (use polars or ndarray) |
Roadmap
The 1.0 surface is stable. Targeted additions for the 1.x series, prioritised around the contest / teaching / recreational use cases:
- Pollard ρ factorization for numbers beyond the wheel-division sweet spot (the contest killer for large semiprimes).
- Segmented Sieve of Eratosthenes for prime enumeration past ~10⁸.
- A
bigintfeature forwardingRational<BigInt>andComplex<BigInt>, unlocking Project Euler problems that overflowu64. - Rational and polynomial GCD, squarefree factorization, and rational polynomial roots — the next layer of CAS-style primitives.
#![no_std]support for the number-theory core (WASM and embedded demos).
Breaking additions wait for 2.0.
Contributing
Issues and pull requests welcome. Please keep changes focused — a new algorithm with a correctness test and a property test carries a lot more weight than a formatting pass.
License
Dual-licensed under MIT or Apache-2.0, at your option.