Rings of numbers

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 nn when they differ by a multiple of nn. The nn congruence classes 0,1,…,n−10, 1, \dots, n - 1 can be added and multiplied, and form the ring Z/n\mathbb Z/n. It is the quotient of Z\mathbb Z by the ideal of multiples of nn, 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:

a is a unit in Z/n  ⟺  gcd⁡(a,n)=1,otherwise a⋅ngcd⁡(a,n)=0. a \text{ is a unit in } \mathbb Z/n \iff \gcd(a, n) = 1, \qquad \text{otherwise } a \cdot \frac{n}{\gcd(a, n)} = 0 .

The inverse comes from the extended Euclidean algorithm: if sa+tn=1sa + tn = 1 then ss is the inverse of aa. The number of units is Euler's φ(n)\varphi(n). So Z/n\mathbb Z/n is a field exactly when nn is prime, and for composite nn 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 nn. There are none except 0 exactly when nn is square-free.
  • Idempotents, with e2=ee^2 = e: there are 2k2^k of them when nn has kk 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 mm and nn have no common factor, a number modulo mnmn is the same thing as a pair: a number modulo mm and a number modulo nn.

Z/mn  ≅  Z/m×Z/n(gcd⁡(m,n)=1). \mathbb Z/mn \;\cong\; \mathbb Z/m \times \mathbb Z/n \qquad (\gcd(m, n) = 1).

Splitting nn into prime powers, Z/n\mathbb Z/n is a product of rings Z/pk\mathbb Z/p^k, and everything about Z/n\mathbb Z/n 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 Z/n\mathbb Z/n is generated by a divisor dd of nn, and the quotient by it is Z/d\mathbb Z/d. The ideal is prime exactly when it is maximal, exactly when dd 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 φ(n)\varphi(n). 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 2k2^k with k≥3k \ge 3 the product of a group of order 2 and a cyclic group of order 2k−22^{k-2}. So the unit group is cyclic, with a single generator called a primitive root, exactly when nn is 1, 2, 4, pkp^k or 2pk2p^k for an odd prime pp.

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.

Try itEdit the code and run it with @ratmath/structures and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.
Ctrl/⌘ + Enter runs