Number fields and the integers inside them
A number field is a finite field extension K of the rationals Q — so [K:Q] is a finite number, and by the primitive element theorem we can write K = Q(α) for a single algebraic number α. The basic example after Q itself is a quadratic field like Q(√-5) or Q(i). Number theory lives inside these fields, but not on all of K: the arithmetic happens in a distinguished subring that plays the role Z plays inside Q.
An algebraic integer is an algebraic number whose minimal polynomial over Q has integer coefficients and is monic. Equivalently, α is an algebraic integer iff it satisfies a monic polynomial in Z[x]. So √2 (root of x²-2) and the golden ratio (root of x²-x-1) are algebraic integers, while 1/2 (root of 2x-1, not monic over Z) is not. This is exactly the notion of being integral over Z — see integral extension.
Why the algebraic integers form a ring
It is not obvious that a sum or product of algebraic integers is again one — adding two monic polynomials' roots doesn't obviously give a monic relation. The clean argument is module-theoretic: α is an algebraic integer iff the subring Z[α] is a finitely generated Z-module. If α, β are integral, then Z[α,β] is finitely generated over Z, hence every element of it — including α+β and αβ — lies in a finitely generated Z-module and is therefore integral. This is the standard “integral closure is a ring” argument from integral closure.
The algebraic integers inside K therefore form a ring, the ring of integers O_K. It is the integral closure of Z in K. Abstractly O_K is a free Z-module of rank n = [K:Q]; a Z-basis of it is called an integral basis. Once you have an integral basis you can do honest arithmetic — add, multiply, factor — entirely inside O_K.
Worked example: the integers of a quadratic field
Take K = Q(√d) with d a squarefree integer. A naive guess is O_K = Z[√d], but that's wrong half the time. Write α = a + b√d with a,b rational. Its conjugate is a - b√d, so trace = 2a and norm = a² - d b². The element α is an algebraic integer iff both 2a and a² - db² are ordinary integers. Chasing this congruence reveals a dependence on d mod 4.
K = Q(sqrt(d)), d squarefree. alpha = a + b*sqrt(d).
min poly: x^2 - (2a)x + (a^2 - d b^2).
alpha integral <=> 2a in Z and a^2 - d b^2 in Z.
Let 2a = m, 2b = n (so a=m/2, b=n/2). Then
4(a^2 - d b^2) = m^2 - d n^2 must be ≡ 0 (mod 4).
m^2 - d n^2 ≡ 0 (mod 4).
Case d ≡ 2,3 (mod 4): forces m,n both even => a,b in Z.
==> O_K = Z[sqrt(d)], integral basis {1, sqrt(d)}.
Case d ≡ 1 (mod 4): m,n may both be odd; m ≡ n (mod 2) works.
==> O_K = Z[(1+sqrt(d))/2], integral basis {1, (1+sqrt(d))/2}.
Examples:
d = -1 (≡ 3): O_K = Z[i] (Gaussian integers)
d = -5 (≡ 3): O_K = Z[sqrt(-5)]
d = -3 (≡ 1): O_K = Z[(1+sqrt(-3))/2] (Eisenstein integers)
d = 5 (≡ 1): O_K = Z[(1+sqrt(5))/2] (golden ratio is an integer!)