Skip to content

Commit 8c7ffb3

Browse files
authored
Clean up Euclidean algorithm article
1 parent e85d87b commit 8c7ffb3

File tree

1 file changed

+32
-34
lines changed

1 file changed

+32
-34
lines changed

src/algebra/euclid-algorithm.md

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
<!--?title The Euclidean algorithm for finding GCD (greatest common divisor) -->
1+
<!--?title The Euclidean algorithm for finding greatest common divisor -->
22

3-
# The Euclidean algorithm for finding GCD (greatest common divisor)
3+
# The Euclidean algorithm for finding greatest common divisor
44

5-
Euclid's algorithm solves the following problem: Given two non-negative integers $a$ and $b$, we require the greater common divisor i.e. the largest number which is a divisor of $a$ and $b$ simultaneously. It's commonly denoted by $gcd$.
5+
Given two non-negative integers $a$ and $b$, find their greatest common divisor, i.e. the largest number which is a divisor of both $a$ and $b$. It's commonly denoted by $gcd$.
66

7-
$$
8-
gcd(a, b) = \max_ {k = 1, \ldots \infty \ : \ k \mid a \ \& \ k \mid b} k
9-
$$
7+
$$gcd(a, b) = \max_ {k = 1 \ldots \infty \ : \ k \mid a \ \& \ k \mid b} k$$
108

11-
(here the symbol "$\mid$" denotes divisibility, i.e. “$k\mid a$” means “$k$ divides $a$”)
9+
(here the symbol "$\mid$" denotes divisibility, i.e. “$k \mid a$” means “$k$ divides $a$”)
1210

13-
When one of the numbers is zero, while the other is non zero, the greatest common divisor, by definition, is the second number. However, when both these numbers are zero, it is undefined(it can be an infinitely large number), but we can take it to be equal to zero. So we have a simple rule: if one of the numbers is zero, the other number is the gcd.
11+
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 we can define it to be zero. So we have a simple rule: if one of the numbers is zero, the greatest common divisor is the other number.
1412

15-
The Euclidean algorithm, discussed below, solves the problem of finding the greatest common divisor of two numbers $a$ and $b$ in $O(\log min(a, b))$.
13+
The Euclidean algorithm, discussed below, allows to find the greatest common divisor of two numbers $a$ and $b$ in $O(\log min(a, b))$.
1614

17-
The algorithm was first described in Euclid's “**Elements**” (circa 300 BC). However, it is possible that the algorithm has earlier origins.
15+
The algorithm was first described in Euclid's “Elements” (circa 300 BC), but it is possible that the algorithm has earlier origins.
1816

1917
## Algorithm
2018

21-
The algorithm is extremely simple and described by this formula:
19+
The algorithm is extremely simple:
2220

23-
$$ gcd(a, b) = \cases{a,&if $b = 0$\cr gcd(b, a \mod b),&otherwise \cr}$$
21+
$$gcd(a, b) = \cases{a,&if $b = 0$\cr gcd(b, a\mod b),&otherwise}$$
2422

2523
## Implementation
2624

@@ -41,7 +39,7 @@ int gcd (int a, int b) {
4139
}
4240
```
4341

44-
Finally, we present a non-recursive form of implementation of the algorithm:
42+
Finally, here is a non-recursive implementation:
4543

4644
```cpp
4745
int gcd (int a, int b) {
@@ -53,20 +51,20 @@ int gcd (int a, int b) {
5351
}
5452
```
5553
56-
## The Proof of correctness
54+
## Correctness Proof
5755
58-
First, observe that at each successive iteration of the Euclidean algorithm, it's second argument strictly decreases, therefore, since the arguments are always non-negative, the algorithm must terminate.
56+
First, notce that at each iteration of the Euclidean algorithm the second argument strictly decreases, therefore (since the arguments are always non-negative) the algorithm will always terminate.
5957
60-
For the proof of correctness, we need to show $gcd(a, b) = gcd(b, a \mod b)$ for all $a \geq 0$, $b > 0$.
58+
For the proof of correctness, we need to show that $gcd(a, b) = gcd(b, a \mod b)$ for all $a \geq 0$, $b > 0$.
6159
62-
We will show that the quantity on the left side of the equation, divides the quantity on it's right. Obviously, this would mean that the left and right sides are equal and prove Euclid's algorithm.
60+
We will show that the value on the left side of the equation divides the value on the right side and vice versa. Obviously, this would mean that the left and right sides are equal, which will prove Euclid's algorithm.
6361
64-
Let $d = gcd(a, b)$. Then, by definition $d\mid a$ and $d\mid b$.
62+
Let $d = gcd(a, b)$. Then by definition $d\mid a$ and $d\mid b$.
6563
66-
New writing the remainder on dividing $a$ by $b$:
64+
Now let's represent the remainder from dividing $a$ by $b$ as follows:
6765
6866
$$
69-
a \mod b = a - b \cdot \Bigl\lfloor\dfrac{a}{b}\Bigr\rfloor\qquad
67+
a \mod b = a - b \cdot \Bigl\lfloor\dfrac{a}{b}\Bigr\rfloor
7068
$$
7169
7270
But then, it follows:
@@ -75,57 +73,57 @@ $$
7573
d \mid (a \mod b)
7674
$$
7775
78-
So, remembering the statement $d \mid b$, we obtain the system:
76+
So, recalling that $d \mid b$, we obtain the system:
7977
8078
$$
8179
\cases{d \mid b,\cr d \mid (a \mod b)\cr}
8280
$$
8381
84-
We now use the fact that if for any three numbers $p$, $q$, and $r$, if $p\mid q$ and $p\mid r$ then $p\mid gcd(q, r)$, In our case, we get:
82+
Now we use the fact that for any three numbers $p$, $q$, $r$, if $p\mid q$ and $p\mid r$ then $p\mid gcd(q, r)$, In our case, we get:
8583
8684
$$
8785
d \mid gcd(b, a \mod b)
8886
$$
8987
90-
Or, by substituting $d$ by it's definition($gcd(a, b)$) we get:
88+
Substituting $d$ with its definition ($gcd(a, b)$) we get:
9189
9290
$$
9391
gcd(a, b) \mid gcd(b, a \mod b)
9492
$$
9593
96-
So, we have shown that the left side divides the right. The second half of the proof is similar.
94+
Thus we have shown that the left side of the original equation divides the right. The second half of the proof is similar.
9795
98-
## Time complexity
96+
## Time Complexity
9997
100-
The running time of the algorithm is estimated by Lame's theorem, which establishes a surprising connection between the euclidean algorithm and the Fibonacci sequence:
98+
The running time of the algorithm is estimated by Lame's theorem, which establishes a surprising connection between the Euclidean algorithm and the Fibonacci sequence:
10199
102-
If $a > b \geq 1$ and $b < F_n$ for some $n$, the euclidean algorithm performs no more than $n-2$ recursive calls.
100+
If $a > b \geq 1$ and $b < F_n$ for some $n$, the Euclidean algorithm performs at most $n-2$ recursive calls.
103101
104-
Moreover, one can show that the upper bound of this theorem is optimal. When $a = F_n$ and $b = F_{n-1}$, $gcd(a, b)$ performs $n-2$ recursive calls. In other words, consecutive fibonacci numbers are the worst case input for Euclid's algorithm.
102+
Moreover, it is possible to show that the upper bound of this theorem is optimal. When $a = F_n$ and $b = F_{n-1}$, $gcd(a, b)$ will perform exactly $n-2$ recursive calls. In other words, consecutive Fibonacci numbers are the worst case input for Euclid's algorithm.
105103
106-
Given that Fibonacci numbers grow exponentially(as a constant with respect to $n$), we find that the Euclidean algorithm is performed in $O(\log min(a, b))$
104+
Given that Fibonacci numbers grow exponentially (as a constant raised to $n$th power), we get that the Euclidean algorithm works in $O(\log min(a, b))$ multiplication operations.
107105
108-
## LCM(Least common multiple)
106+
## LCM (least common multiple)
109107
110-
Calculating least common multiple (least common multiplier or LCM) is reduced to calculating $gcd$ with the following simple statement:
108+
Calculating least common multiple (commonly denoted LCM) is reduced to calculating $gcd$ with the following simple formula:
111109
112110
$$
113111
lcm(a, b) = \dfrac{a \cdot b}{gcd(a, b)}
114112
$$
115113
116-
Thus, the calculation of the LCM can also be done using the euclidean algorithm with the same time complexity
114+
Thus, LCM can be calculated using the Euclidean algorithm with the same time complexity:
117115
118116
```cpp
119117
int lcm (int a, int b) {
120118
return a / gcd (a, b) * b;
121119
}
122120
```
123-
(here it is advantageous to first divide $gcd$ and then multiply by $b$, as it helps avoid overflow in some cases)
121+
(here it is advantageous to first divide by $gcd$ and then multiply by $b$ to avoid overflow in some cases)
124122

125123
## Literature
126124

127125
- Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Algorithms: Construction and analysis [2005]
128126

129127
## Practice Problems
130128

131-
- [GCD and LCM[easy]](https://www.codechef.com/problems/FLOW016)
129+
- [GCD and LCM [easy]](https://www.codechef.com/problems/FLOW016)

0 commit comments

Comments
 (0)