Skip to content

Commit 8821b68

Browse files
Add Euclidean algorithm (#201)
Co-authored-by: David Leal <[email protected]>
1 parent d8c2cb0 commit 8821b68

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

en/Basic Math/Euclidean algorithm.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Euclidean algorithm
2+
3+
## Problem
4+
5+
Find the Greatest Common Divisor (GCD) of two positive integers $a$ and $b$, which is defined as the largest number $g = gcd(a, b)$ such that $g$ divides both $a$ and $b$ without remainder.
6+
7+
## Idea
8+
9+
The Euclidean algorithm is based on the simple observation that the GCD of two numbers doesn't change if the smaller number is subtracted from the larger number:
10+
11+
Let $a > b$. Let $g$ be the GCD of $a$ and $b$.
12+
Then $g$ divides $a$ and $b$. Thus $g$ also divides $a - b$.
13+
14+
Let $g'$ be the GCD of $b$ and $a - b$.
15+
16+
Proof by contradiction that $g' = g$:
17+
18+
Assume $g' < g$ or $g' > g$.
19+
20+
If $g' < g$, $g'$ would not be the *greatest* common divisor,
21+
since $g$ is also a common divisor of $a - b$ and $b$.
22+
23+
If $g' > g$, $g'$ divides $b$ and $a - b$ -
24+
that is, there exist integers $n, m$
25+
such that $g'n = b$ and $g'm = a - b$.
26+
Thus $g'm = a - g'n \iff g'm + g'n = a \iff g'(m + n) = a$.
27+
This imples that $g' > g$ also divides $a$,
28+
which contradicts the initial assumption that $g$ is the GCD of $a$ and $b$.
29+
30+
## Implementation
31+
32+
To speed matters up in practice, modulo division is used instead of repeated subtractions:
33+
$b$ can be subtracted from $a$ as long as $a >= b$.
34+
After these subtractions only the remainder of $a$ when divided by $b$ remains.
35+
36+
A straightforward Lua implementation might look as follows:
37+
38+
```lua
39+
function gcd(a, b)
40+
while b ~= 0 do
41+
a, b = b, a % b
42+
end
43+
return a
44+
end
45+
```
46+
47+
note that `%` is the modulo/remainder operator;
48+
`a` is assigned to the previous value of `b`,
49+
and `b` is assigned to the previous value of `a`
50+
modulo the previous value of `b` in each step.
51+
52+
## Analysis
53+
54+
### Space Complexity
55+
56+
The space complexity can trivially be seen to be constant:
57+
Only two numbers (of assumed constant size), $a$ and $b$, need to be stored.
58+
59+
### Time Complexity
60+
61+
Each iteration of the while loop runs in constant time and at least halves $b$.
62+
Thus $O(log_2(n))$ is an upper bound for the runtime.
63+
64+
## Walkthrough
65+
66+
Finding the GCD of $a = 42$ and $b = 12$:
67+
68+
1. $42 \mod 12 = 6$
69+
2. $12 \mod 6 = 0$
70+
71+
The result is $gcd(42, 12) = 6$.
72+
73+
Finding the GCD of $a = 633$ and $b = 142$ using the Euclidean algorithm:
74+
75+
1. $633 \mod 142 = 65$
76+
2. $142 \mod 65 = 12$
77+
3. $65 \mod 12 = 5$
78+
4. $12 \mod 5 = 2$
79+
5. $5 \mod 2 = 1$
80+
6. $2 \mod 1 = 0$
81+
82+
The result is $gcd(633, 142) = 1$: $a$ and $b$ are co-prime.
83+
84+
## Applications
85+
86+
* Shortening fractions
87+
* Finding the Least Common Multiple (LCM)
88+
* Efficiently checking whether two numbers are co-prime (needed e.g. for the RSA cryptosystem)
89+
90+
## Resources
91+
92+
* [Wikipedia Article](https://en.wikipedia.org/wiki/Euclidean_algorithm)

0 commit comments

Comments
 (0)