Polynomial GCDs and resultants
A greatest common divisor of two univariate polynomials A and B can be computed over any field by the Euclidean algorithm, and over any unique factorisation domain by a pseudo-remainder sequence, which avoids divisions. These algorithms are implemented once in gr_poly for any coefficient ring. Which one is fast depends on the ring: the Euclidean algorithm and its asymptotically fast form, the half-gcd algorithm, are the methods of choice over finite fields; over \mathbb{Q} and \mathbb{Z} their intermediate coefficients grow, and the best algorithms reduce the problem modulo primes instead.
Throughout, A and B are nonzero with m = \deg A \ge n = \deg B, and \operatorname{lc} denotes the leading coefficient.
The Euclidean algorithm#
Over a field K, the Euclidean remainder sequence is R_0 = A, R_1 = B and R_{i+1} = R_{i-1} \bmod R_i, until R_{k+1} = 0; then R_k is a GCD of A and B, and \gcd(A, B) is its monic associate. Each step costs O(\deg R_i \cdot (\deg R_{i-1} - \deg R_i + 1)) operations, and the whole sequence O(mn) operations [GG2003, Ch. 3]. Keeping track of cofactors S_i, T_i with S_i A + T_i B = R_i (starting from S_0 = 1, T_0 = 0, S_1 = 0, T_1 = 1 and applying the quotients) gives the extended GCD at the same asymptotic cost.
The resultant follows from the remainder sequence. If R = A \bmod B has degree k, then
while \operatorname{res}(A, B) = 0 if R = 0 and n > 0, and \operatorname{res}(A, B) = \operatorname{lc}(B)^m if n = 0. The Euclidean resultant algorithm accumulates these factors along the sequence (Resultants).
In gr_poly these are _gr_poly_gcd_euclidean, _gr_poly_xgcd_euclidean and _gr_poly_resultant_euclidean. They need to invert the leading coefficient of each remainder and to decide when a remainder vanishes. Over a field with decidable equality this always works. Over \mathbb{Z}/n\mathbb{Z} with composite n pretending to be a field (gr_ctx_set_is_pretend_field), a non-invertible leading coefficient makes the algorithm fail with an error status, and the factor of n it has found can be read with gr_ctx_recover_zero_divisor, which is the basis of dynamic evaluation [DDDD1985]. Over inexact rings such as real balls the algorithm succeeds when the zero tests are decided, typically for exactly represented inputs, and returns GR_UNABLE when a remainder cannot be shown to be zero or nonzero: the GCD of polynomials known only approximately is not a well-posed problem.
The half-gcd algorithm#
The quotients of the first half of the Euclidean remainder sequence depend only on the top halves of A and B. The half-gcd algorithm computes the 2 \times 2 polynomial matrix that maps (A, B) to a pair of consecutive remainders of about half the degree, recursively, and repeats until the remaining polynomials are short, giving the GCD, the extended GCD and the resultant with O(M(n) \log n) operations [Moe1973; BGY1980; ThullYap1990]. The generic implementation _gr_poly_hgcd uses the Euclidean algorithm below an inner cutoff, and _gr_poly_gcd_hgcd, _gr_poly_xgcd_hgcd and _gr_poly_resultant_hgcd call it repeatedly above an outer cutoff. Its matrix products are done by the ring method _gr_poly_hgcd_mat_mul, with Strassen's seven products and, when the ring has an FFT representation, products of transformed entries (Greatest common divisors). Over infinite fields such as \mathbb{Q} the half-gcd algorithm is not used by default, because its intermediate coefficients are no smaller than those of the Euclidean algorithm while its overhead is larger, and over approximate fields because of numerical instability.
nmod_poly.
Pseudo-remainder sequences#
Over an integral domain D that is not a field, A \bmod B is not defined, but the pseudo-remainder
has coefficients in D. Repeated pseudo-division leads to a sequence whose remainders are associate (over the fraction field) to the Euclidean remainders, but whose coefficients grow exponentially in the number of steps. The subresultant pseudo-remainder sequence of Collins and of Brown and Traub [Col1967; Bro1971; BT1971] divides each pseudo-remainder by a factor known in advance to divide it exactly, so that its elements are, up to sign, the subresultants of A and B: determinants of submatrices of the Sylvester matrix, whose size is bounded by Hadamard's inequality and grows only linearly in the step number.
The generic _gr_poly_gcd_subresultant follows Cohen's formulation [Coh1996, Algorithm 3.3.1] over a unique factorisation domain:
- Compute the contents a = \operatorname{cont}(A) and b = \operatorname{cont}(B) (GCDs of the coefficients, using the ring's GCD) and d = \gcd(a, b); replace A and B by their primitive parts, and set g = h = 1.
- Let \delta = \deg A - \deg B and R = \operatorname{prem}(A, B). If R = 0, stop with B; if \deg R = 0, the GCD is d.
- Set A \leftarrow B, B \leftarrow R / (g h^\delta) (an exact division), g \leftarrow \operatorname{lc}(A) and h \leftarrow h^{1 - \delta} g^\delta (also exact), and go to step 2.
- The GCD is d times the primitive part of the final B.
The extended version _gr_poly_xgcd_subresultant and the resultant version _gr_poly_resultant_subresultant follow the same sequence. The resultant version needs only exact divisions and works over any integral domain with exact division.
As a last resort, _gr_poly_resultant_sylvester evaluates the determinant of the Sylvester matrix with gr_mat_det, which uses a division-free algorithm by default and therefore works over any commutative ring, including rings with zero divisors.
Generic algorithm selection#
gr_poly_gcd, gr_poly_xgcd and gr_poly_resultant call ring methods, whose defaults choose from the properties of the ring:
| Ring | GCD and extended GCD | Resultant |
|---|---|---|
| finite field (or finite ring pretending to be a field) | Euclidean below length 200, half-gcd above (inner cutoff 100) | small formulas, then Euclidean or half-gcd with the same cutoffs |
other field (\mathbb{Q}, number fields, qqbar, ca, balls, floating point) | Euclidean | small formulas, then Euclidean |
| unique factorisation domain (\mathbb{Z}, \mathbb{Z}[y], \mathbb{Z}[i]) | subresultant | small formulas, then subresultant |
| other rings (\mathbb{Z}/6\mathbb{Z}) | GR_UNABLE | Sylvester determinant |
For the resultant, each failing algorithm falls back to the next one in the list subresultant, Sylvester determinant. The "small formulas" (_gr_poly_resultant_small) are division-free closed forms for a divisor of length at most 3 or a dividend of length at most 2. The non-underscore functions canonicalise the GCD: monic over a field, with a canonical unit as leading coefficient over other rings (positive over \mathbb{Z}); see Greatest common divisors over generic rings for the conventions and an example.
Specialised algorithms#
Several rings override the methods, mostly to tune the cutoffs between the Euclidean and half-gcd algorithms, which depend on the cost of the ring's multiplication:
| Ring or module | GCD algorithm |
|---|---|
nmod_poly (word-size prime p) | Euclidean below a cutoff tabulated by the bit size of p (470 to 2199), with a Montgomery-form inner loop for some moduli; half-gcd above. The resultant uses the same cutoff, the extended GCD its own table. |
fmpz_mod_poly (multi-word prime) | generic Euclidean below length 256, half-gcd above with inner cutoff 128 |
mpn_mod | Euclidean below length 240, half-gcd above with inner cutoff 80 |
fq_poly, fq_nmod_poly, fq_zech_poly | generic Euclidean below 80 to 120 (depending on the variant and on whether the characteristic has at most 8 bits), half-gcd above with inner cutoff 25 to 35 |
fmpz_poly | subresultant below length 6; otherwise the heuristic GCD, which evaluates at a large integer and reconstructs from one integer GCD, when the coefficients are small, and otherwise (or if the heuristic fails) the multimodular algorithm (GCDs of polynomials over Z) |
fmpq_poly | GCD of the primitive parts with fmpz_poly_gcd |
| multivariate polynomials | Brown's, Zippel's and Hensel-lifting algorithms (Greatest common divisors) |
gr_poly_gcd over fmpz calls _fmpz_poly_gcd through the ring method, but over fmpq it runs the generic Euclidean algorithm, which is much slower than fmpq_poly_gcd for large inputs. Resultants are better served: polynomial rings over \mathbb{Z}/p\mathbb{Z}, \mathbb{Z} and \mathbb{Q} override the resultant method with bivariate algorithms by evaluation and interpolation and by Chinese remaindering (Bivariate resultants by evaluation and interpolation, Resultants and discriminants over Z).
Referenced from#
- GCDs of polynomials over Z (algorithm)
- Half-GCD (algorithm)
- Univariate polynomials (overview)
- Polynomial arithmetic over generic rings and its specialisations (overview)
- Greatest common divisors over generic rings
- gr_poly.h (module)
References#
- [GG2003] J. von zur Gathen and J. Gerhard, Modern Computer Algebra, second edition, Cambridge University Press (2003)
- [DDDD1985] J. Della Dora, C. Dicrescenzo and D. Duval. About a new method for computing in algebraic number fields. EUROCAL '85, Lecture Notes in Computer Science 204, Springer, 1985, 289–290. https://doi.org/10.1007/3-540-15984-3_279
- [Moe1973] R. T. Moenck, "Fast computation of GCDs", Proceedings of the 5th Annual ACM Symposium on Theory of Computing (STOC 1973), 142–151
- [BGY1980] R. P. Brent, F. G. Gustavson and D. Y. Y. Yun, "Fast solution of Toeplitz systems of equations and computation of Padé approximants", Journal of Algorithms 1:3 (1980), 259–295
- [ThullYap1990] Thull, K. and Yap, C. : A Unified Approach to HGCD Algorithms for Polynomials and Integers, (1990)
- [Col1967] G. E. Collins. Subresultants and reduced polynomial remainder sequences. Journal of the ACM 14 (1967), 128–142. https://doi.org/10.1145/321371.321381
- [Bro1971] W. S. Brown. On Euclid's algorithm and the computation of polynomial greatest common divisors. Journal of the ACM 18:4 (1971), 478–504. https://doi.org/10.1145/321662.321664
- [BT1971] W. S. Brown and J. F. Traub. On Euclid's algorithm and the theory of subresultants. Journal of the ACM 18 (1971), 505–514. https://doi.org/10.1145/321662.321665
- [Coh1996] Cohen, Henri : A course in computational algebraic number theory, Springer, 1996
The FLINT team, “Polynomial GCDs and resultants,” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/algorithms/polynomials/gcd.html (generated 2026-10-20).
@misc{flint-doc-algorithms-polynomials-gcd,
author = {{The FLINT team}},
title = {{Polynomial GCDs and resultants}},
howpublished = {FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev},
year = {2026},
url = {https://flintlib.org/doc/algorithms/polynomials/gcd.html},
note = {Generated 2026-10-20}
}