Why the heck does Euclidean Algorithm work
The Euclidean algorithm is a method for finding the greatest common divisor (GCD) of two numbers, which is the largest number that divides both of them without leaving a remainder. The algorithm works by repeatedly applying the following process:
- Start with two numbers, say
a
andb
, where a≥b. - Divide
a
byb
and find the remainder. Let’s call the remainderr
. - Replace
a
withb
andb
withr
. - Repeat the process until the remainder
r
becomes 0. At that point,b
will be the greatest common divisor (GCD) of the originala
andb
.
Euclidean Algorithm Example
We want to find the greatest common divisor (GCD) of 252 and 105.
-
Initial step: Start with
and . - Divide
by : with a remainder of . - So,
.
- Divide
-
Second step: Replace
with 105 and with 42. - Divide
by : with a remainder of . - So,
.
- Divide
-
Third step: Replace
with 42 and with 21. - Divide
by : with a remainder of . - So,
.
- Divide
-
Conclusion: Since the remainder is now 0, the GCD is the last non-zero remainder, which is 21.
Thus, the GCD of 252 and 105 is 21.
Proof
The Euclidean algorithm is used to find the greatest common divisor (GCD) of two numbers. Here’s a step-by-step proof of why it works and why it gives the largest common divisor.
Step 1: Divisibility Property
Let
where
Divisibility
Let
We know that the remainder
where
Since
Thus,
Step 2: GCD Reduction
We now argue that the greatest common divisor of
Since
Thus,
Step 3: Termination
The Euclidean algorithm continues to reduce the size of the numbers by replacing
Therefore, the Euclidean algorithm guarantees that:
and this process will eventually terminate when the remainder becomes 0, at which point the GCD will be found.
Step 4: Maximality (Proving it is the Largest Common Divisor)
We will now prove that the GCD is the largest divisor of both
Proof by Contradiction
Assume there exists a divisor
Thus, the assumption that
This proves that the Euclidean algorithm not only finds a common divisor but also the largest one, making it correct and efficient.
Python Code
def get_gcd(x, y):
while y:
x, y = y, x%y
return x