Packages / rupm

rs-math-number-theory 0.1.0

Number theory toolkit: gcd, lcm, extended gcd, modular power and inverse, prime sieve, Miller-Rabin for u64 and factorization, std only

Install

cargo add rs-math-number-theory --registry rupm

In Ruststudio: Packages, search for rs-math-number-theory, Install.

Owner: Rust Studio. Licence: MIT OR Apache-2.0. Total downloads: 0. Published 2026-11-23.

Versions

README

# rs-math-number-theory

Number theory toolkit: gcd, lcm, extended gcd, modular power and inverse, prime sieve, Miller-Rabin for u64 and factorization, std only

Add it with `rupm add rs-math-number-theory` (or `cargo add rs-math-number-theory --registry rupm`).

The primality test is deterministic for every `u64` (Miller-Rabin with the first twelve prime bases), and factorization uses trial division plus Pollard's rho. Results are checked against published values: strong pseudoprimes, prime counts, Project Euler 3 and textbook modular examples. Nothing panics on any input; operations that can fail return `Option`.

## Install

```
rupm add rs-math-number-theory
```

## Usage

```rust
use rs_math_number_theory as nt;

assert_eq!(nt::gcd(48, 18), 6);
assert_eq!(nt::lcm(4, 6), Some(12));
assert_eq!(nt::mod_pow(4, 13, 497), Some(445));
assert_eq!(nt::mod_inverse(17, 3120), Some(2753));
assert!(nt::is_prime(18446744073709551557));
assert_eq!(nt::factorize(600851475143), vec![(71, 1), (839, 1), (1471, 1), (6857, 1)]);
assert_eq!(nt::sieve(30), vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29]);
```

## API overview

- `gcd(a, b)`, `lcm(a, b)` (None on overflow), `ext_gcd(a, b)` returning `(g, x, y)` with `a*x + b*y = g`.
- `mod_pow(base, exp, modulus)` and `mod_inverse(a, modulus)`, both `Option` (None for modulus 0, or no inverse).
- `mod_mul(a, b, modulus)`: overflow-free modular multiplication.
- `sieve(limit)`: all primes up to and including `limit` (Eratosthenes; memory is about one byte per number).
- `is_prime(n)`: deterministic Miller-Rabin for all `u64`. `next_prime(n)`: smallest prime greater than `n`, None past the largest u64 prime.
- `factorize(n)`: prime factorization as ascending `(prime, exponent)` pairs (empty for 0 and 1). `totient(n)`: Euler phi.