You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<!--?title The Euclidean algorithm for finding GCD (greatest common divisor)-->
1
+
<!--?title The Euclidean algorithm for finding greatest common divisor -->
2
2
3
-
# The Euclidean algorithm for finding GCD (greatest common divisor)
3
+
# The Euclidean algorithm for finding greatest common divisor
4
4
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$.
6
6
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$$
10
8
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$”)
12
10
13
-
When one of the numbers is zero, while the other is nonzero, 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.
14
12
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))$.
16
14
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.
18
16
19
17
## Algorithm
20
18
21
-
The algorithm is extremely simple and described by this formula:
$$gcd(a, b) = \cases{a,&if $b = 0$\cr gcd(b, a\mod b),&otherwise}$$
24
22
25
23
## Implementation
26
24
@@ -41,7 +39,7 @@ int gcd (int a, int b) {
41
39
}
42
40
```
43
41
44
-
Finally, we present a non-recursive form of implementation of the algorithm:
42
+
Finally, here is a non-recursive implementation:
45
43
46
44
```cpp
47
45
intgcd (int a, int b) {
@@ -53,20 +51,20 @@ int gcd (int a, int b) {
53
51
}
54
52
```
55
53
56
-
## The Proof of correctness
54
+
## Correctness Proof
57
55
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.
59
57
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$.
61
59
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.
63
61
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$.
65
63
66
-
New writing the remainder on dividing $a$ by $b$:
64
+
Now let's represent the remainder from dividing $a$ by $b$ as follows:
67
65
68
66
$$
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
70
68
$$
71
69
72
70
But then, it follows:
@@ -75,57 +73,57 @@ $$
75
73
d \mid (a \mod b)
76
74
$$
77
75
78
-
So, remembering the statement $d \mid b$, we obtain the system:
76
+
So, recalling that $d \mid b$, we obtain the system:
79
77
80
78
$$
81
79
\cases{d \mid b,\cr d \mid (a \mod b)\cr}
82
80
$$
83
81
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:
85
83
86
84
$$
87
85
d \mid gcd(b, a \mod b)
88
86
$$
89
87
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:
91
89
92
90
$$
93
91
gcd(a, b) \mid gcd(b, a \mod b)
94
92
$$
95
93
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.
97
95
98
-
## Time complexity
96
+
## Time Complexity
99
97
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:
101
99
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.
103
101
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.
105
103
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.
107
105
108
-
## LCM(Least common multiple)
106
+
## LCM (least common multiple)
109
107
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:
111
109
112
110
$$
113
111
lcm(a, b) = \dfrac{a \cdot b}{gcd(a, b)}
114
112
$$
115
113
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:
117
115
118
116
```cpp
119
117
int lcm (int a, int b) {
120
118
return a / gcd (a, b) * b;
121
119
}
122
120
```
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)
124
122
125
123
## Literature
126
124
127
125
- Thomas Cormen, Charles Leiserson, Ronald Rivest, Clifford Stein. Algorithms: Construction and analysis [2005]
128
126
129
127
## Practice Problems
130
128
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