The integers mod n
Clock arithmetic, taken seriously. The ring Z/n is small enough to list and rich enough to show units, zero divisors, ideals and the Chinese remainder theorem in action.
The ring Z/n
Two whole numbers are congruent modulo when they differ by a multiple of . The congruence classes can be added and multiplied, and form the ring . It is the quotient of by the ideal of multiples of , and the first example of nearly every idea in ring theory.
import { Zn } from "@ratmath/structures";
const Z = Zn(12);
Z.add(7, 8); // 3n
Z.mul(5, 7); // 11n
Z.pow(7, 100); // 1n
Z.inverse(5); // 5n, because 5·5 = 25 = 1
Z.isUnit(4); // false
Elements are BigInts in the range 0 to n − 1, so the modulus can be as large as you like. Numbers and BigInts are both accepted.
Units and zero divisors
Every nonzero class is one or the other:
The inverse comes from the extended Euclidean algorithm: if then is the inverse of . The number of units is Euler's . So is a field exactly when is prime, and for composite it is not even an integral domain.
Two more kinds of element are worth finding.
- Nilpotents, with some power equal to 0: the multiples of the product of the distinct primes dividing . There are none except 0 exactly when is square-free.
- Idempotents, with : there are of them when has distinct prime factors, and they are the key to the next section.
Z.units(); // [1n, 5n, 7n, 11n]
Z.zeroDivisors(); // [2n, 3n, 4n, 6n, 8n, 9n, 10n]
Z.nilpotents(); // [0n, 6n]
Z.idempotents(); // [0n, 1n, 4n, 9n]
Zn(7).isField; // true
Zn(30).isReduced; // true: 30 is square-free, so no nilpotents
The Chinese remainder theorem
If and have no common factor, a number modulo is the same thing as a pair: a number modulo and a number modulo .
Splitting into prime powers, is a product of rings , and everything about can be read off one prime at a time. The idempotents are the elements that are 0 or 1 in each factor.
import { crt } from "@ratmath/structures";
crt([2, 3, 2], [3, 5, 7]); // { value: 23n, modulus: 105n }: Sunzi's problem
crt([1, 3], [4, 6]); // { value: 9n, modulus: 12n }: moduli need not be coprime
crt([1, 2], [4, 6]); // null: odd and even at once
const d = Zn(60).decompose();
d.moduli; // [4n, 3n, 5n]
d.to(47); // [3n, 2n, 2n]
d.from([3, 2, 2]); // 47n
d.idempotents; // [45n, 40n, 36n]: 45 is 1 mod 4 and 0 mod 3 and 5
Ideals
Every ideal of is generated by a divisor of , and the quotient by it is . The ideal is prime exactly when it is maximal, exactly when is prime.
Zn(12).ideals().map((I) => `(${I.generator}) of size ${I.size}, quotient ${I.quotient}${I.isMaximal ? ", maximal" : ""}`);
// ["(1) of size 12, quotient Z/1", "(2) of size 6, quotient Z/2, maximal", "(3) of size 4, quotient Z/3, maximal",
// "(4) of size 3, quotient Z/4", "(6) of size 2, quotient Z/6", "(0) of size 1, quotient Z/12"]
The group of units
The units form a group under multiplication, of order . By the Chinese remainder theorem it is the product of the unit groups of the prime-power factors, and those are known: cyclic for an odd prime power, and for with the product of a group of order 2 and a cyclic group of order . So the unit group is cyclic, with a single generator called a primitive root, exactly when is 1, 2, 4, or for an odd prime .
Zn(7).primitiveRoot(); // 3n: the powers of 3 are 3, 2, 6, 4, 5, 1
Zn(7).orderOf(2); // 3n
Zn(9).unitGroup().text; // "C6"
Zn(8).unitGroup().text; // "C2 × C2"
Zn(8).primitiveRoot(); // null
Zn(15).unitGroup().text; // "C2 × C4"
Zn(24).unitGroup().text; // "C2 × C2 × C2": every unit squares to 1
For a prime modulus, Zn(p).asField() gives the field for use as polynomial coefficients. The number theory package goes further with congruences: quadratic residues, discrete logarithms, and the like.
@ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.