Euclidean Algorithm

Introduction

Most of the modern cryptosystems were developed based on the fact that it is extremely inefficient to factoring large composite numbers, usually in exponential complexity. Therefore, breaking these cryptosystems have been extremely hard. With a quantum computer, the quantum Shor’s algorithm is able to factorize large composite numbers in polynomial complexity. However, the Shor’s algorithm does have a fraction that uses classical algorithm to find the greatest common divisor of two integers. The most efficient classical algorithms to find the greatest common divisor of two integers is Euclidean algorithm and its variants.

In this blog post, I would like to discuss the Euclidean algorithm and systematically prove its usefulness.

Prerequisites

Greatest Common Divisor Reduction

Let a=bq+r, where a, b, q, and r are integers. Then gcd(a,b)=gcd(b,r).

Proof

Suppose d|a and d|b, then we have

a=dk1b=dk2

for some k1,k2N.

r=abq=dk1dk2q=d(k1k2q)

This means that any common divisor for a and b must also be a divisor for r, and further any common divisor for a and b must also be a common divisor for b and r. So one of the common divisors for a and b, gcd(a,b), is a common divisor for b and r. Since the greatest common divisor for b and r is gcd(b,r), then we have gcd(a,b)gcd(b,r).

Similarly, we could prove that any common divisor for b and r must also be a divisor for a, and further any common divisor for b and r must also be a common divisor for a and b. So one of the common divisors for b and r, gcd(b,r), is a common divisor for a and b. Since the greatest common divisor for a and b is gcd(a,b), then we have gcd(b,r)gcd(a,b).

Because gcd(a,b)gcd(b,r) and gcd(b,r)gcd(a,b), we must have gcd(a,b)=gcd(b,r).

This concludes the proof.

Fibonacci Sequence

The numbers in the Fibonacci sequence, f0,f1,f2,f3,, given f0=0 and f1=1, are defined as

fn=fn1+fn2

for integer n2.

The Fibonacci sequence has a property that is useful for proving the usefulness of the Euclidean algorithm.

fn>αn2 for n3 where α=1+52.

Proof

We could prove this property using strong induction. Let P(n) be the statement that fn>αn2. We would like to show that P(n) is true for n3.

Basis Step: it is easy to verify that P(3) and P(4) are true.

Induction Step: Assume P(j), namely fj>αj2, is true for all integers j with 3j where k4, we must show P(k+1) is true.

Because α2=α+1,

αk1=α2αk3=(α+1)αk3=αk2+αk3

Because of the induction hypothesis, we have

fk1>αk3fk>αk2

Therefore,

fk+1=fk+fk1>αk2+αk3=αk1

Therefore, fk+1>αk1, P(k+1) is true. This concludes the proof.

Euclidean Algorithm

Based on the property of the greatest common divisor reduction in the prerequisites, the greatest common divisor problem could be solved recursively. Instead of using recursively function, we implemented the function in a iterative manner.

Euclidean Algorithm Pseudocode

When the remainder r becomes 0, gcd(b,r)=gcd(b,0)=b, we finish the greatest common divisor computation.

1
2
3
4
5
6
function gcd(a, b)
while b ≠ 0
t := b
b := a mod b
a := t
return a

It does not matter if a<b. If a<b, after the first iteration, the value of a and b will switch.

One might ask why the remainder r in Euclidean algorithm will eventually become 0 and the iteration will eventually stop. This will become more clear in our proof for the Lamé’s theorem.

Euclidean Algorithm Implementation

gcd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int gcd(int a, int b)
{
int t;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
return a;
}

int main()
{
int a = 6;
int b = 15;
std::cout << gcd(6, 15) << std::endl;
}
1
2
3
$ g++ gcd.cpp -o gcd
$ ./gcd
3

Lamé’s Theorem

Let a and b be positive integers with ab. Then the number of divisions used by the Euclidean algorithm to find gcd(a,b) is less than or equal to five times the number of decimal digits in b.

Proof

Suppose the Euclidean algorithm is applied to find gcd(a,b) with ab. In practice, even if a<b, because a=0b+a, gcd(a,b)=gcd(b,a), computing gcd(a,b) is equivalent to computing gcd(b,a) with ba.

We initialize r0=a and r1=b.

r0=r1q1+r20r2<r1r1=r2q2+r30r3<r2rn2=rn1qn1+rn0rn<rn1rn1=rnqn

There must exist an ending condition where rn1=rnqn. Because if there is no such ending condition r1>r2>>rk>>0, However, because r1 is finite and all ri is positive integers for i0, there must exist a rj for some j0 so that rj0. This raises a contradiction. Therefore, there must exist an ending condition where rn1=rnqn. This also answers the question for the Euclidean algorithm why the remainder r in Euclidean algorithm will eventually become 0 and the iteration will eventually stop.

Note that the quotients q1,q2,,qn1,qn1 because ri>ri+1 for i[1,n1]. In addition, qn2 because rn1>rn and rn divides rn1. In addition, ri1 for i[1,n]. This implies that

rn1=f2rn12rn2f2=2=f3rn2rn1+rnf3+f2=f4r2r3+r4fn1+fn2=fnb=r1r2+r3fn+fn1=fn+1

where fi is the ith number in the Fibonacci sequence, n is the number of iterations for the Euclidean algorithm. We are interested to know what the upper bound of the value of n is given b.

Using the special property of Fibonacci sequence we have just proved in the prerequisites section, fn>αn2 for n3 where α=1+52.

bfn+1>αn1

Furthermore, because log10α0.208>15

log10b>(n1)log10α>n15

Thus, we have

n<5log10b+1

We further have

n5(log10b+1)

where log10b+1 is the number of decimal digits in b, and n is the number of divisions used by the Euclidean algorithm to find gcd(a,b). This concludes the proof for the Lamé’s theorem.

Asymptotic Complexity

Based on the Lamé’s theorem, the asymptotic complexity for the Euclidean algorithm to compute gcd(a,b) when ab is O(h) where h is the number of digits in b.

Euclidean Algorithm Variants

There are Euclidean algorithm variants, such as the extended Euclidean algorithm, which are actually used in practice instead of the vanilla Euclidean algorithm, due to their efficiency in computation. However, since they don’t improve the asymptotic complexity compared to the vanilla Euclidean algorithm, we are not going into the details for the moment.

References

Author

Lei Mao

Posted on

09-12-2020

Updated on

09-12-2020

Licensed under


Comments