Modular GCD
also: crt, chinese remainder, brown's algorithm
Computing a polynomial GCD over Z by computing it modulo several machine-word primes and reconstructing by the Chinese remainder theorem, stopping once the modulus provably exceeds twice the largest coefficient the answer could have. The answer is small even when the road to it is not, which is why this is orders of magnitude faster than any remainder sequence.
The answer is small even when the road to it is not: the GCD of two polynomials with ten-digit coefficients has coefficients of about the same size. There is therefore no reason to ever hold a three-thousand-digit intermediate, and the modular algorithm never does.
The shape of it.
for each machine-word prime p (skipping bad ones):reduce both inputs mod p coefficients are now intstake the gcd in F_p[x] plain Euclid: F_p is a fieldcombine with the previous primes Chinese remainder theoremuntil the modulus exceeds twice the coefficient boundthen verify by trial division
Two primes have to be rejected. One dividing the leading coefficients drops the degree; an unlucky one gives an image GCD of too high a degree, detected by comparing against the accumulation so far. An image of smaller degree invalidates everything collected before it, so the accumulation restarts.
The final trial division is not optional. The bound is a bound on the true GCD, and an unlucky run of primes can converge on a proper multiple of it, so dividing into both inputs is what turns a likely answer into a certain one.
Both algorithms on the same random inputs with a planted common factor. Verbatim.
deg(g) deg(a) prs (s) modular (s) max digits8 16 0.0041 0.0005 1816 32 0.0541 0.0008 1924 48 0.2715 0.0012 1932 64 0.7490 0.0020 1940 80 1.7554 0.0026 1948 96 3.5659 0.0033 19
Roughly a thousand times faster at degree 96, and the gap widens. The last column is why: the answer's size never moves, so the modular cost grows linearly while the remainder sequence grows faster than the cube of the degree.
see also
Subresultant PRS · Landau-Mignotte bound
read more