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 (i_{1},i_{2}), the fact that m.(i_{1},i_{2}) gives a relatively small second element means that m_{2}.(i_{1},i_{2}) can be considered approximately zero, relative to the size of inputs. This in turn implies that |m_{2,2}/m_{2,1}| is a good approximation to |i_{1}/i_{2}|. One can use this to create rational approximations of irrational values.
Find the half-gcd of the nearest integer to 10^{10}π with the prefactor 10^{10}:
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: