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" }
is solvable exactly when divides . Then dividing through gives , where is invertible, so there is one solution modulo and of them modulo . The inverse comes from the extended Euclidean algorithm: .
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 , are compatible exactly when , and then they have one solution modulo . Combining two at a time handles any list.
Polynomial congruences and Hensel's lemma
To solve , solve modulo each prime power in and glue with the Chinese remainder theorem. Modulo , try every residue (or factor, for large ). To go from to : if and , then
is the unique lift, Newton's method with -adic convergence. When 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 form a cyclic group exactly when is or for an odd prime (Gauss). A generator is a primitive root; is one exactly when for every prime . Then each unit is for one modulo , its index or discrete logarithm, found by baby-step giant-step in about 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 up to each give one factor to , the multiples of one more, and so on, so , which also equals where is the sum of the base- digits of . factorialValuation(n, b) takes any : for the answer is the smallest , the number of zeros that end in base . It never builds .
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
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.