|
6520 | 6520 |
|
6521 | 6521 |
|
6522 | 6522 |
|
| 6523 | + |
| 6524 | + |
| 6525 | + |
| 6526 | + |
| 6527 | + |
| 6528 | + |
6523 | 6529 |
|
6524 | 6530 |
|
6525 | 6531 |
|
|
6620 | 6626 |
|
6621 | 6627 |
|
6622 | 6628 |
|
| 6629 | + |
| 6630 | + |
| 6631 | + |
| 6632 | + |
6623 | 6633 |
|
6624 | 6634 |
|
6625 | 6635 |
|
|
6632 | 6642 | <ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
|
6633 | 6643 |
|
6634 | 6644 | Last update:
|
6635 |
| - <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">July 11, 2024</span>  |
| 6645 | + <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date">October 15, 2024</span>  |
6636 | 6646 |
|
6637 | 6647 | <!-- Tags -->
|
6638 | 6648 |
|
@@ -6664,7 +6674,7 @@ <h1 id="euclidean-algorithm-for-computing-the-greatest-common-divisor">Euclidean
|
6664 | 6674 | <div class="arithmatex">$$\gcd(a, b) = \max \{k > 0 : (k \mid a) \text{ and } (k \mid b) \}$$</div>
|
6665 | 6675 | <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>
|
6666 | 6676 | <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> |
6668 | 6678 | <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>
|
6669 | 6679 | <h2 id="algorithm">Algorithm<a class="headerlink" href="#algorithm" title="Permanent link">¶</a></h2>
|
6670 | 6680 | <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
|
6698 | 6708 | <p>If <span class="arithmatex">$a > b \geq 1$</span> and <span class="arithmatex">$b < 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>
|
6699 | 6709 | <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>
|
6700 | 6710 | <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> |
6702 | 6712 | <h2 id="least-common-multiple">Least common multiple<a class="headerlink" href="#least-common-multiple" title="Permanent link">¶</a></h2>
|
6703 | 6713 | <p>Calculating the least common multiple (commonly denoted <strong>LCM</strong>) can be reduced to calculating the GCD with the following simple formula:</p>
|
6704 | 6714 | <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
|
6744 | 6754 |
|
6745 | 6755 | <ul class="metadata page-metadata" data-bi-name="page info" lang="en-us" dir="ltr">
|
6746 | 6756 | <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> |
6748 | 6758 | </ul>
|
6749 | 6759 |
|
6750 | 6760 | </article>
|
|
0 commit comments