Skip to content

Commit 23f44db

Browse files
1 parent 41b1f83 commit 23f44db

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

main/algebra/euclid-algorithm.html

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6520,6 +6520,12 @@
65206520

65216521

65226522

6523+
6524+
6525+
6526+
6527+
6528+
65236529

65246530

65256531

@@ -6620,6 +6626,10 @@
66206626

66216627

66226628

6629+
6630+
6631+
6632+
66236633

66246634

66256635

@@ -6632,7 +6642,7 @@
66326642
<ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
66336643

66346644
Last update:
6635-
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">July 11, 2024</span>&emsp;
6645+
<span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">October 15, 2024</span>&emsp;
66366646

66376647
<!-- Tags -->
66386648

@@ -6664,7 +6674,7 @@ <h1 id="euclidean-algorithm-for-computing-the-greatest-common-divisor">Euclidean
66646674
<div class="arithmatex">$$\gcd(a, b) = \max \{k &gt; 0 : (k \mid a) \text{ and } (k \mid b) \}$$</div>
66656675
<p>(here the symbol "<span class="arithmatex">$\mid$</span>" denotes divisibility, i.e. "<span class="arithmatex">$k \mid a$</span>" means "<span class="arithmatex">$k$</span> divides <span class="arithmatex">$a$</span>")</p>
66666676
<p>When one of the numbers is zero, while the other is non-zero, their greatest common divisor, by definition, is the second number. When both numbers are zero, their greatest common divisor is undefined (it can be any arbitrarily large number), but it is convenient to define it as zero as well to preserve the associativity of <span class="arithmatex">$\gcd$</span>. Which gives us a simple rule: if one of the numbers is zero, the greatest common divisor is the other number.</p>
6667-
<p>The Euclidean algorithm, discussed below, allows to find the greatest common divisor of two numbers <span class="arithmatex">$a$</span> and <span class="arithmatex">$b$</span> in <span class="arithmatex">$O(\log \min(a, b))$</span>.</p>
6677+
<p>The Euclidean algorithm, discussed below, allows to find the greatest common divisor of two numbers <span class="arithmatex">$a$</span> and <span class="arithmatex">$b$</span> in <span class="arithmatex">$O(\log \min(a, b))$</span>. Since the function is <strong>associative</strong>, to find the GCD of <strong>more than two numbers</strong>, we can do <span class="arithmatex">$\gcd(a, b, c) = \gcd(a, \gcd(b, c))$</span> and so forth.</p>
66686678
<p>The algorithm was first described in Euclid's "Elements" (circa 300 BC), but it is possible that the algorithm has even earlier origins.</p>
66696679
<h2 id="algorithm">Algorithm<a class="headerlink" href="#algorithm" title="Permanent link">&para;</a></h2>
66706680
<p>Originally, the Euclidean algorithm was formulated as follows: subtract the smaller number from the larger one until one of the numbers is zero. Indeed, if <span class="arithmatex">$g$</span> divides <span class="arithmatex">$a$</span> and <span class="arithmatex">$b$</span>, it also divides <span class="arithmatex">$a-b$</span>. On the other hand, if <span class="arithmatex">$g$</span> divides <span class="arithmatex">$a-b$</span> and <span class="arithmatex">$b$</span>, then it also divides <span class="arithmatex">$a = b + (a-b)$</span>, which means that the sets of the common divisors of <span class="arithmatex">$\{a, b\}$</span> and <span class="arithmatex">$\{b,a-b\}$</span> coincide.</p>
@@ -6698,7 +6708,7 @@ <h2 id="time-complexity">Time Complexity<a class="headerlink" href="#time-comple
66986708
<p>If <span class="arithmatex">$a &gt; b \geq 1$</span> and <span class="arithmatex">$b &lt; F_n$</span> for some <span class="arithmatex">$n$</span>, the Euclidean algorithm performs at most <span class="arithmatex">$n-2$</span> recursive calls.</p>
66996709
<p>Moreover, it is possible to show that the upper bound of this theorem is optimal. When <span class="arithmatex">$a = F_n$</span> and <span class="arithmatex">$b = F_{n-1}$</span>, <span class="arithmatex">$gcd(a, b)$</span> will perform exactly <span class="arithmatex">$n-2$</span> recursive calls. In other words, consecutive Fibonacci numbers are the worst case input for Euclid's algorithm.</p>
67006710
<p>Given that Fibonacci numbers grow exponentially, we get that the Euclidean algorithm works in <span class="arithmatex">$O(\log \min(a, b))$</span>.</p>
6701-
<p>Another way to estimate the complexity is to notice that <span class="arithmatex">$a \bmod b$</span> for the case <span class="arithmatex">$a \geq b$</span> is at least <span class="arithmatex">$2$</span> times smaller than <span class="arithmatex">$a$</span>, so the larger number is reduced at least in half on each iteration of the algorithm.</p>
6711+
<p>Another way to estimate the complexity is to notice that <span class="arithmatex">$a \bmod b$</span> for the case <span class="arithmatex">$a \geq b$</span> is at least <span class="arithmatex">$2$</span> times smaller than <span class="arithmatex">$a$</span>, so the larger number is reduced at least in half on each iteration of the algorithm. Applying this reasoning to the case when we compute the GCD of the set of numbers <span class="arithmatex">$a_1,\dots,a_n \leq C$</span>, this also allows us to estimate the total runtime as <span class="arithmatex">$O(n + \log C)$</span>, rather than <span class="arithmatex">$O(n \log C)$</span>, since every non-trivial iteration of the algorithm reduces the current GCD candidate by at least a factor of <span class="arithmatex">$2$</span>.</p>
67026712
<h2 id="least-common-multiple">Least common multiple<a class="headerlink" href="#least-common-multiple" title="Permanent link">&para;</a></h2>
67036713
<p>Calculating the least common multiple (commonly denoted <strong>LCM</strong>) can be reduced to calculating the GCD with the following simple formula:</p>
67046714
<div class="arithmatex">$$\text{lcm}(a, b) = \frac{a \cdot b}{\gcd(a, b)}$$</div>
@@ -6744,7 +6754,7 @@ <h2 id="practice-problems">Practice Problems<a class="headerlink" href="#practic
67446754

67456755
<ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
67466756
<span class="contributors-text">Contributors:</span>
6747-
<ul class="contributors" data-bi-name="contributors"><li><a href="https://github.com/jakobkogler" title="jakobkogler" data-bi-name="contributorprofile" target="_blank">jakobkogler</a> (48.06%)</li><li><a href="#" title="Nalin Bhardwaj" data-bi-name="contributorprofile" target="_blank">Nalin Bhardwaj</a> (34.11%)</li><li><a href="https://github.com/adamant-pwn" title="adamant-pwn" data-bi-name="contributorprofile" target="_blank">adamant-pwn</a> (11.63%)</li><li><a href="https://github.com/tcNickolas" title="tcNickolas" data-bi-name="contributorprofile" target="_blank">tcNickolas</a> (3.88%)</li><li><a href="https://github.com/AniketR10" title="AniketR10" data-bi-name="contributorprofile" target="_blank">AniketR10</a> (0.78%)</li><li><a href="https://github.com/itssachinkr" title="itssachinkr" data-bi-name="contributorprofile" target="_blank">itssachinkr</a> (0.78%)</li><li><a href="https://github.com/jinyulink" title="jinyulink" data-bi-name="contributorprofile" target="_blank">jinyulink</a> (0.78%)</li></ul>
6757+
<ul class="contributors" data-bi-name="contributors"><li><a href="https://github.com/jakobkogler" title="jakobkogler" data-bi-name="contributorprofile" target="_blank">jakobkogler</a> (47.29%)</li><li><a href="#" title="Nalin Bhardwaj" data-bi-name="contributorprofile" target="_blank">Nalin Bhardwaj</a> (34.11%)</li><li><a href="https://github.com/adamant-pwn" title="adamant-pwn" data-bi-name="contributorprofile" target="_blank">adamant-pwn</a> (10.85%)</li><li><a href="https://github.com/tcNickolas" title="tcNickolas" data-bi-name="contributorprofile" target="_blank">tcNickolas</a> (3.88%)</li><li><a href="https://github.com/mdeapedro" title="mdeapedro" data-bi-name="contributorprofile" target="_blank">mdeapedro</a> (1.55%)</li><li><a href="https://github.com/AniketR10" title="AniketR10" data-bi-name="contributorprofile" target="_blank">AniketR10</a> (0.78%)</li><li><a href="https://github.com/itssachinkr" title="itssachinkr" data-bi-name="contributorprofile" target="_blank">itssachinkr</a> (0.78%)</li><li><a href="https://github.com/jinyulink" title="jinyulink" data-bi-name="contributorprofile" target="_blank">jinyulink</a> (0.78%)</li></ul>
67486758
</ul>
67496759

67506760
</article>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy