Skip to main content

qualia_core_db/solvers/number_theory/
mod.rs

1//! **Number theory & combinatorics** (Gap analysis §3.2-NT).
2//!
3//! Primality, factorization, modular arithmetic, the classic arithmetic functions
4//! (Euler totient, Möbius, divisor sums) and combinatorics (factorials, binomials,
5//! partitions, Stirling, Catalan). Underpins the CAS, crypto, and **constructibility**
6//! (the regular-polygon decision needs Fermat-prime factorization and `φ(n)`), and is
7//! one of the bounded, high-demand gaps the computational-engine gap analysis surfaced.
8//!
9//! Exact integer arithmetic over `u64`/`i64` (with `u128` intermediates to avoid
10//! overflow in modular multiply); fail-closed via `Option`/[`NumberTheoryError`] on
11//! degenerate input. Kernel-class `Divergent` (branch-heavy) with a pure CPU path.
12//!
13//! Layers (§11): [`modular`], [`primes`], [`arithmetic_functions`], [`combinatorics`].
14
15pub mod arithmetic_functions;
16pub mod combinatorics;
17pub mod modular;
18pub mod primes;
19
20pub use arithmetic_functions::{divisor_count, divisor_sum, euler_totient, mobius};
21pub use combinatorics::{
22    binomial, catalan, factorial, partitions, stirling_first, stirling_second,
23};
24pub use modular::{extended_gcd, gcd, lcm, mod_inverse, mod_pow};
25pub use primes::{divisors, is_prime, next_prime, prime_factors};
26
27/// Fail-closed errors for number-theoretic operations.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub enum NumberTheoryError {
30    /// Input outside the function's domain (e.g. factoring 0, totient of 0).
31    OutOfDomain,
32    /// No result exists (e.g. modular inverse when gcd ≠ 1).
33    NoSolution,
34}
35
36impl core::fmt::Display for NumberTheoryError {
37    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38        match self {
39            NumberTheoryError::OutOfDomain => write!(f, "argument out of domain"),
40            NumberTheoryError::NoSolution => write!(f, "no solution exists"),
41        }
42    }
43}
44impl std::error::Error for NumberTheoryError {}