FLINT3.7.0-dev
Algorithm

Squarefree factorisation

Cost
O(M(n)log⁡n)O(M(n) \log n) field operations for a polynomial of degree nn with fast gcds (a constant number of gcds of total degree O(n)O(n)), plus pp-th roots of at most nn coefficients in characteristic pp.

A polynomial ff over a field KK is squarefree if it is not divisible by the square of a nonconstant polynomial. Every nonzero ff has a unique squarefree factorisation

f=c∏i≥1gi if = c \prod_{i \ge 1} g_i^{\,i}

with c∈K×c \in K^\times and monic, squarefree, pairwise coprime gig_i, where gig_i is the product of the irreducible factors of multiplicity exactly ii. It is the first step of every factorisation algorithm (Factoring polynomials over finite fields, Factoring polynomials over the integers) and of root finding, and it costs only gcds and exact divisions. FLINT returns the nontrivial gig_i together with their exponents ii, made monic over a field and primitive with positive leading coefficient over Z\mathbb{Z}.

The tool is the derivative. If f=∏ggegf = \prod_g g^{e_g} over the monic irreducible gg, then

f′=∑geg g′ fg.f' = \sum_g e_g\, g' \, \frac{f}{g}.

Lemma 1. Suppose that every irreducible gg is separable (gcd⁡(g,g′)=1\gcd(g, g') = 1), which holds in characteristic 0 and over finite fields. Then the multiplicity of gg in gcd⁡(f,f′)\gcd(f, f') is eg−1e_g - 1 if eg≠0e_g \ne 0 in KK, and ege_g if eg=0e_g = 0 in KK (that is, if the characteristic pp divides ege_g).

Proof. In the sum, every term except the one for gg is divisible by gegg^{e_g}. The term for gg is divisible by exactly geg−1g^{e_g - 1} if eg≠0e_g \ne 0 in KK, since g∤egg′g \nmid e_g g', and vanishes otherwise. Hence geg−1∥f′g^{e_g - 1} \| f' in the first case and geg∣f′g^{e_g} \mid f' in the second.

Characteristic zero: Yun's algorithm#

In characteristic 0, Lemma 1 gives gcd⁡(f,f′)=∏igi i−1\gcd(f, f') = \prod_i g_i^{\,i-1}, so f/gcd⁡(f,f′)=∏igif / \gcd(f, f') = \prod_i g_i is the squarefree part of ff. The multiplicities can be separated by repeating this, but Yun's algorithm [Yun1976] does it with gcds whose degrees add up to O(n)O(n).

Algorithm 1: Yun's algorithm

Input: ff of degree n≥1n \ge 1 over a field of characteristic 0, monic (or primitive over a UFD).

Output: the pairs (gi,i)(g_i, i) with gi≠1g_i \ne 1.

  1. d←gcd⁡(f,f′)d \gets \gcd(f, f'). If d=1d = 1, return (f,1)(f, 1).
  2. v←f/dv \gets f / d, w←f′/dw \gets f' / d, i←1i \gets 1.
  3. Loop:

    1. s←w−v′s \gets w - v'. If s=0s = 0, output (v,i)(v, i) if v≠1v \ne 1 and stop.
    2. u←gcd⁡(v,s)u \gets \gcd(v, s); output (u,i)(u, i) if u≠1u \ne 1.
    3. v←v/uv \gets v / u, w←s/uw \gets s / u, i←i+1i \gets i + 1.

Theorem 2. Algorithm 1 is correct. At the start of iteration ii,

v=∏j≥igj,w=∑j≥i(j−i+1) gj′ vgj,v = \prod_{j \ge i} g_j, \qquad w = \sum_{j \ge i} (j - i + 1)\, g_j'\, \frac{v}{g_j},

and the gcd of iteration ii is gig_i.

Proof. For i=1i = 1, d=∏jgj j−1d = \prod_j g_j^{\,j-1} gives v=∏jgjv = \prod_j g_j and w=f′/d=∑jjgj′v/gjw = f'/d = \sum_j j g_j' v / g_j. Given the invariant,

s=w−v′=∑j>i(j−i) gj′vgj.s = w - v' = \sum_{j > i} (j - i)\, g_j' \frac{v}{g_j}.

Every term is divisible by gig_i. For j>ij > i with gj≠1g_j \ne 1, all terms but the jj-th are divisible by gjg_j, and the jj-th is not, since j−i≠0j - i \ne 0 and gcd⁡(gj,gj′)=1\gcd(g_j, g_j') = 1. Hence gcd⁡(v,s)=gi\gcd(v, s) = g_i, and v/giv / g_i, s/gis / g_i satisfy the invariant for i+1i + 1. If s=0s = 0, then gj=1g_j = 1 for all j>ij > i, so v=giv = g_i.

The degrees of the polynomials in iteration ii are at most deg⁡v≤∑j≥ideg⁡gj\deg v \le \sum_{j \ge i} \deg g_j, and ∑i∑j≥ideg⁡gj=∑jjdeg⁡gj=n\sum_i \sum_{j \ge i} \deg g_j = \sum_j j \deg g_j = n. With gcds in O(M(m)log⁡m)O(M(m) \log m) for degree mm (Half-GCD), the whole algorithm therefore costs O(M(n)log⁡n)O(M(n) \log n), the same as one gcd of degree nn up to a constant factor [Yun1976] [GG2003, §14.6].

Finite fields#

In characteristic pp the derivative of geg^{e} vanishes when p∣ep \mid e, and f′=0f' = 0 if and only if f∈K[xp]f \in K[x^p]. Over a perfect field, in particular over Fq\mathbb{F}_q with q=pkq = p^k, such an ff is a pp-th power: if f=∑icixipf = \sum_i c_i x^{ip}, then f=(∑ici1/pxi)pf = (\sum_i c_i^{1/p} x^i)^p, where c1/p=cpk−1c^{1/p} = c^{p^{k-1}} (and c1/p=cc^{1/p} = c in a prime field). FLINT uses the following algorithm, the classical gcd iteration [Mus1971] [Knu1997, §4.6.2] combined with pp-th roots [GG2003, §14.6].

Algorithm 2: Squarefree factorisation over Fq\mathbb{F}_q

Input: ff of degree n≥1n \ge 1 over Fq\mathbb{F}_q.

Output: the pairs (gi,i)(g_i, i) with gi≠1g_i \ne 1, the gig_i monic.

  1. If f′=0f' = 0: compute hh with hp=fh^p = f by taking pp-th roots of the coefficients, factor hh recursively, multiply all exponents by pp and return.
  2. d←gcd⁡(f,f′)d \gets \gcd(f, f'). If d=1d = 1, return (f,1)(f, 1) (made monic).
  3. v←f/dv \gets f / d, i←1i \gets 1. While deg⁡v≥1\deg v \ge 1:

    1. s←gcd⁡(v,d)s \gets \gcd(v, d); w←v/sw \gets v / s; output (w,i)(w, i) if w≠1w \ne 1.
    2. d←d/sd \gets d / s, v←sv \gets s, i←i+1i \gets i + 1.
  4. If d≠1d \ne 1: dd is a pp-th power; factor d1/pd^{1/p} recursively and output its factors with exponents multiplied by pp.

Theorem 3. Algorithm 2 is correct. The exponents output in step 3 are not divisible by pp, those output in step 4 are, and all are distinct.

Proof. Write f=∏ggegf = \prod_g g^{e_g}. By Lemma 1, after step 2,

d=∏p∤eggeg−1∏p∣eggeg,v=∏p∤egg.d = \prod_{p \nmid e_g} g^{e_g - 1} \prod_{p \mid e_g} g^{e_g}, \qquad v = \prod_{p \nmid e_g} g.

By induction on ii, at the start of iteration ii we have v=∏p∤eg, eg≥igv = \prod_{p \nmid e_g,\, e_g \ge i} g and d=∏p∤eg, eg≥igeg−i∏p∣eggegd = \prod_{p \nmid e_g,\, e_g \ge i} g^{e_g - i} \prod_{p \mid e_g} g^{e_g}. Since vv is squarefree and contains only factors with p∤egp \nmid e_g, s=gcd⁡(v,d)=∏p∤eg, eg>igs = \gcd(v, d) = \prod_{p \nmid e_g,\, e_g > i} g, so w=v/sw = v/s is the product of the factors of multiplicity exactly ii (which is 1 if p∣ip \mid i), and the updated vv and dd satisfy the invariant for i+1i + 1. When v=1v = 1, the remaining d=∏p∣eggegd = \prod_{p \mid e_g} g^{e_g} is a pp-th power. In step 1 and step 4 the recursion is applied to a polynomial of degree at most n/pn/p, whose squarefree factorisation, with exponents multiplied by pp, gives the factors of ff with multiplicities divisible by pp.

As in Yun's algorithm, the degrees of the gcds in step 3 add up to at most nn, so the cost is O(M(n)log⁡n)O(M(n) \log n), plus at most n/pn/p pp-th roots of field elements per level of recursion.

The algorithm gives the factorisation with distinct exponents, not necessarily with exponents 1,2,3,…1, 2, 3, \ldots in order; FLINT returns the factors in the order in which they are found.

Squarefree part and squarefreeness test#

The squarefree part ∏igi\prod_i g_i is f/gcd⁡(f,f′)f / \gcd(f, f') (up to a unit) in characteristic 0, while in characteristic pp it is computed from the squarefree factorisation. To test whether ff is squarefree it suffices, over a perfect field, to test gcd⁡(f,f′)=1\gcd(f, f') = 1: if f′=0f' = 0 and deg⁡f≥1\deg f \ge 1 then ff is a pp-th power, hence not squarefree.

Use in FLINT#

gr_poly_factor_squarefree implements Algorithm 1 for unique factorisation domains of characteristic 0, after making ff monic (over a field) or removing its content (otherwise), and Algorithm 2 for finite fields, where the pp-th roots of the coefficients are computed with gr_fq_pth_root. gr_poly_squarefree_part and gr_poly_is_squarefree use the methods of the previous section. The finite field modules wrap the generic code; fmpz_poly_factor_squarefree has its own implementation of Yun's algorithm with integer gcds, and the squarefreeness tests for nmod_poly compute gcd⁡(f,f′)\gcd(f, f') directly.

Referenced from#

References#

  • [Yun1976] D. Y. Y. Yun. On square-free decomposition algorithms. Proceedings of the 1976 ACM Symposium on Symbolic and Algebraic Computation (SYMSAC '76), 26–35. https://doi.org/10.1145/800205.806320
  • [GG2003] J. von zur Gathen and J. Gerhard, Modern Computer Algebra, second edition, Cambridge University Press (2003)
  • [Mus1971] D. R. Musser. Algorithms for polynomial factorization. PhD thesis, University of Wisconsin, 1971.
  • [Knu1997] Knuth, D. E. The Art of Computer Programming, volume 2: Seminumerical algorithms, 1997
Cite this page

The FLINT team, “Squarefree factorisation,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/polynomials/squarefree.html (generated 2026-11-02).