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
(section)
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!
== 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>
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)