**The Euclidean Algorithm and Solving Congruences**

Prepared by:

Joseph Malkevitch

Mathematics and Computer Studies Department

York College (CUNY)

Jamaica, New York 11451

If d is the greatest common divisor of two (positive) integers a and b then d will divide the remainder r that results from dividing the smaller of a and b into the larger.

Example 1:

The greatest common divisor of 24 and 16 is 8.

24 = 1(16) + 8. Hence, the remainder when 24 is divided by 16 is 8, which is divisible by 8.

Example 2:

The greatest common divisor of 100 and 60 is 20.

100 = 1(60) + 40. The remainder 40, is divisible by 20.

The fact above constitutes the basis for what is known as the Euclidean Algorithm for finding the greatest common divisor of two numbers. The algorithm is named for the same mathematician Euclid who is famous for his work in geometry. The charm of this algorithm is that it does not find the greatest common divisor by using factorization into primes. Factorization of large numbers appears to be a very hard problem. It is not currently known what the precise computational complexity of factoring is but one of the most widely used cryptography algorithms (e.g. RSA, named for the computer scientists Ronald Rivest, Adi Shamir, and Leonard Adelman, who developed the method which was published in 1978 while they were students at MIT) depends for its security on the assumption that factoring large numbers is very difficult.

Given a and b, divide the smaller and into the larger obtaining a quotient q and a remainder r.

Assuming b is larger than a we have:

b = q(a) + r. Now the greatest common divisor of b and a is the same as the greatest common divisor of a and r.

Repeating the process for a and r (instead of a and b) we eventually come to a situation where the remainder is zero. It is a property of the division algorithm that the remainder r we get must be a smaller number than b. The greatest common divisor will be the last nonzero remainder in this process.

Example 3:

Find the greatest common divisor of 36 and 10.

36 = 3 (10) + 6

10 = 1(6) + 4

6 = 1(4) + 2

4 = 2(2) + 0

The last nonzero remainder is 2, and hence, the greatest common divisor of 10 and 36 is 2.

Theorem 1:

If d is the greatest common divisor of a and b then d can be written in the form:

d = ax + by (1)

where x and y are integers.

In particular, if two numbers are *relatively prime*, that is, their greatest common divisor is 1, then we can conclude that:

1 = ax + by

for some integers x and y.

Let us find the values x and y for the Example 3. In that example, we saw that the greatest common divisor of 36 and 10 is 2.

We saw that:

36 = 3 (10) + 6 (2)

10 = 1(6) + 4 (3)

6 = 1(4) + 2 (4)

4 = 2(2) + 0

We see from equation (4) that the the greatest common divisor of 36 and 10 is 2 and that:

2 = (1)(6) -1(4) (5)

But we know from equation (3) that 4 = 1(10) - 1(6), so we can replace the value 4 in equation (5) by 1 (10) - 1(6) to obtain:

2 = (1)(6) - 1 (1(10) - 1(6)) = (1)(6) -1(10) + 1(6) = 2(6) - 1 (10). (6)

But we know from equation (2) that 6 = 1(36) - 3(10), so we can replace the value 6 in equation (6) by 1(36) - 3(10) to obtain:

2 = 2(1(36) - 3(10)) -1(10) = 2(36) - 6(10) - 1(10) = 2(36) - 7(10).

Hence, the values of x we are looking for is 2, and the value of y we are looking for is -7, and sure enough, with these values we have 2 = 36x + 10y.

This method of backtracking through the steps used to find the gcd of two integers can always be used to find the values of the integers x and y in Theorem 1.

Now suppose we are given a congruence:

ax ≡b modulo p (*)

assuming that a and p are relatively prime we can find two integers s and t such that as + pt =1. If we take this congruence modulo p we have that:

as ≡ 1 modulo p

which implies that

bas ≡ b modulo p

Hence, if we set x to the value of bs, we have found a value for x which solves the congruence (*) above.

What remains to do is to have an algorithm for solving congruences where the modulus is a prime, is to be able to find the values for x and y in equation (1) above. To do this we make use of the Euclidean algorithm. Rather than describe what happens in general, I will illustrate with some typical examples.

Suppose one wishes to solve the congruence:

11x ≡ 1 mod 23

As a first step find the greatest common divisor of 23 and 11 using the Euclidean Algorithm:

23 = 2(11) +1

11 = 1(11) + 0, so the gcd is 1.

From the equation above we see that 1 = (1)(23) + (-2)11. Modulo 23 this equations gives that (-2)(11) ≡ 1 mod 23. Thus, x = -2 solves the congruence. Since we want to be able to write the answer as a value from 0 to 22 we see that -2 ≡ 21 mod 23, so x= 21 is the solution.

Now consider the congruence:

16x ≡ 5 modulo 29

As a first step we will solve the congruence: 16x ≡ 1 mod 29

We have 29 = 1(16) +13 (2)

16 = 1(13) + 3 (3)

13 = 4(3) + 1. (4)

So we can write that 1 = 1(13) + (-4)(3).

Substituting in this equation using the fact that from (3), 3 = 16 -1(13) we get:

1 = 1(13) + (-4)(16-1(13)) = 5(13) + (-4)(16) (5)

Now, using (2) (from which we know that 13 = 29 -1(16)) we can substitute for 13 in (5):

1 = 5(29-1(16)) + (-4)(16) = (-9)(16) +(5)(29).

This last equation modulo 29 gives:

(-9)(16) ≡ 1 modulo 29

So the value of x we seek is -9 which is congruent modulo 29 to 20.

Since we now know that 16(20) is congruent to 1 modulo 29 we can multiply both sides of this congruence by 5 to obtain that 100(16) is congruent to 5 modulo 29. Since 100 is congruent to 13 modulo 29 we conclude that 13 is the solution to the congruence we wished to solve:

16x ≡ 5 modulo 29

You should verify that 16(13) - 5 does indeed leave a zero remainder when divided by 29.

The methods above can be extended to solve congruences where p is not a prime as well. The advantage of working with a prime modulus is that as long as a in (*) is not a multiple of p we can always find a unique solution to the congruence with a value of x having a value between 0 and (p-1).

Problems:

1. Use the Euclidean algorithm to find the gcd of 27 and 42.

2. Use the Euclidean algorithm to find the gcd of 146 and 224.

3. Solve the congruence 21x ≡ 5 modulo 29.

4. Solve the congruence 31x ≡ 5 modulo 23

5. Solve the congruence 16x ≡ 5 modulo 23