Scope (3)
HalfGCD handles negative inputs:
Inputs need not be ordered by magnitude:
HalfGCD handles Gaussian integer inputs:
Check the result:
Properties and Relations (4)
The half-GCD can be iterated to compute the usual GCD (a step of the Euclidean algorithm is interleaved in order to guarantee a reduction when the numbers are too far apart for HalfGCD to give a nontrivial reduction):
Check that 8400 is the correct GCD:
Check that the corresponding row of the conversion matrix gives the multipliers for the extended GCD:
The half-GCD is often identical (up to row signs) to the result of a certain lattice reduction:
The asymptotic speed of the half-GCD implementation is quasilinear (linear times logarithmic factors) in the size of the inputs, so doubling the sizes only slightly more than doubles the timings:
For large inputs (i1,i2), the fact that m.(i1,i2) gives a relatively small second element means that m2.(i1,i2) can be considered approximately zero, relative to the size of inputs. This in turn implies that is a good approximation to . One can use this to create rational approximations of irrational values.
Find the half-GCD of the nearest integer to 1010π with the prefactor 1010:
Check the quotient of the second row in the multiplier matrix:
This quotient will often appear in the list of continued fraction approximations to π:
Possible Issues (1)
When the second input divides the first and is less than the square root of the first, the GCD can be computed in one step, but HalfGCD will not take that step due to criterion (iii) noted in Details & Options:
Neat Examples (3)
Thue's Lemma guarantees that for a given modulus p>1, an integer n has a rational equivalent modulo p with numerator and denominator no larger than . It can be found using the half-GCD. A common use case is in reconstructing rational solutions to linear equations from solutions computed over the integers modulo a power of a prime.
Code to reconstruct a rational equivalent mod p for a given n:
Find the rational equivalent to 123456789 modulo the cube of the 100th prime:
Show that it is equivalent: