Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Greatest common divisor
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Use American English|date = March 2019}} {{Short description|Largest integer that divides given integers}} In [[mathematics]], the '''greatest common divisor''' ('''GCD'''), also known as '''greatest common factor (GCF)''', of two or more [[integer]]s, which are not all zero, is the largest positive integer that [[divides]] each of the integers. For two integers {{math|''x''}}, {{math|''y''}}, the greatest common divisor of {{math|''x''}} and {{math|''y''}} is denoted <math>\gcd (x,y)</math>. For example, the GCD of 8 and 12 is 4, that is, {{math|1=gcd(8, 12) = 4}}.<ref name="Long 1972 33">{{harvtxt|Long|1972|p=33}}</ref><ref name="Pettofrezzo 1970 34">{{harvtxt|Pettofrezzo|Byrkit|1970|p=34}}</ref> In the name "greatest common divisor", the adjective "greatest" may be replaced by "highest", and the word "divisor" may be replaced by "factor", so that other names include '''highest common factor''', etc.<ref> {{cite book | last = Kelley | first = W. Michael | isbn = 978-1-59257-161-1 | page = 142 | publisher = Penguin | title = The Complete Idiot's Guide to Algebra | url = https://books.google.com/books?id=K1hCltk-2RwC&pg=PA142 | year = 2004}}.</ref><ref>{{cite book | last = Jones | first = Allyn | isbn = 978-1-86441-378-6 | page = 16 | publisher = Pascal Press | title = Whole Numbers, Decimals, Percentages and Fractions Year 7 | url = https://books.google.com/books?id=l-ItSuk-zngC&pg=PA16 | year = 1999 }}.</ref><ref name="Hardy&Wright 1979 20" /><ref>Some authors treat '''{{vanchor|greatest common denominator}}''' as synonymous with ''greatest common divisor''. This contradicts the common meaning of the words that are used, as ''[[denominator]]'' refers to [[fraction (mathematics)|fractions]], and two fractions do not have any greatest common denominator (if two fractions have the same denominator, one obtains a greater common denominator by multiplying all numerators and denominators by the same [[integer]]).</ref> Historically, other names for the same concept have included '''greatest common measure'''.<ref> {{cite book | last1 = Barlow | first1 = Peter | author1-link = Peter Barlow (mathematician) | last2 = Peacock | first2 = George | author2-link = George Peacock | last3 = Lardner | first3 = Dionysius | author3-link = Dionysius Lardner | last4 = Airy | first4 = Sir George Biddell | author4-link = George Biddell Airy | last5 = Hamilton | first5 = H. P. | author5-link = Henry Hamilton (priest) | last6 = Levy | first6 = A. | last7 = De Morgan | first7 = Augustus | author7-link = Augustus De Morgan | last8 = Mosley | first8 = Henry | page = 589 | publisher = R. Griffin and Co. | title = Encyclopaedia of Pure Mathematics | url = https://books.google.com/books?id=3fIUAQAAMAAJ&pg=PA589 | year = 1847 }}.</ref> This notion can be extended to polynomials (see ''[[Polynomial greatest common divisor]]'') and other [[commutative ring]]s (see ''{{section link|#In commutative rings}}'' below). == Overview == === Definition === The ''greatest common divisor'' (GCD) of [[integer]]s {{mvar|a}} and {{mvar|b}}, at least one of which is nonzero, is the greatest [[positive integer]] {{mvar|d}} such that {{mvar|d}} is a [[divisor]] of both {{mvar|a}} and {{mvar|b}}; that is, there are integers {{mvar|e}} and {{mvar|f}} such that {{math|1=''a'' = ''de''}} and {{math|1=''b'' = ''df''}}, and {{mvar|d}} is the largest such integer. The GCD of {{mvar|a}} and {{mvar|b}} is generally denoted {{math|gcd(''a'', ''b'')}}.{{refn|Some authors use {{math|(''a'', ''b'')}},<ref name="Long 1972 33" /><ref name="Pettofrezzo 1970 34" /><ref name="Hardy&Wright 1979 20" /> but this notation is often ambiguous. {{harvtxt|Andrews|1994|p=16}} explains this as: "Many authors write {{math|(''a'', ''b'')}} for {{math|g.c.d.(''a'', ''b'')}}. We do not, because we shall often use {{math|(''a'', ''b'')}} to represent a point in the Euclidean plane."}} When one of {{math|''a''}} and {{math|''b''}} is zero, the GCD is the absolute value of the nonzero integer: {{math|1=gcd(''a'', 0) = gcd(0, ''a'') = {{abs|''a''}}}}. This case is important as the terminating step of the [[#Euclidean algorithm|Euclidean algorithm]]. The above definition is unsuitable for defining {{math|gcd(0, 0)}}, since there is no greatest integer {{math|''n''}} such that {{math|1=0 × ''n'' = 0}}. However, zero is its own greatest divisor if ''greatest'' is understood in the context of the divisibility relation, so {{math|gcd(0, 0)}} is commonly defined as {{math|0}}. This preserves the usual identities for GCD, and in particular [[Bézout's identity]], namely that {{math|gcd(''a'', ''b'')}} [[generating set of an ideal|generates]] the same [[ideal (ring theory)|ideal]] as {{math|{{mset|''a'', ''b''}}}}.<ref>Thomas H. Cormen, ''et al.'', ''Introduction to Algorithms'' (2nd edition, 2001) {{isbn|0262032937}}, p. 852</ref><ref>Bernard L. Johnston, Fred Richman, ''Numbers and Symmetry: An Introduction to Algebra'' {{isbn|084930301X}}, p. 38</ref><ref>Martyn R. Dixon, ''et al.'', ''An Introduction to Essential Algebraic Structures'' {{isbn|1118497759}}, p. 59</ref> This convention is followed by many [[computer algebra system]]s.<ref>e.g., [[Wolfram Alpha]] [https://www.wolframalpha.com/input/?i=gcd%280%2C+0%29 calculation] and [[Maxima computer algebra system|Maxima]]</ref> Nonetheless, some authors leave {{math|gcd(0, 0)}} undefined.<ref>Jonathan Katz, Yehuda Lindell, ''Introduction to Modern Cryptography'' {{isbn|1351133012}}, 2020, section 9.1.1, p. 45</ref> The GCD of {{mvar|a}} and {{mvar|b}} is their [[greatest element|greatest]] positive common divisor in the [[preorder]] relation of [[divisibility]]. This means that the common divisors of {{mvar|a}} and {{mvar|b}} are exactly the divisors of their GCD. This is commonly proved by using either [[Euclid's lemma]], the [[fundamental theorem of arithmetic]], or the [[Euclidean algorithm]]. This is the meaning of "greatest" that is used for the generalizations of the concept of GCD. === Example === The number 54 can be expressed as a product of two integers in several different ways: : <math> 54 \times 1 = 27 \times 2 = 18 \times 3 = 9 \times 6.</math> Thus the complete list of ''divisors'' of 54 is 1, 2, 3, 6, 9, 18, 27, 54. Similarly, the divisors of 24 are 1, 2, 3, 4, 6, 8, 12, 24. The numbers that these two lists have ''in common'' are the ''common divisors'' of 54 and 24, that is, : <math> 1, 2, 3, 6. </math> Of these, the greatest is 6, so it is the ''greatest common divisor'': : <math> \gcd(54,24) = 6. </math> Computing all divisors of the two numbers in this way is usually not efficient, especially for large numbers that have many divisors. Much more efficient methods are described in ''{{slink|#Calculation}}''. === Coprime numbers === {{Main|Coprime integers}} Two numbers are called relatively prime, or [[coprime]], if their greatest common divisor equals {{math|1}}.<ref>{{Cite web|last=Weisstein|first=Eric W.|title=Greatest Common Divisor|url=https://mathworld.wolfram.com/GreatestCommonDivisor.html|access-date=2020-08-30|website=mathworld.wolfram.com|language=en}}</ref> For example, 9 and 28 are coprime. === A geometric view === [[File:24x60.svg|thumb|upright|alt="Tall, slender rectangle divided into a grid of squares. The rectangle is two squares wide and five squares tall."|A 24-by-60 rectangle is covered with ten 12-by-12 square tiles, where 12 is the GCD of 24 and 60. More generally, an ''a''-by-''b'' rectangle can be covered with square tiles of side length ''c'' only if ''c'' is a common divisor of ''a'' and ''b''.]] For example, a 24-by-60 rectangular area can be divided into a grid of: 1-by-1 squares, 2-by-2 squares, 3-by-3 squares, 4-by-4 squares, 6-by-6 squares or 12-by-12 squares. Therefore, 12 is the greatest common divisor of 24 and 60. A 24-by-60 rectangular area can thus be divided into a grid of 12-by-12 squares, with two squares along one edge ({{math|1=24/12 = 2}}) and five squares along the other ({{math|1=60/12 = 5}}). == Applications == === Reducing fractions === {{further|Irreducible fraction}} The greatest common divisor is useful for reducing [[fraction]]s to the [[Irreducible fraction|lowest terms]].<ref>{{Cite web|title=Greatest Common Factor|url=https://www.mathsisfun.com/greatest-common-factor.html|access-date=2020-08-30|website=www.mathsisfun.com}}</ref> For example, {{math|1=gcd(42, 56) = 14}}, therefore, : <math>\frac{42}{56}=\frac{3 \cdot 14 }{ 4 \cdot 14}=\frac{3 }{ 4}.</math> === Least common multiple === {{Further|Least common multiple}} The least common multiple of two integers that are not both zero can be computed from their greatest common divisor, by using the relation : <math>\operatorname{lcm}(a,b)=\frac{|a\cdot b|}{\operatorname{gcd}(a,b)}.</math> == Calculation == <!-- Section linked from [[Fundamental theorem of arithmetic]] --> === Using prime factorizations === Greatest common divisors can be computed by determining the [[prime factorization]]s of the two numbers and comparing factors. For example, to compute {{math|gcd(48, 180)}}, we find the prime factorizations 48 = 2<sup>4</sup> · 3<sup>1</sup> and 180 = 2<sup>2</sup> · 3<sup>2</sup> · 5<sup>1</sup>; the GCD is then 2<sup>min(4,2)</sup> · 3<sup>min(1,2)</sup> · 5<sup>min(0,1)</sup> = 2<sup>2</sup> · 3<sup>1</sup> · 5<sup>0</sup> = 12 The corresponding LCM is then 2<sup>max(4,2)</sup> · 3<sup>max(1,2)</sup> · 5<sup>max(0,1)</sup> = 2<sup>4</sup> · 3<sup>2</sup> · 5<sup>1</sup> = 720. In practice, this method is only feasible for small numbers, as computing prime factorizations takes too long. === Euclid's algorithm === {{Main|Euclidean algorithm}} The method introduced by [[Euclid]] for computing greatest common divisors is based on the fact that, given two positive integers {{mvar|a}} and {{mvar|b}} such that {{math|''a'' > ''b''}}, the common divisors of {{mvar|a}} and {{mvar|b}} are the same as the common divisors of {{math|''a'' – ''b''}} and {{mvar|b}}. So, Euclid's method for computing the greatest common divisor of two positive integers consists of replacing the larger number with the difference of the numbers, and repeating this until the two numbers are equal: that is their greatest common divisor. For example, to compute {{math|gcd(48,18)}}, one proceeds as follows: : <math>\begin{align}\gcd(48,18)\quad&\to\quad \gcd(48-18, 18)= \gcd(30,18)\\ &\to \quad \gcd(30-18, 18)= \gcd(12,18)\\ &\to \quad \gcd(12,18-12)= \gcd(12,6)\\ &\to \quad \gcd(12-6,6)= \gcd(6,6).\end{align}</math> So {{math|1=gcd(48, 18) = 6}}. This method can be very slow if one number is much larger than the other. So, the variant that follows is generally preferred. === Euclidean algorithm === {{Main|Euclidean algorithm}} [[File:The Great Common Divisor of 62 and 36 is 2.ogv|thumb|Animation showing an application of the Euclidean algorithm to find the greatest common divisor of 62 and 36, which is 2.]] A more efficient method is the ''Euclidean algorithm'', a variant in which the difference of the two numbers {{mvar|a}} and {{mvar|b}} is replaced by the ''remainder'' of the [[Euclidean division]] (also called ''division with remainder'') of {{mvar|a}} by {{mvar|b}}. Denoting this remainder as {{math|''a'' mod ''b''}}, the algorithm replaces {{math|(''a'', ''b'')}} with {{math|(''b'', ''a'' mod ''b'')}} repeatedly until the pair is {{math|(''d'', 0)}}, where {{mvar|d}} is the greatest common divisor. For example, to compute gcd(48,18), the computation is as follows: : <math>\begin{align}\gcd(48,18)\quad&\to\quad \gcd(18, 48\bmod 18)= \gcd(18, 12)\\ &\to \quad \gcd(12, 18\bmod 12)= \gcd(12,6)\\ &\to \quad \gcd(6,12\bmod 6)= \gcd(6,0).\end{align}</math> This again gives {{math|1=gcd(48, 18) = 6}}. === Binary GCD algorithm === {{Main|Binary GCD algorithm}} The binary GCD algorithm is a variant of Euclid's algorithm that is specially adapted to the [[binary representation]] of the numbers, which is used in most [[computers]]. The binary GCD algorithm differs from Euclid's algorithm essentially by dividing by two every even number that is encountered during the computation. Its efficiency results from the fact that, in binary representation, testing parity consists of testing the right-most digit, and dividing by two consists of removing the right-most digit. The method is as follows, starting with {{math|''a''}} and {{math|''b''}} that are the two positive integers whose GCD is sought. # If {{math|''a''}} and {{math|''b''}} are both even, then divide both by two until at least one of them becomes odd; let {{mvar|d}} be the number of these paired divisions. # If {{math|''a''}} is even, then divide it by two until it becomes odd. # If {{math|''b''}} is even, then divide it by two until it becomes odd. #: Now, {{math|''a''}} and {{math|''b''}} are both odd and will remain odd until the end of the computation # While {{math|''a'' ≠ ''b''}} do #* If {{math|''a'' > ''b''}}, then replace {{mvar|a}} with {{math|''a'' – ''b''}} and divide the result by two until {{mvar|a}} becomes odd (as {{math|''a''}} and {{math|''b''}} are both odd, there is, at least, one division by 2). #* If {{math|''a'' < ''b''}}, then replace {{mvar|b}} with {{math|''b'' – ''a''}} and divide the result by two until {{mvar|b}} becomes odd. # Now, {{math|1=''a'' = ''b''}}, and the greatest common divisor is <math>2^d a.</math> Step 1 determines {{mvar|d}} as the highest power of {{math|2}} that divides {{math|''a''}} and {{math|''b''}}, and thus their greatest common divisor. None of the steps changes the set of the odd common divisors of {{math|''a''}} and {{math|''b''}}. This shows that when the algorithm stops, the result is correct. The algorithm stops eventually, since each steps divides at least one of the operands by at least {{math|2}}. Moreover, the number of divisions by {{math|2}} and thus the number of subtractions is at most the total number of digits. Example: (''a'', ''b'', ''d'') = (48, 18, 0) → (24, 9, 1) → (12, 9, 1) → (6, 9, 1) → (3, 9, 1) → (3, 3, 1) ; the original GCD is thus the product 6 of {{math|1=2<sup>''d''</sup> = 2<sup>1</sup>}} and {{math|1=''a'' = ''b'' = 3}}. The binary GCD algorithm is particularly easy to implement and particularly efficient on binary computers. Its [[computational complexity]] is : <math>O((\log a + \log b)^2).</math> The square in this complexity comes from the fact that division by {{math|2}} and subtraction take a time that is proportional to the number of [[bit]]s of the input. The computational complexity is usually given in terms of the length {{math|''n''}} of the input. Here, this length is {{math|1=''n'' = log ''a'' + log ''b''}}, and the complexity is thus : <math>O(n^2)</math>. === Lehmer's GCD algorithm === {{Main|Lehmer's GCD algorithm}} Lehmer's algorithm is based on the observation that the initial quotients produced by Euclid's algorithm can be determined based on only the first few digits; this is useful for numbers that are larger than a [[Word (computer architecture)|computer word]]. In essence, one extracts initial digits, typically forming one or two computer words, and runs Euclid's algorithms on these smaller numbers, as long as it is guaranteed that the quotients are the same with those that would be obtained with the original numbers. The quotients are collected into a small 2-by-2 transformation matrix (a matrix of single-word integers) to reduce the original numbers. This process is repeated until numbers are small enough that the binary algorithm (see below) is more efficient. This algorithm improves speed, because it reduces the number of operations on very large numbers, and can use hardware arithmetic for most operations. In fact, most of the quotients are very small, so a fair number of steps of the Euclidean algorithm can be collected in a 2-by-2 matrix of single-word integers. When Lehmer's algorithm encounters a quotient that is too large, it must fall back to one iteration of Euclidean algorithm, with a [[Euclidean division]] of large numbers. === Other methods === [[File:Greatest common divisor.png|thumb|[[Thomae's function]]]] If {{math|''a''}} and {{math|''b''}} are both nonzero, the greatest common divisor of {{math|''a''}} and {{math|''b''}} can be computed by using [[least common multiple]] (LCM) of {{math|''a''}} and {{math|''b''}}: : <math>\gcd(a,b)=\frac{|a\cdot b|}{\operatorname{lcm}(a,b)}</math>, but more commonly the LCM is computed from the GCD. Using [[Thomae's function]] {{math|''f''}}, : <math>\gcd(a,b) = a f\left(\frac b a\right),</math> which generalizes to {{math|''a''}} and {{math|''b''}} [[rational number]]s or [[Commensurability (mathematics)|commensurable]] real numbers. Keith Slavin has shown that for odd {{math|''a'' ≥ 1}}: : <math>\gcd(a,b)=\log_2\prod_{k=0}^{a-1} (1+e^{-2i\pi k b/a})</math> which is a function that can be evaluated for complex ''b''.<ref>{{cite journal |last=Slavin |first=Keith R. |title=Q-Binomials and the Greatest Common Divisor |journal=INTEGERS: The Electronic Journal of Combinatorial Number Theory |volume=8 |pages=A5 |publisher=[[University of West Georgia]], [[Charles University in Prague]] |year=2008 |url=http://www.integers-ejcnt.org/vol8.html |access-date=2008-05-26}}</ref> Wolfgang Schramm has shown that : <math>\gcd(a,b)=\sum\limits_{k=1}^a \exp (2\pi ikb/a) \cdot \sum\limits_{d\left| a\right.} \frac{c_d (k)}{d} </math> is an [[entire function]] in the variable ''b'' for all positive integers ''a'' where ''c''<sub>''d''</sub>(''k'') is [[Ramanujan's sum]].<ref>{{cite journal |last=Schramm |first=Wolfgang |title=The Fourier transform of functions of the greatest common divisor |journal=INTEGERS: The Electronic Journal of Combinatorial Number Theory |volume=8 |pages=A50 |publisher=[[University of West Georgia]], [[Charles University in Prague]] |year=2008 |url=http://www.integers-ejcnt.org/vol8.html |access-date=2008-11-25}}</ref> === Complexity === The [[computational complexity]] of the computation of greatest common divisors has been widely studied.<ref>{{Cite book |first=Donald E. |last=Knuth | author-link = Donald Knuth |title=[[The Art of Computer Programming]] |volume=2: Seminumerical Algorithms |edition=3rd |year=1997 |publisher=Addison-Wesley Professional |isbn=0-201-89684-2}}</ref> If one uses the [[Euclidean algorithm]] and the elementary algorithms for multiplication and division, the computation of the greatest common divisor of two integers of at most {{mvar|n}} [[bit]]s is {{math|''O''(''n''<sup>2</sup>)}}.{{cn|reason=This is far from obvious, imo.|date=March 2025}} This means that the computation of greatest common divisor has, up to a constant factor, the same complexity as the multiplication. However, if a fast [[multiplication algorithm]] is used, one may modify the Euclidean algorithm for improving the complexity, but the computation of a greatest common divisor becomes slower than the multiplication. More precisely, if the multiplication of two integers of {{math|''n''}} bits takes a time of {{math|''T''(''n'')}}, then the fastest known algorithm for greatest common divisor has a complexity {{math|''O''(''T''(''n'') log ''n'')}}. This implies that the fastest known algorithm has a complexity of {{math|''O''(''n'' (log ''n'')<sup>2</sup>)}}. Previous complexities are valid for the usual [[models of computation]], specifically [[multitape Turing machine]]s and [[random-access machine]]s. The computation of the greatest common divisors belongs thus to the class of problems solvable in [[quasilinear time]]. ''A fortiori'', the corresponding [[decision problem]] belongs to the class [[P (complexity)|P]] of problems solvable in polynomial time. The GCD problem is not known to be in [[NC (complexity)|NC]], and so there is no known way to [[parallel algorithm|parallelize]] it efficiently; nor is it known to be [[P-complete]], which would imply that it is unlikely to be possible to efficiently parallelize GCD computation. Shallcross et al. showed that a related problem (EUGCD, determining the remainder sequence arising during the Euclidean algorithm) is NC-equivalent to the problem of [[integer linear programming]] with two variables; if either problem is in '''NC''' or is '''P-complete''', the other is as well.<ref>{{cite book |first1=D. |last1=Shallcross |first2=V. |last2=Pan |first3=Y. |last3=Lin-Kriz |chapter=The NC equivalence of planar integer linear programming and Euclidean GCD |title=34th IEEE Symp. Foundations of Computer Science |year=1993 |pages=557–564 |chapter-url=http://www.icsi.berkeley.edu/pubs/techreports/tr-92-041.pdf |archive-url=https://web.archive.org/web/20060905023143/http://www.icsi.berkeley.edu/pubs/techreports/tr-92-041.pdf |archive-date=2006-09-05 |url-status=live }}</ref> Since '''NC''' contains [[NL (complexity)|NL]], it is also unknown whether a space-efficient algorithm for computing the GCD exists, even for nondeterministic Turing machines. Although the problem is not known to be in '''NC''', parallel algorithms [[asymptotically faster algorithm|asymptotically faster]] than the Euclidean algorithm exist; the fastest known deterministic algorithm is by [[Benny Chor|Chor]] and [[Oded Goldreich|Goldreich]], which (in the [[CRCW-PRAM]] model) can solve the problem in {{math|''O''(''n''/log ''n'')}} time with {{math|''n''<sup>1+''ε''</sup>}} processors.<ref>{{cite journal |first1=B. |last1=Chor | author1-link = Benny Chor |first2=O. |last2=Goldreich |title=An improved parallel algorithm for integer GCD |journal=Algorithmica |volume=5 |issue=1–4 |pages=1–10 |year=1990 |doi=10.1007/BF01840374 |s2cid=17699330 }}</ref> [[Randomized algorithm]]s can solve the problem in {{math|''O''((log ''n'')<sup>2</sup>)}} time on <math>\exp\left(O\left(\sqrt{n \log n}\right)\right)</math> processors {{clarify|reason=with O notation in exponent, the complexity is not changed if the sqrt and the log are omitted|date=April 2018}} (this is [[superpolynomial]]).<ref>{{cite conference |first1=L. M. |last1=Adleman |first2=K. |last2=Kompella |chapter=Using smoothness to achieve parallelism |title=20th Annual ACM Symposium on Theory of Computing |pages=528–538 |year=1988 |isbn=0-89791-264-0 |location=New York |doi=10.1145/62212.62264 |s2cid=9118047 }}</ref> == Properties == * For positive integers {{math|''a''}}, {{math|1=gcd(''a'', ''a'') = ''a''}}. * Every common divisor of {{math|''a''}} and {{math|''b''}} is a divisor of {{math|gcd(''a'', ''b'')}}. * {{math|gcd(''a'', ''b'')}}, where ''a'' and ''b'' are not both zero, may be defined alternatively and equivalently as the smallest positive integer ''d'' which can be written in the form {{math|1=''d'' = ''a''⋅''p'' + ''b''⋅''q''}}, where ''p'' and ''q'' are integers. This expression is called [[Bézout's identity]]. Numbers ''p'' and ''q'' like this can be computed with the [[extended Euclidean algorithm]]. * {{math|1=gcd(''a'', 0) = {{abs|''a''}}}}, for {{math|''a'' ≠ 0}}, since any number is a divisor of 0, and the greatest divisor of ''a'' is {{math|{{abs|''a''}}}}.<ref name="Pettofrezzo 1970 34" /><ref name="Hardy&Wright 1979 20">{{harvtxt|Hardy|Wright|1979|p=20}}</ref> This is usually used as the base case in the Euclidean algorithm. * If ''a'' divides the product ''b''⋅''c'', and {{math|1=gcd(''a'', ''b'') = ''d''}}, then ''a''/''d'' divides ''c''. * If ''m'' is a positive integer, then {{math|1=gcd(''m''⋅''a'', ''m''⋅''b'') = ''m''⋅gcd(''a'', ''b'')}}. * If ''m'' is any integer, then {{math|1=gcd(''a'' + ''m''⋅''b'', ''b'') = gcd(''a'', ''b'')}}. Equivalently, {{math|1=gcd(''a'' mod ''b'',''b'') = gcd(''a'',''b'')}}. * If ''m'' is a positive common divisor of ''a'' and ''b'', then {{math|1=gcd(''a''/''m'', ''b''/''m'') = gcd(''a'', ''b'')/''m''}}. * If {{math|1=gcd(''a'', ''b'') = ''d''}}, then {{math|1=gcd(''a''/''d'', ''b''/''d'') = 1}}. * The GCD is a [[Commutativity|commutative]] function: {{math|1=gcd(''a'', ''b'') = gcd(''b'', ''a'')}}. * The GCD is an [[Associativity|associative]] function: {{math|1=gcd(''a'', gcd(''b'', ''c'')) = gcd(gcd(''a'', ''b''), ''c'')}}. Thus {{math|1=gcd(''a'', ''b'', ''c'', ...)}} can be used to denote the GCD of multiple arguments. * The GCD is a [[multiplicative function]] in the following sense: if ''a''<sub>1</sub> and ''a''<sub>2</sub> are relatively prime, then {{math|1=gcd(''a''<sub>1</sub>⋅''a''<sub>2</sub>, ''b'') = gcd(''a''<sub>1</sub>, ''b'')⋅gcd(''a''<sub>2</sub>, ''b'')}}. * {{math|gcd(''a'', ''b'')}} is closely related to the [[least common multiple]] {{math|lcm(''a'', ''b'')}}: we have *: {{math|1=gcd(''a'', ''b'')⋅lcm(''a'', ''b'') = {{abs|''a''⋅''b''}}}}. : This formula is often used to compute least common multiples: one first computes the GCD with Euclid's algorithm and then divides the product of the given numbers by their GCD. * The following versions of [[distributivity]] hold true: *: {{math|1=gcd(''a'', lcm(''b'', ''c'')) = lcm(gcd(''a'', ''b''), gcd(''a'', ''c''))}} *: {{math|1=lcm(''a'', gcd(''b'', ''c'')) = gcd(lcm(''a'', ''b''), lcm(''a'', ''c''))}}. * If we have the unique prime factorizations of {{math|1=''a'' = ''p''<sub>1</sub><sup>''e''<sub>1</sub></sup> ''p''<sub>2</sub><sup>''e''<sub>2</sub></sup> ⋅⋅⋅ ''p''<sub>''m''</sub><sup>''e''<sub>''m''</sub></sup>}} and {{math|1=''b'' = ''p''<sub>1</sub><sup>''f''<sub>1</sub></sup> ''p''<sub>2</sub><sup>''f''<sub>2</sub></sup> ⋅⋅⋅ ''p''<sub>''m''</sub><sup>''f''<sub>''m''</sub></sup>}} where {{math|1=''e''<sub>''i''</sub> ≥ 0}} and {{math|1=''f''<sub>''i''</sub> ≥ 0}}, then the GCD of ''a'' and ''b'' is *: {{math|1=gcd(''a'',''b'') = ''p''<sub>1</sub><sup>min(''e''<sub>1</sub>,''f''<sub>1</sub>)</sup> ''p''<sub>2</sub><sup>min(''e''<sub>2</sub>,''f''<sub>2</sub>)</sup> ⋅⋅⋅ ''p''<sub>''m''</sub><sup>min(''e''<sub>''m''</sub>,''f''<sub>''m''</sub>)</sup>}}. * It is sometimes useful to define {{math|1=gcd(0, 0) = 0}} and {{math|1=lcm(0, 0) = 0}} because then the [[natural number]]s become a [[complete lattice|complete]] [[distributive lattice]] with GCD as meet and LCM as join operation.<ref> {{cite book | last1 = Müller-Hoissen | first1 = Folkert | last2 = Walther | first2 = Hans-Otto | editor1-last = Müller-Hoissen | editor1-first = Folkert | editor2-last = Pallo | editor2-first = Jean Marcel | editor3-last = Stasheff | editor3-first = Jim | editor3-link = Jim Stasheff | contribution = Dov Tamari (formerly Bernhard Teitler) | isbn = 978-3-0348-0405-9 | pages = 1–40 | publisher = Birkhäuser | series = Progress in Mathematics | title = Associahedra, Tamari Lattices and Related Structures: Tamari Memorial Festschrift | volume = 299 | year = 2012 }}. Footnote 27, p. 9: "For example, the natural numbers with ''gcd'' (greatest common divisor) as meet and ''lcm'' (least common multiple) as join operation determine a (complete distributive) lattice." Including these definitions for 0 is necessary for this result: if one instead omits 0 from the set of natural numbers, the resulting lattice is not complete.</ref> This extension of the definition is also compatible with the generalization for commutative rings given below. * In a [[Cartesian coordinate system]], {{math|gcd(''a'', ''b'')}} can be interpreted as the number of segments between points with integral coordinates on the straight [[line segment]] joining the points {{math|(0, 0)}} and {{math|(''a'', ''b'')}}. * For non-negative integers {{math|''a''}} and {{math|''b''}}, where {{math|''a''}} and {{math|''b''}} are not both zero, provable by considering the Euclidean algorithm in base ''n'':<ref>{{cite book |first1=Donald E. |last1=Knuth |author-link1=Donald Knuth |last2=Graham |first2=R. L. |last3=Patashnik |first3=O. |title=[[Concrete Mathematics: A Foundation for Computer Science]] |date=March 1994 |publisher=[[Addison-Wesley]] |isbn=0-201-55802-5}}</ref> *: {{math|1=gcd(''n''<sup>''a''</sup> − 1, ''n''<sup>''b''</sup> − 1) = ''n''<sup>gcd(''a'',''b'')</sup> − 1}}. * An [[Identity (mathematics)|identity]] involving [[Euler's totient function]]: *: <math> \gcd(a,b) = \sum_{k|a \text{ and }k|b} \varphi(k) .</math> * GCD Summatory function (Pillai's arithmetical function): <math>\sum_{k=1}^n \gcd(k,n) = \sum_{d|n} d \varphi \left( \frac n d \right) =n\sum_{d|n}\frac{\varphi(d)}{d} =n\prod_{p|n}\left(1+\nu_p(n)\left(1-\frac{1}{p}\right)\right)</math> where <math>\nu_p(n)</math> is the {{math|''p''}}-adic valuation. {{OEIS|A018804}} == Probabilities and expected value == In 1972, James E. Nymann showed that {{math|''k''}} integers, chosen independently and uniformly from {{math|{{mset|1, ..., ''n''}}}}, are coprime with probability {{math|1/''ζ''(''k'')}} as {{math|''n''}} goes to infinity, where {{math|''ζ''}} refers to the [[Riemann zeta function]].<ref name="nymann">{{cite journal |first=J. E. |last=Nymann |title=On the probability that {{math|''k''}} positive integers are relatively prime |journal=[[Journal of Number Theory]] |volume=4 |issue=5 |pages=469–473 |year=1972 |doi=10.1016/0022-314X(72)90038-8 |bibcode=1972JNT.....4..469N |doi-access=free }}</ref> (See [[coprime]] for a derivation.) This result was extended in 1987 to show that the probability that {{math|''k''}} random integers have greatest common divisor {{math|''d''}} is {{math|''d''<sup>−''k''</sup>/ζ(''k'')}}.<ref name="chid">{{cite journal |first1=J. |last1=Chidambaraswamy |first2=R. |last2=Sitarmachandrarao |title=On the probability that the values of ''m'' polynomials have a given g.c.d. |journal=Journal of Number Theory |volume=26 |issue=3 |pages=237–245 |year=1987 |doi=10.1016/0022-314X(87)90081-3 |doi-access=free }}</ref> Using this information, the [[expected value]] of the greatest common divisor function can be seen (informally) to not exist when {{math|1=''k'' = 2}}. In this case the probability that the GCD equals {{math|''d''}} is {{math|''d''<sup>−2</sup>/''ζ''(2)}}, and since {{math|1=''ζ''(2) = π<sup>2</sup>/6}} we have : <math>\mathrm{E}( \mathrm{2} ) = \sum_{d=1}^\infty d \frac{6}{\pi^2 d^2} = \frac{6}{\pi^2} \sum_{d=1}^\infty \frac{1}{d}.</math> This last summation is the [[Harmonic series (mathematics)|harmonic series]], which diverges. However, when {{math|''k'' ≥ 3}}, the expected value is well-defined, and by the above argument, it is : <math> \mathrm{E}(k) = \sum_{d=1}^\infty d^{1-k} \zeta(k)^{-1} = \frac{\zeta(k-1)}{\zeta(k)}. </math> For {{math|1=''k'' = 3}}, this is approximately equal to 1.3684. For {{math|1=''k'' = 4}}, it is approximately 1.1106. == In commutative rings == {{See also|Divisibility (ring theory)}} {{More citations needed section|date=August 2024}} The notion of greatest common divisor can more generally be defined for elements of an arbitrary [[commutative ring]], although in general there need not exist one for every pair of elements.<ref>{{cite book |last=Lovett |first=Stephen |date=2015 |title=Abstract Algebra: Structures and Applications |chapter=Divisibility in Commutative Rings |pages=267–318 |publisher=CRC Press |publication-place=Boca Raton |isbn=9781482248913}}</ref> * If {{mvar|R}} is a commutative ring, and {{mvar|a}} and {{mvar|b}} are in {{mvar|R}}, then an element {{mvar|d}} of {{mvar|R}} is called a ''common divisor'' of {{mvar|a}} and {{mvar|b}} if it divides both {{mvar|a}} and {{mvar|b}} (that is, if there are elements {{mvar|x}} and {{mvar|y}} in {{mvar|R}} such that ''d''·''x'' = ''a'' and ''d''·''y'' = ''b''). * If {{mvar|d}} is a common divisor of {{mvar|a}} and {{mvar|b}}, and every common divisor of {{mvar|a}} and {{mvar|b}} divides {{mvar|d}}, then {{mvar|d}} is called a ''greatest common divisor'' of {{mvar|a}} and ''b''. With this definition, two elements {{mvar|a}} and {{mvar|b}} may very well have several greatest common divisors, or none at all. If {{mvar|R}} is an [[integral domain]], then any two GCDs of {{mvar|a}} and {{mvar|b}} must be [[associate elements]], since by definition either one must divide the other. Indeed, if a GCD exists, any one of its associates is a GCD as well. Existence of a GCD is not assured in arbitrary integral domains. However, if {{mvar|R}} is a [[unique factorization domain]] or any other [[GCD domain]], then any two elements have a GCD. If {{mvar|R}} is a [[Euclidean domain]] in which euclidean division is given algorithmically (as is the case for instance when {{math|1=''R'' = ''F''[''X'']}} where {{mvar|F}} is a [[field (mathematics)|field]], or when {{mvar|R}} is the ring of [[Gaussian integer]]s), then greatest common divisors can be computed using a form of the Euclidean algorithm based on the division procedure. The following is an example of an integral domain with two elements that do not have a GCD: : <math>R = \mathbb{Z}\left[\sqrt{-3}\,\,\right],\quad a = 4 = 2\cdot 2 = \left(1+\sqrt{-3}\,\,\right)\left(1-\sqrt{-3}\,\,\right),\quad b = \left(1+\sqrt{-3}\,\,\right)\cdot 2.</math> The elements {{math|2}} and {{math|1 + {{sqrt|−3}}}} are two [[maximal common divisor]]s (that is, any common divisor which is a multiple of {{math|2}} is associated to {{math|2}}, the same holds for {{math|1 + {{sqrt|−3}}}}, but they are not associated, so there is no greatest common divisor of {{mvar|a}} and {{math|''b''}}. Corresponding to the Bézout property we may, in any commutative ring, consider the collection of elements of the form {{math|''pa'' + ''qb''}}, where {{mvar|p}} and {{mvar|q}} range over the ring. This is the [[ideal (ring theory)|ideal]] generated by {{mvar|a}} and {{mvar|b}}, and is denoted simply {{math|(''a'', ''b'')}}. In a ring all of whose ideals are principal (a [[principal ideal domain]] or PID), this ideal will be identical with the set of multiples of some ring element {{math|''d''}}; then this {{mvar|d}} is a greatest common divisor of {{mvar|a}} and {{math|''b''}}. But the ideal {{math|(''a'', ''b'')}} can be useful even when there is no greatest common divisor of {{mvar|a}} and {{math|''b''}}. (Indeed, [[Ernst Kummer]] used this ideal as a replacement for a GCD in his treatment of [[Fermat's Last Theorem]], although he envisioned it as the set of multiples of some hypothetical, or ''ideal'', ring element {{mvar|d}}, whence the ring-theoretic term.) == See also == * [[Bézout domain]] * [[Lowest common denominator]] * [[Unitary divisor]] == Notes == {{reflist}} == References == {{refbegin}} * {{cite book |last=Andrews |first=George E. |author-link=George Andrews (mathematician) |year=1994 |orig-year=1971 |title=Number Theory |publisher=Dover |isbn=978-0-486-68252-5 |url=https://books.google.com/books?id=eVwvvwZeBf4C }} * {{cite book | last1 = Hardy | first1 = G. H. | author1-link = G. H. Hardy | last2 = Wright | first2 = E. M. | author2-link = E. M. Wright | title = [[An Introduction to the Theory of Numbers]] | edition = Fifth | publisher = [[Oxford University Press]] | location = Oxford | year = 1979 | isbn = 978-0-19-853171-5}} * {{cite book | first = Calvin T. | last = Long | year = 1972 | title = Elementary Introduction to Number Theory | edition = 2nd | publisher = [[D. C. Heath and Company]] | location = Lexington | lccn = 77171950 }} * {{cite book | first1 = Anthony J. | last1 = Pettofrezzo | first2 = Donald R. | last2 = Byrkit | year = 1970 | title = Elements of Number Theory | publisher = [[Prentice Hall]] | location = Englewood Cliffs | lccn = 71081766}} {{refend}} == Further reading == {{refbegin}} * [[Donald Knuth]]. ''[[The Art of Computer Programming]]'', Volume 2: ''Seminumerical Algorithms'', Third Edition. Addison-Wesley, 1997. {{ISBN|0-201-89684-2}}. Section 4.5.2: The Greatest Common Divisor, pp. 333–356. * [[Thomas H. Cormen]], [[Charles E. Leiserson]], [[Ronald L. Rivest]], and [[Clifford Stein]]. ''[[Introduction to Algorithms]]'', Second Edition. MIT Press and McGraw-Hill, 2001. {{ISBN|0-262-03293-7}}. Section 31.2: Greatest common divisor, pp. 856–862. * [[Saunders Mac Lane]] and [[Garrett Birkhoff]]. ''A Survey of Modern Algebra'', Fourth Edition. MacMillan Publishing Co., 1977. {{ISBN|0-02-310070-2}}. 1–7: "The Euclidean Algorithm." {{refend}} == External links == * gcd(x,y) = y function graph: https://www.desmos.com/calculator/6nizzenog5 {{Number-theoretic algorithms}} [[Category:Multiplicative functions]] [[Category:Articles containing video clips]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite conference
(
edit
)
Template:Cite journal
(
edit
)
Template:Cite web
(
edit
)
Template:Clarify
(
edit
)
Template:Cn
(
edit
)
Template:Further
(
edit
)
Template:Harvtxt
(
edit
)
Template:ISBN
(
edit
)
Template:Isbn
(
edit
)
Template:Main
(
edit
)
Template:Math
(
edit
)
Template:More citations needed section
(
edit
)
Template:Mvar
(
edit
)
Template:Number-theoretic algorithms
(
edit
)
Template:OEIS
(
edit
)
Template:Refbegin
(
edit
)
Template:Refend
(
edit
)
Template:Reflist
(
edit
)
Template:Refn
(
edit
)
Template:Section link
(
edit
)
Template:See also
(
edit
)
Template:Short description
(
edit
)
Template:Slink
(
edit
)
Template:Use American English
(
edit
)
Template:Vanchor
(
edit
)