Skip to content
Euclidean Algorithm

Euclidean Algorithm

Euclidean algorithm, or Euclid's algorithm, is an efficient method for computing the GCD(greatest common divisor)

Implementation

1
2
3
4
int gcd(int a, int b) {
    if(!a) return b;
    return gcd(b % a, a);
}

Proof

This proof consists of two parts.
- part 1: proves that Euclidean Algorithm gives us a common factor of two integers.
- part 2: proves that the common divisor that Euclidean Algorithm produces is the largest possible

Part1

Euclidean Algorithm gives us a common factor of two integers(a,ba, b).

a=q1b+r1,where(0<r<b)b=q2r1+r2,where(0<r2<r1)r1=q3r2+r3,where(0<r3<r2)ri=qi+2ri+1+ri+2,where(0<ri+2<ri+1)rk2=qkrk1+rkrk1=qk+1rk \begin{aligned} a & = q_1b + r_1, &\text{where}(0<r<b)& \\ b & = q_2r_1 + r_2, &\text{where}(0<r_2<r_1)& \\ r_1 & = q_3r_2 + r_3, &\text{where}(0<r_3<r_2)&\\ &\vdots \\ r_i & = q_{i+2}r_{i+1} + r_{i+2}, & \text{where}( 0 < r_{i+2} < r_{i+1})& \\ &\vdots \\ r_{k-2} & = q_{k}r_{k-1} + r_{k}\\ r_{k-1} & = q_{k+1}r_k \\ \end{aligned} From the last euqation, we know that rkrk1r_k|r_{k-1}. So, we know that we can express rk1=crkr_{k-1} = cr_k. where cc is an integer. Now consider the previous equation. rk2=qkrk1+rk=qkcrk+rk=rk(qkc+1) \begin{aligned} r_{k-2} & = q_{k} r_{k-1} + r_{k} \\ & = q_{k} cr_{k} + r_{k} \\ & = r_{k} (q_{k}c + 1) \\ \end{aligned} Thus, we have that rkrk2r_{k} | r_{k-2}. In our equation previous to that one, we have rk3=qk1rk2+rk1 r_{k-3} = q_{k-1} r_{k-2} + r_{k-1} From here, since rkrk1r_{k} | r_{k-1} and rkrk2r_{k}| r{k-2}, using our rules of divisibility we have that rkrk3r_{k} | r_{k-3}. As you can see, we can continue this process, considering each previous equation until we get to the last two, where we will find that rkar_{k} | a and rkbr_{k} | b. Thus, we find that Euclids algorithm gives us a common factor of a and b.

Part2

The common divisor Euclidean Algorithm produces is the largest possible.

We will start by assuming that aa and bb have a common factor dd, and then show that drkd | r_{k}.

consider an arbitrary common factor d of aa and bb. If dd is a common factor, we can rewrite aa and bb as follows:

a=da,b=db,where d,a,b are all positive integers  a = d a^{\prime}, b = d b^{\prime}, \text{where } d, a, b \text{ are all positive integers }

Now, consider the first euqation from Euclidean algorithm:

a=q1b+r1r1=aq1b=daq1db=d(aq1b) \begin{aligned} a & = q_{1} b + r_{1} \\ r_{1} & = a - q_{1}b \\ & = da^{\prime} - q_{1} d b^{\prime} \\ & = d(a^{\prime} - q_{1}b^{\prime}) \end{aligned}

Thus, we have that dr1d|r_{1}. Now, consider the second equation, and repeat the steps we did on the first, this time solving for r2r_{2}

b=q2r1+r2r2=bq2r1=dbq2dr1=d(bq2r1) \begin{aligned} b & = q_{2}r_{1} + r_{2} \\ r_{2} & = b - q_{2}r_{1} \\ & = d b^{\prime} - q_{2}dr_1^{\prime} \\ & = d (b^{\prime} - q_{2} r_1^{\prime}) \end{aligned}

As you can see, we can continue this process through each of the equations until we hit the second to last one, where we will have

rk2=qkrk1+rkrk=qkrk1rk2=qkdrk1drk2=d(qkrk1rk2) \begin{aligned} r_{k-2} & = q_{k}r_{k-1} + r_{k} \\ r_{k} & = q_{k}r_{k-1} - r_{k-2} \\ & = q_{k}dr_{k-1}^{\prime} - d r_{k-2}^{\prime} \\ & = d(q_{k}r_{k-1}^{\prime} - r_{k-2}^{\prime}) \end{aligned} Thus, drkd| r_k

But this says that any arbitrary common factor of aa and bb that we originally picked divides into rkr_{k}, the value that Euclidean algorithm produced. Since we know that rkr_{k} is a common factor to both aa and bb, this shows that is must be the largest possible common factor, or gcd(a,b)gcd(a, b) \blacksquare

Extended Euclidean Algorithm

Given integers aa and bb, there is always an integral solution to the equation ax+by=gcd(a,b) ax + by = gcd(a, b) and we can find the values of xx and yy.

implementation

not yet

Proof

Consider writing down the steps of Euclidean Algorithm

a=q1b+r1,where(0<r<b)b=q2r1+r2,where(0<r2<r1)r1=q3r2+r3,where(0<r3<r2)ri=qi+2ri+1+ri+2,where(0<ri+2<ri+1)rk2=qkrk1+rkwhere(0<rk<rk1)rk1=qk+1rk \begin{aligned} a & = q_1b + r_1, &\text{where}(0<r<b)& \\ b & = q_2r_1 + r_2, &\text{where}(0<r_2<r_1)& \\ r_1 & = q_3r_2 + r_3, &\text{where}(0<r_3<r_2)&\\ &\vdots \\ r_i & = q_{i+2}r_{i+1} + r_{i+2}, & \text{where}( 0 < r_{i+2} < r_{i+1})& \\ &\vdots \\ r_{k-2} & = q_{k}r_{k-1} + r_{k} & \text{where}(0 < r_k < r_{k-1}) \\ r_{k-1} & = q_{k+1}r_k \\ \end{aligned}

Consider solving the second to last euqation for rkr_k. You get

rk2=qkrk1+rkrk=rk2qkrk1gcd(a,b)=rk2qkrk1 \begin{aligned} r_{k-2} & = q_{k}r_{k-1} + r_{k} \\ r_{k} & = r_{k-2} - q_{k}r_{k-1} \\ gcd(a, b) & = r_{k-2} - q_{k}r_{k-1} \end{aligned}

Now, solve the previous equation for rk1r_{k-1}

rk3=qk1rk2+rk1rk1=rk3qk1rk2 \begin{aligned} r_{k-3} & = q_{k-1}r_{k-2} + r_{k-1} \\ r_{k-1} & = r_{k-3} - q_{k-1}r_{k-2} \\ \end{aligned}

and we substitute this value in to the previous derived equation

gcd(a,b)=rk2qkrk1=rk2qk(rk3qk1rk2)=rk2(1qk1)qkrk3 \begin{aligned} gcd(a, b) & = r_{k-2} - q_{k}r_{k-1} \\ & = r_{k-2} - q_k(r_{k-3} - q_{k-1}r_{k-2}) \\ & = r_{k-2}(1 - q_{k-1}) - q_kr_{k-3} \end{aligned}

Notice that now we have expressed gcd(a,b)gcd(a, b) as a linear combination of rk2r_{k-2} and rk3r_{k-3}. Next we can substitute for of rk2r_{k-2} in terms of rk3r_{k-3} and rk4r_{k-4}, so that the gcd(a,b)gcd(a, b) can be expressed as the linear combination of rk3r_{k-3} and rk4r_{k-4}. Eventually, by continuing this process, gcd(a,b)gcd(a, b) will be expressed as a linear combination of aa and bb as desired.

This process will be much easier to see with examples:

Find integers xx and yy such that

135x+50y=5 135x + 50y = 5

Use Euclidean Algorithm to compute gcd(135,50)gcd(135, 50)

135=250+35150=135+15235=215+5315=35 \begin{aligned} 135 & = 2 \sdot 50 + 35 \cdots\text{\textcircled 1}\\ 50 & = 1 \sdot 35 + 15 \cdots\text{\textcircled 2}\\ 35 & = 2 \sdot 15 + 5 \cdots\text{\textcircled 3}\\ 15 & = 3 \sdot 5 \end{aligned}

Now, let's use the Extended Euclidean algorithm to solve the problem

5=35215 5 = 35 - 2 \sdot 15 from equation 3

But, we have that

15=5035 15 = 50 - 35 from euqation 2\text{\textcircled 2}

Now we, substitute this value into the previously derived equation: 5=352155=352(5035)5=335250 \begin{aligned} 5 & = 35 - 2 \sdot 15 \\ 5 & = 35 - 2 \sdot (50 - 35) \\ 5 & = 3 \sdot 35 - 2 \sdot 50 \end{aligned}

Now, finally use the first equation to determine an expression for 3535 as a linear combination of 135135 and 5050 35=135250 from equation1 35 = 135 - 2 \sdot 50 \text{ from equation}\text{\textcircled 1}

Plug this into our last euqation:

5=3352505=3(135250)2505=3135850 \begin{aligned} 5 & = 3 \sdot 35 - 2 \sdot 50 \\ 5 & = 3 \sdot (135 - 2 \sdot 50) - 2 \sdot 50 \\ 5 & = 3 \sdot 135 - 8 \sdot 50 \end{aligned}

So, a set of solutions to the equation is x=3,y=8x=3, y = -8

This article is from 'COT3100Euclid01'

Comments