The algorithm finds the GCD of two nonnegative numbers u {\displaystyle u} and v {\displaystyle v} by repeatedly applying these identities:
As GCD is commutative ( gcd ( u , v ) = gcd ( v , u ) {\displaystyle \gcd(u,v)=\gcd(v,u)} ), those identities still apply if the operands are swapped: gcd ( 0 , v ) = v {\displaystyle \gcd(0,v)=v} , gcd ( 2 u , v ) = gcd ( u , v ) {\displaystyle \gcd(2u,v)=\gcd(u,v)} if v {\displaystyle v} is odd, etc.
While the above description of the algorithm is mathematically correct, performant software implementations typically differ from it in a few notable ways:
The following is an implementation of the algorithm in Rust exemplifying those differences, adapted from uutils:
Note: The implementation above accepts unsigned (non-negative) integers; given that gcd ( u , v ) = gcd ( ± u , ± v ) {\displaystyle \gcd(u,v)=\gcd(\pm {}u,\pm {}v)} , the signed case can be handled as follows:
Asymptotically, the algorithm requires O ( n ) {\displaystyle O(n)} steps, where n {\displaystyle 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 {\displaystyle 2} . Each step involves only a few arithmetic operations ( O ( 1 ) {\displaystyle 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 {\displaystyle n} , i.e. log 2 ( max ( u , v ) ) {\displaystyle \log _{2}(\max(u,v))} .
For arbitrarily large numbers, the asymptotic complexity of this algorithm is O ( n 2 ) {\displaystyle O(n^{2})} ,8 as each arithmetic operation (subtract and shift) involves a linear number of machine operations (one per word in the numbers' binary representation). If the numbers can be represented in the machine's memory, i.e. each number's size can be represented by a single machine word, this bound is reduced to: O ( n 2 log 2 n ) {\displaystyle O\left({\frac {n^{2}}{\log _{2}n}}\right)}
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.9
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: integers a {\displaystyle a} and b {\displaystyle b} such that a ⋅ u + b ⋅ v = gcd ( u , v ) {\displaystyle a\cdot {}u+b\cdot {}v=\gcd(u,v)} .101112
In the case of large integers, the best asymptotic complexity is O ( M ( n ) log n ) {\displaystyle O(M(n)\log n)} , with M ( n ) {\displaystyle M(n)} the cost of n {\displaystyle n} -bit multiplication; this is near-linear and vastly smaller than the binary GCD algorithm's O ( n 2 ) {\displaystyle O(n^{2})} , 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.13
The binary GCD algorithm has also been extended to domains other than natural numbers, such as Gaussian integers,14 Eisenstein integers,15 quadratic rings,1617 and integer rings of number fields.18
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.— Fangtian – Land surveying, The Nine Chapters on the Mathematical Art
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,19
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.
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. /wiki/Richard_P._Brent ↩
Brent, Richard P. (November 1999). Further analysis of the Binary Euclidean algorithm (Technical report). Oxford University Computing Laboratory. arXiv:1303.2772. PRG TR-7-99. /wiki/Richard_P._Brent ↩
Stein, J. (February 1967), "Computational problems associated with Racah algebra", Journal of Computational Physics, 1 (3): 397–405, Bibcode:1967JCoPh...1..397S, doi:10.1016/0021-9991(67)90047-2, ISSN 0021-9991 /wiki/Bibcode_(identifier) ↩
Knuth, Donald (1998), Seminumerical Algorithms, The Art of Computer Programming, vol. 2 (3rd ed.), Addison-Wesley, ISBN 978-0-201-89684-8 978-0-201-89684-8 ↩
Godbolt, Matt. "Compiler Explorer". Retrieved 4 February 2024. https://rust.godbolt.org/z/56jva3KPn ↩
Kapoor, Rajiv (21 February 2009). "Avoiding the Cost of Branch Misprediction". Intel Developer Zone. https://software.intel.com/content/www/us/en/develop/articles/avoiding-the-cost-of-branch-misprediction.html ↩
Lemire, Daniel (15 October 2019). "Mispredicted branches can multiply your running times". https://lemire.me/blog/2019/10/15/mispredicted-branches-can-multiply-your-running-times/ ↩
"GNU MP 6.1.2: Binary GCD". http://gmplib.org/manual/Binary-GCD.html ↩
Akhavi, Ali; Vallée, Brigitte (2000), "Average Bit-Complexity of Euclidean Algorithms", Proceedings ICALP'00, Lecture Notes Computer Science 1853: 373–387, CiteSeerX 10.1.1.42.7616 https://vallee.users.greyc.fr/Publications/icalp8-2000.ps ↩
Knuth 1998, p. 646, answer to exercise 39 of section 4.5.2 - Knuth, Donald (1998), Seminumerical Algorithms, The Art of Computer Programming, vol. 2 (3rd ed.), Addison-Wesley, ISBN 978-0-201-89684-8 ↩
Menezes, Alfred J.; van Oorschot, Paul C.; Vanstone, Scott A. (October 1996). "§14.4 Greatest Common Divisor Algorithms" (PDF). Handbook of Applied Cryptography. CRC Press. pp. 606–610. ISBN 0-8493-8523-7. Retrieved 9 September 2017. 0-8493-8523-7 ↩
Cohen, Henri (1993). "Chapter 1 : Fundamental Number-Theoretic Algorithms". A Course In Computational Algebraic Number Theory. Graduate Texts in Mathematics. Vol. 138. Springer-Verlag. pp. 17–18. ISBN 0-387-55640-0. 0-387-55640-0 ↩
Stehlé, Damien; Zimmermann, Paul (2004), "A binary recursive gcd algorithm" (PDF), Algorithmic number theory, Lecture Notes in Comput. Sci., vol. 3076, Springer, Berlin, pp. 411–425, CiteSeerX 10.1.1.107.8612, doi:10.1007/978-3-540-24847-7_31, ISBN 978-3-540-22156-2, MR 2138011, S2CID 3119374, INRIA Research Report RR-5050. 978-3-540-22156-2 ↩
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. https://doi.org/10.1006%2Fjsco.2000.0422 ↩
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. /wiki/Malm%C3%B6 ↩
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. /wiki/Burlington,_VT ↩
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. /wiki/Doi_(identifier) ↩
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. /wiki/Doi_(identifier) ↩