Congruences

Congruences

Arithmetic modulo m: when a·x ≡ b has solutions and how many, gluing congruences together with the Chinese remainder theorem, roots of polynomials modulo any m, and the cyclic groups that make discrete logarithms possible.

Linear congruences

import { solveLinear, crt, rootsMod, hensel, primitiveRootMod, discreteLog } from "@ratmath/numtheory";

solveLinear(6, 4, 10);   // { solutions: [4n, 9n], modulus: 10n, g: 2n, reduced: { x: 4n, modulus: 5n } }
solveLinear(6, 5, 10);   // { solutions: [], g: 2n, reason: "gcd(6, 10) = 2 does not divide 5" }

ax≡b(modm)ax \equiv b \pmod m is solvable exactly when g=gcd⁡(a,m)g = \gcd(a, m) divides bb. Then dividing through gives (a/g)x≡b/g(modm/g)(a/g)x \equiv b/g \pmod{m/g}, where a/ga/g is invertible, so there is one solution modulo m/gm/g and gg of them modulo mm. The inverse comes from the extended Euclidean algorithm: as+mt=gas + mt = g.

The Chinese remainder theorem, for any moduli

crt([2, 3, 2], [3, 5, 7]);   // { x: 23n, modulus: 105n }: Sunzi's problem
crt([1, 3], [4, 6]);          // { x: 9n, modulus: 12n }: moduli need not be coprime
crt([1, 2], [4, 6]);          // null: x odd and x even at once
solveLinearSystem([[2, 1, 5], [3, 2, 7]]);   // 2x ≡ 1 (mod 5), 3x ≡ 2 (mod 7): { x: 3n, modulus: 35n }

Two congruences x≡r1(modm1)x \equiv r_1 \pmod{m_1}, x≡r2(modm2)x \equiv r_2 \pmod{m_2} are compatible exactly when r1≡r2(modgcd⁡(m1,m2))r_1 \equiv r_2 \pmod{\gcd(m_1, m_2)}, and then they have one solution modulo lcm⁡(m1,m2)\operatorname{lcm}(m_1, m_2). Combining two at a time handles any list.

Polynomial congruences and Hensel's lemma

To solve f(x)≡0(modm)f(x) \equiv 0 \pmod m, solve modulo each prime power in mm and glue with the Chinese remainder theorem. Modulo pp, try every residue (or factor, for large pp). To go from pkp^k to pk+1p^{k+1}: if f(r)≡0(modpk)f(r) \equiv 0 \pmod{p^k} and f′(r)≢0(modp)f'(r) \not\equiv 0 \pmod p, then

r′=r−f(r)f′(r)(modpk+1) r' = r - \frac{f(r)}{f'(r)} \pmod{p^{k+1}}

is the unique lift, Newton's method with pp-adic convergence. When f′(r)≡0f'(r) \equiv 0 a root may lift to many or none; rootsMod then tries every lift.

rootsMod([1, 0, 1], 65);       // x² + 1 ≡ 0 (mod 65): [8n, 18n, 47n, 57n]
rootsMod([-1, 0, 1], 8);       // x² ≡ 1 (mod 8): [1n, 3n, 5n, 7n], four roots, not two
hensel([1, 0, 1], 2, 5, 4);    // the root ≡ 2 of x² + 1 lifted to mod 625: 182n

Primitive roots and indices

The units modulo nn form a cyclic group exactly when nn is 1,2,4,pk1, 2, 4, p^k or 2pk2p^k for an odd prime pp (Gauss). A generator is a primitive root; gg is one exactly when gφ(n)/q≢1g^{\varphi(n)/q} \not\equiv 1 for every prime q∣φ(n)q \mid \varphi(n). Then each unit hh is gxg^x for one xx modulo φ(n)\varphi(n), its index or discrete logarithm, found by baby-step giant-step in about n\sqrt n steps.

primitiveRootMod(25);    // 2n
primitiveRootMod(8);     // null: (Z/8)ˣ is {1, 3, 5, 7}, every element squares to 1
order(10, 13);           // 6n: 1/13 has a period of 6 decimal digits
discreteLog(3, 13, 17);  // 4n: 3⁴ = 81 ≡ 13 (mod 17)
discreteLog(2, 3, 7);    // null: 2 generates only {1, 2, 4}

Factorials: Legendre's formula

The multiples of pp up to nn each give one factor pp to n!n!, the multiples of p2p^2 one more, and so on, so vp(n!)=⌊n/p⌋+⌊n/p2⌋+⋯v_p(n!) = \lfloor n/p\rfloor + \lfloor n/p^2\rfloor + \cdots, which also equals (n−sp(n))/(p−1)(n - s_p(n))/(p - 1) where sp(n)s_p(n) is the sum of the base-pp digits of nn. factorialValuation(n, b) takes any b≥2b \ge 2: for b=∏peb = \prod p^{e} the answer is the smallest ⌊vp(n!)/e⌋\lfloor v_p(n!)/e\rfloor, the number of zeros that end n!n! in base bb. It never builds n!n!.

factorialValuation(100, 2);     // 97 = 50 + 25 + 12 + 6 + 3 + 1
factorialValuation(100, 10);    // 24: 100! ends in 24 zeros
factorialValuation(10n ** 18n, 5);   // 249999999999999998n: a BigInt once it is too large for a plain number
Try itEdit the code and run it with @ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs