Binary GCD algorithm

From HandWiki
Visualisation of using the binary GCD algorithm to find the greatest common divisor (GCD) of 36 and 24. Thus, the GCD is 22 × 3 = 12.

The binary GCD algorithm, also known as Stein's algorithm or the binary Euclidean algorithm,[1][2] is an algorithm that computes the greatest common divisor (GCD) of two nonnegative integers. Stein's algorithm uses simpler arithmetic operations than the conventional Euclidean algorithm; it replaces division with arithmetic shifts, comparisons, and subtraction.

Although the algorithm in its contemporary form was first published by the Israeli physicist and programmer Josef Stein in 1967,[3] it may have been known by the 2nd century BCE, in ancient China.[4]

Algorithm

The algorithm reduces the problem of finding the GCD of two nonnegative numbers v and u by repeatedly applying these identities:

  1. gcd(0, v) = v, because everything divides zero, and v is the largest number that divides v. Similarly, gcd(u, 0) = u.
  2. gcd(2u2v) = 2·gcd(u, v)
  3. gcd(2uv) = gcd(uv), if v is odd (2 is not a common divisor). Similarly, gcd(u2v) = gcd(uv) if u is odd.
  4. gcd(uv) = gcd(|u − v|, min(u, v)), if u and v are both odd.

Implementation

While the above description of the algorithm is mathematically correct, performant software implementations typically differ from it in a few notable ways:

  • eschewing trial division by [math]\displaystyle{ 2 }[/math] in favour of a single bitshift and the count trailing zeros primitive; this is functionally equivalent to repeatedly applying identity 3, but much faster;
  • expressing the algorithm iteratively rather than recursively: the resulting implementation can be laid out to avoid repeated work, invoking identity 2 at the start and maintaining as invariant that both numbers are odd upon entering the loop, which only needs to implement identities 3 and 4;
  • after the initial checks for either number being zero, using the fact that [math]\displaystyle{ gcd(x, x) = x }[/math] to stop when [math]\displaystyle{ u = v }[/math] rather than doing an extra subtraction to get [math]\displaystyle{ u = 0 }[/math].

The following is an implementation of the algorithm in Rust exemplifying those differences, adapted from uutils:

/// Gives the greatest common denominator of the two inputs, unless that's 2³¹.
/// 2³¹ doesn't fit in an `i32`, so it returns -2³¹, which does.
pub fn gcd(u: i32, v: i32) -> i32 {
    // `wrapping_abs` gives a number's absolute value, unless that's 2³¹. 2³¹
    // won't fit in `i32`, so it gives -2³¹ instead.
    let mut v = v.wrapping_abs() as u32;
    if u == 0 {
        return v as i32;
    }
    let mut u = u.wrapping_abs() as u32;
    if v == 0 {
        return u as i32;
    }

    // `|` is bitwise OR. `trailing_zeros` quickly counts a binary number's
    // trailing zeros, giving its prime factorization's exponent on two.
    let gcd_exponent_on_two = (u | v).trailing_zeros();

    // `>>=` divides the left by two to the power of the right, storing that in
    // the left variable. `u` divided by its prime factorization's power of two
    // turns it odd.
    u >>= u.trailing_zeros();
    v >>= v.trailing_zeros();

    while u != v {
        if u < v {
            // Swap the variables' values with each other.
            core::mem::swap(&mut u, &mut v);
        }
        u -= v;
        u >>= u.trailing_zeros();
    }

    // `<<` multiplies the left by two to the power of the right.
    (u << gcd_exponent_on_two) as i32
}

Complexity

The algorithm requires O(n) steps, where n is the number of bits in the larger of the two numbers, as every two steps reduce at least one of the operands by at least a factor of 2. Each step involves only a few arithmetic operations (O(1) with a small constant); when working with word-sized numbers, each arithmetic operation translates to a single machine operation, so the number of machine operations is on the order of n, i.e. log₂(max(u, v)).

For arbitrary numbers, the asymptotic complexity of this algorithm is O(n2),[5] as each arithmetic operation (subtract and shift) on arbitrary-sized integers involves a linear number of machine operations (one per word in the numbers' binary representation). This bound is reduced to O(n² / log₂ n) when assuming that the (input) numbers can be represented in the (abstract) machine's memory, i.e. the machine's words can represent each number's size.

This is the same as for the Euclidean algorithm, though a more precise analysis by Akhavi and Vallée proved that binary GCD uses about 60% fewer bit operations.[6]

Extensions

The binary GCD algorithm can be extended in several ways, either to output additional information, deal with arbitrarily-large integers more efficiently, or to compute GCDs in domains other than the integers.

The extended binary GCD algorithm, analogous to the extended Euclidean algorithm, fits in the first kind of extension, as it provides the Bézout coefficients in addition to the GCD, i.e. integers a and b such that a·u + b·v = gcd(u, v).[7][8][9]

In the case of large integers, the best asymptotic complexity is O(log n M(n)), with M(n) the cost of n-bit multiplication; this is near-linear, and vastly smaller than the O(n2) of the binary GCD algorithm, though concrete implementations only outperform older algorithms for numbers larger than about 64 kilobits (i.e. greater than 8×1019265). This is achieved by extending the binary GCD algorithm using ideas from the Schönhage–Strassen algorithm for fast integer multiplication.[10]

The binary GCD algorithm has also been extended to domains other than natural numbers, such as Gaussian integers,[11] Eisenstein integers,[12] quadratic rings,[13][14] and integer rings of number fields.[15]

Historical description

An algorithm for computing the GCD of two numbers was known in ancient China, under the Han dynasty, as a method to reduce fractions:

If possible halve it; otherwise, take the denominator and the numerator, subtract the lesser from the greater, and do that alternately to make them the same. Reduce by the same number.

The phrase "if possible halve it" is ambiguous,[4]

  • if this applies when either of the numbers become even, the algorithm is the binary GCD algorithm;
  • if this only applies when both numbers are even, the algorithm is similar to the Euclidean algorithm.

See also

References

  1. Brent, Richard P. (13–15 September 1999). "Twenty years' analysis of the Binary Euclidean Algorithm". 1999 Oxford-Microsoft Symposium in honour of Professor Sir Antony Hoare. Oxford. http://wwwmaths.anu.edu.au/~brent/pub/pub183.html. 
  2. Template:Cite tech report
  3. Stein, J. (February 1967), "Computational problems associated with Racah algebra", Journal of Computational Physics 1 (3): 397–405, doi:10.1016/0021-9991(67)90047-2, ISSN 0021-9991, Bibcode1967JCoPh...1..397S 
  4. 4.0 4.1 Knuth, Donald (1998), Seminumerical Algorithms, The Art of Computer Programming, 2 (3rd ed.), Addison-Wesley, ISBN 978-0-201-89684-8 
  5. "GNU MP 6.1.2: Binary GCD". http://gmplib.org/manual/Binary-GCD.html. 
  6. Akhavi, Ali; Vallée, Brigitte (2000), "Average Bit-Complexity of Euclidean Algorithms", Proceedings ICALP'00, Lecture Notes Computer Science 1853: 373–387, https://vallee.users.greyc.fr/Publications/icalp8-2000.ps 
  7. Knuth 1998, p. 646, answer to exercise 39 of section 4.5.2
  8. Menezes, Alfred J.; van Oorschot, Paul C.; Vanstone, Scott A. (October 1996). "§14.4 Greatest Common Divisor Algorithms". Handbook of Applied Cryptography. CRC Press. pp. 606–610. ISBN 0-8493-8523-7. http://cacr.uwaterloo.ca/hac/about/chap14.pdf#page=17. Retrieved 9 September 2017. 
  9. Cohen, Henri (1993). "Chapter 1 : Fundamental Number-Theoretic Algorithms". A Course In Computational Algebraic Number Theory. Graduate Texts in Mathematics. 138. Springer-Verlag. pp. 17–18. ISBN 0-387-55640-0. https://books.google.com/books?id=hXGr-9l1DXcC. 
  10. Stehlé, Damien (2004), "A binary recursive gcd algorithm", Algorithmic number theory, Lecture Notes in Comput. Sci., 3076, Springer, Berlin, pp. 411–425, doi:10.1007/978-3-540-24847-7_31, INRIA Research Report RR-5050, ISBN 978-3-540-22156-2, http://hal.archives-ouvertes.fr/docs/00/07/15/33/PDF/RR-5050.pdf .
  11. Weilert, André (July 2000). "(1+i)-ary GCD Computation in Z[i] as an Analogue to the Binary GCD Algorithm". Journal of Symbolic Computation 30 (5): 605–617. doi:10.1006/jsco.2000.0422. 
  12. Damgård, Ivan Bjerre; Frandsen, Gudmund Skovbjerg (12–15 August 2003). "Efficient Algorithms for GCD and Cubic Residuosity in the Ring of Eisenstein Integers". 14th International Symposium on the Fundamentals of Computation Theory. Malmö, Sweden. pp. 109–117. doi:10.1007/978-3-540-45077-1_11. 
  13. Agarwal, Saurabh; Frandsen, Gudmund Skovbjerg (13–18 June 2004). "Binary GCD Like Algorithms for Some Complex Quadratic Rings". Algorithmic Number Theory Symposium. Burlington, VT, USA. pp. 57–71. doi:10.1007/978-3-540-24847-7_4. 
  14. Agarwal, Saurabh; Frandsen, Gudmund Skovbjerg (20–24 March 2006). "A New GCD Algorithm for Quadratic Number Rings with Unique Factorization". 7th Latin American Symposium on Theoretical Informatics. Valdivia, Chile. pp. 30–42. doi:10.1007/11682462_8. 
  15. Wikström, Douglas (11–15 July 2005). "On the l-Ary GCD-Algorithm in Rings of Integers". Automata, Languages and Programming, 32nd International Colloquium. Lisbon, Portugal. pp. 1189–1201. doi:10.1007/11523468_96. 

Further reading

Covers the extended binary GCD, and a probabilistic analysis of the algorithm.

Covers a variety of topics, including the extended binary GCD algorithm which outputs Bézout coefficients, efficient handling of multi-precision integers using a variant of Lehmer's GCD algorithm, and the relationship between GCD and continued fraction expansions of real numbers.

An analysis of the algorithm in the average case, through the lens of functional analysis: the algorithms' main parameters are cast as a dynamical system, and their average value is related to the invariant measure of the system's transfer operator.

External links