logo

Euclidean Algorithm Proof 📂Number Theory

Euclidean Algorithm Proof

Algorithm

For two integers aba \ge b, gcd(a,b)\text{gcd}(a,b) can be calculated as follows.

Pseudocode

Algorithm: Euclidean method
InGiven integers a,ba, b.
1.r0ar_{0} \gets a
2.r1br_{1} \gets b
3.while ri+10r_{i+1} \ne 0
4.  Continue to find qiq_i and a new ri+1r_{i+1} such that the following is satisfied.ri1=riqi+ri+1,(ri>ri+1)r_{i-1} = r_i \cdot q_i + r_{i+1} \qquad , (r_i>r_{i+1})
5.  ii+1i \gets i+1
6.end while
OutObtain the greatest common divisor gcd(a,b)\gcd (a,b) of aa and bb.

Theorem

Let’s define a:=r1a: = r_{-1} and b:=r0b:=r_{0} that satisfy the recurrence ri1=qi+1ri+ri+1r_{i-1} = q_{i+1} \cdot r_{i} + r_{i+1} for ri<ri+1 r_i<r_{i+1}. The least common multiple of nn that satisfies rn+1=0r_{n+1} = 0 is as follows. rn=gcd(a,b) r_{n} = \gcd (a,b) This algorithm can find the answer within at most 2log2(b)+1 2 \log _2 (b) + 1 times.

Explanation

The algorithm known as the Euclidean Algorithm makes it very efficient to calculate the greatest common divisor. It shines more as the numbers get bigger since it provides the answer without having to factorize the numbers one by one.

The algorithm summarized above is purely a translation of Euclid’s original method, but the code can be written more easily.

Proof 1

a=r0=bq1+r1b=r1=r2q2+r3ri=ri+1qi+1+ri+2rt1=rtqt \begin{align*} a = r_{0} =& b \cdot q_1 + r_1 \\ b = r_{1} =& r_2 \cdot q_2 + r_3 \\ &\vdots \\ r_i =& r_{i+1} \cdot q_{i+1} + r_{i+2} \\ &\vdots \\ r_{t-1} =& r_t \cdot q_t \end{align*} Let’s assume. Given the condition ri1=riqi+ri+1,(ri>ri+1) r_{i-1} = r_i \cdot q_i + r_{i+1} \qquad , (r_i>r_{i+1}) we can see that the divisors of ri1r_{i-1} and ri r_i are also the divisors of ri+1r_{i+1}. Therefore, gcd(ri1,ri)=gcd(ri,ri+1) \text{gcd}( r_{i-1} , r_i )=\text{gcd}( r_i , r_{i+1} ) it is. On the other hand, thinking about rt1=rtqt,(rt+1=0)r_{t-1} = r_t \cdot q_t \qquad , (r_{t+1}=0), gcd(rt1,rt)=gcd(rtqt,rt)=rt \text{gcd}( r_{t-1} , r_t )=\text{gcd}( r_t \cdot q_t , r_t ) = r_t Now, let’s calculate the required time, namely the number of iterations needed. Before that, we must show that ri+2<12ri\displaystyle r_{i+2} < {1 \over 2} r_i.

  • If ri+112rir_{i+1} \le {1 \over 2} r_i,
    ri+2<ri+112rir_{i+2} < r_{i+1} \le {1 \over 2} r_i
  • If ri+1>12rir_{i+1} > {1 \over 2} r_i ,
    ri=ri+11+ri+2r_i = r_{i+1} \cdot 1 + r_{i+2} thus, ri+2=riri+1<ri12ri12ri r_{i+2} = r_i - r_{i+1} < r_i - {1 \over 2} r_i \le {1 \over 2} r_i it is. To summarize, ri+2<12ri r_{i+2} < {1 \over 2} r_i and by the above inequality, b=r1>2r3>22r5>>2kr2k+1 \begin{align*} b &= r_1 \\ &> 2 \cdot r_3 \\ &> 2^2 \cdot r_5 \\ &> \cdots \\ &> 2^k \cdot r_{2k+1} \end{align*} it is. To summarize, b>2kr2k+1 b > 2^k \cdot r_{2k+1} Meanwhile, because 2kb2^k \ge b, r2k+1<1r_{2k+1} < 1 must be true, which means r2k+1=0 r_{2k+1} = 0 it is. Also, since t+12k+1t+1 \le 2k +1, t2k t \le 2k In other words, the total number of iterations to end the algorithm is less or equal to tt, t2k=2(k1)+2<2log2(b)+2 t \le 2k = 2(k-1) + 2 < 2 \log _2 (b) + 2 thus, the number of iterations required is at most as follows. 2log2(b)+1 2 \log _2 (b) + 1

Implementation

Below is the implementation of the Euclidean method in R code. The first is a version that directly translates the algorithm, and the bottom is a version that cuts out unnecessary parts and tidies up.

gcd<-function(a,b)
{    
    if (b>a) {i=b; b=a; a=i;}
    i=2
     r=numeric(0)
    r[1]=b
    r[2]=a%%b
    while(r[i]!=0)
    {
        r[i+1]=r[i-1]%%r[i]
        i=i+1
    }
    return(r[i-1])
}
 
gcd <- function(a, b) {
  if (b>a) {i=b; b=a; a=i;}
  while(b) {
    temp = b
    b = a %% b
    a = temp
  }
  return(a)
}

  1. Silverman. (2012). A Friendly Introduction to Number Theory (4th Edition): p34. ↩︎