Number theory: modular arithmetic, primes, factorisation and polynomials
Number theory studies the integers, and its algorithms are among the oldest and the most used. Every public-key handshake runs modular exponentiation, every hash table picks a prime, and every error-correcting code computes in a finite field. This chapter covers lib_jsl_number_theory:
- the arithmetic of remainders: gcd, inverses, powers, square roots and the Chinese remainder theorem;
- the primes: sieves, primality testing and factorisation;
- polynomials, and integers of any size held as decimal strings.
The central questions
- Exact or approximate? Number-theoretic algorithms are exact, so their failures are not rounding but overflow, infinite loops and wrong conventions. Most of the defects fixed here were of those kinds.
- Certain or probable? Miller–Rabin is probabilistic in general but deterministic for 64-bit inputs with the right bases.
- How big? Everything here works in 64 or 128 bits; beyond that, methods change (and so does their cost).
Testing against SymPy
The references come from SymPy 1.14, by book/generator/data/make_number_theory_refs.py. They include the classic strong pseudoprimes and Carmichael numbers, the factorisation of , products of two primes near , and moduli up to :
Table 22.1. Number-theory routines against SymPy.
| computation (SymPy function) | agreement |
|---|---|
Chinese remainder theorem, coprime and non-coprime moduli (crt) | true |
factorisation and totient (factorint, totient), up to 2⁶⁴ − 1 | 7 of 7 agree |
integer square roots, Newton and bisection (integer_nthroot) | true |
modular inverses, moduli up to 10³⁰ (mod_inverse) | true |
modular powers, moduli up to 10³⁰ (Python pow) | true |
primality (isprime), including strong pseudoprimes and Carmichael numbers | 13 of 13 agree |
prime counting π(10ᵏ), k = 1 … 8 (primepi) | 8 of 8 agree |
In the library
All of these are in lib_jsl_number_theory (API reference).
| You need | Use | Entry |
|---|---|---|
| A gcd, an inverse, a modular power, an exact square root, simultaneous congruences | gcd, extended_gcd, multiplicative_inverse_modulo, base_exponent_in_modulus, integer_square_root_newton, chinese_remainder | Modular arithmetic |
| Primes up to a bound or in a window; a primality test; a factorisation | sieve_of_eratosthenes, segmented_sieve, is_prime, factorize, euler_totient, divisors | Primes |
| Polynomial evaluation and roots; integers of any size | polynomial_eval, polynomial_from_roots, polynomial_roots, TextCodedInteger | Polynomials and decimal-string integers |