Skip to main content
Logo image

Applied Discrete Structures

Section 11.4 Greatest Common Divisors and the Integers Modulo n

In this section introduce the greatest common divisor operation, and introduce an important family of concrete groups, the integers modulo n.

Subsection 11.4.1 Greatest Common Divisors

We start with a theorem about integer division that is intuitively clear. We leave the proof as an exercise.

Note 11.4.2.

The division property says that if m is divided by n, you will obtain a quotient and a remainder, where the remainder is less than n. This is a fact that most elementary school students learn when they are introduced to long division. In doing the division problem 1986÷97, you obtain a quotient of 20 and a remainder of 46. This result could either be written 198697=20+4697 or 1986=9720+46. The latter form is how the division property is normally expressed in higher mathematics.
List 11.4.3.
We now remind the reader of some interchangeable terminology that is used when r=0, i. e., a=bq. All of the following say the same thing, just from slightly different points of view.
divides
b divides a
multiple
a is a multiple of b
factor
b is a factor of a
divisor
b is a divisor of a
We use the notation ba if b divides a.
For example 218 and 918 , but 418.
Caution: Don’t confuse the “divides” symbol with the “divided by” symbol. The former is vertical while the latter is slanted. Notice however that the statement 218 is related to the fact that 18/2 is a whole number.

Definition 11.4.4. Greatest Common Divisor.

Given two integers, a and b, not both zero, the greatest common divisor of a and b is the positive integer g=gcd(a,b) such that ga, gb, and
ca and cbcg
A little simpler way to think of gcd(a,b) is as the largest positive integer that is a divisor of both a and b. However, our definition is easier to apply in proving properties of greatest common divisors.
For small numbers, a simple way to determine the greatest common divisor is to use factorization. For example if we want the greatest common divisor of 660 and 350, you can factor the two integers: 660=223511 and 350=2527. Single factors of 2 and 5 are the only ones that appear in both factorizations, so the greatest common divisor is 25=10.
Some pairs of integers have no common divisors other than 1. Such pairs are called relatively prime pairs.

Definition 11.4.5. Relatively Prime.

A pair of integers, a and b, are relatively prime if gcd(a,b)=1
For example, 128=27 and 135=335 are relatively prime. Notice that neither 128 nor 135 are primes. In general, a and b need not be prime in order to be relatively prime. However, if you start with a prime, like 23, for example, it will be relatively prime to everything but its multiples. This theorem, which we prove later, generalizes this observation.

Subsection 11.4.2 The Euclidean Algorithm

As early as Euclid’s time it was known that factorization wasn’t the best way to compute greatest common divisors.
The Euclidean Algorithm is based on the following properties of the greatest common divisor.
(11.4.1)gcd(a,0)=a for a0(11.4.2)gcd(a,b)=gcd(b,r) if b0 and a=bq+r
To compute gcd(a,b), we divide b into a and get a remainder r such that 0r<|b|. By the property above, gcd(a,b)=gcd(b,r). We repeat the process until we get zero for a remainder. The last nonzero number that is the second entry in our pairs is the greatest common divisor. This is inevitable because the second number in each pair is smaller than the previous one. Table 11.4.7 shows an example of how this calculation can be systematically performed.
Table 11.4.7. A Table to Compute gcd(99,53)
q a b
- 99 53
1 53 46
1 46 7
6 7 4
1 4 3
1 3 1
3 1 0
Here is a Sage computation to verify that gcd(99,53)=1. At each line, the value of a is divided by the value of b. The quotient is placed on the next line along with the new value of a, which is the previous b; and the remainder, which is the new value of b. Recall that in Sage, a%b is the remainder when dividing b into a.

Investigation 11.4.1.

If you were allowed to pick two integers less than 100, which would you pick in order to force Euclid to work hardest? Here’s a hint: The size of the quotient at each step determines how quickly the numbers decrease.
Solution.
If quotient in division is 1, then we get the slowest possible completion. If a=b+r, then working backwards, each remainder would be the sum of the two previous remainders. This described a sequence like the Fibonacci sequence and indeed, the greatest common divisor of two consecutive Fibonacci numbers will take the most steps to reach a final value of 1.
For fixed values of a and b, consider integers of the form ax+by where x and y can be any two integers. For example if a = 36 and b = 27, some of these results are tabulated below with x values along the left column and the y values on top.
Linear combinations of 36 and 27
Figure 11.4.8. Linear combinations of 36 and 27
Do you notice any patterns? What is the smallest positive value that you see in this table? How is it connected to 36 and 27?

Proof.

If g=gcd(a,b), since ga and gb, we know that g(ax+by) for any integers x and y, so ax+by can’t be less than g. To show that g is exactly the least positive value, we show that g can be attained by extending the Euclidean Algorithm. Performing the extended algorithm involves building a table of numbers. The way in which it is built maintains an invariant, and by The Invariant Relation Theorem, we can be sure that the desired values of x and y are produced.
To illustrate the algorithm, Table 11.4.10 displays how to compute gcd(152,53). In the r column, you will find 152 and 53, and then the successive remainders from division. So each number in r after the first two is the remainder after dividing the number immediately above it into the next number up. To the left of each remainder is the quotient from the division. In this case the third row of the table tells us that 152=532+46. The last nonzero value in r is the greatest common divisor.
Table 11.4.10. The extended Euclidean algorithm to compute gcd(152,53)
q r s t
152 1 0
53 0 1
2 46 1 2
1 7 1 3
6 4 7 20
1 3 8 23
1 1 15 43
3 0 53 152
The “s” and “t” columns are new. The values of s and t in each row are maintained so that 152s+53t is equal to the number in the r column. Notice that
Table 11.4.11. Invariant in computing gcd(152,53)
152=1521+530
53=1520+531
46=1521+53(2)
1=15215+53(43)
0=152(53)+53152
The next-to-last equation is what we’re looking for in the end! The main problem is to identify how to determine these values after the first two rows. The first two rows in these columns will always be the same. Let’s look at the general case of computing gcd(a,b). If the s and t values in rows i1 and i2 are correct, we have
(A) {asi2+bti2=ri2asi1+bti1=ri1
In addition, we know that
ri2=ri1qi+ri  ri=ri2ri1qi
If you substitute the expressions for ri1 and ri2 from (A) into this last equation and then collect the a and b terms separately you get
ri=a(si2qisi1)+b(ti2qiti1)
si=si2qisi1 and ti=ti2qiti1
Look closely at the equations for ri,si, and ti. Their forms are all the same. With a little bit of practice you should be able to compute s and t values quickly.

Subsection 11.4.3 Modular Arithmetic

We remind you of the relation on the integers that we call Congruence Modulo n. If two integers, a and b, differ by a multiple of n, we say that they are congruent modulo n, denoted ab(modn). For example, 1338(mod5) because 1338=25, which is a multiple of 5.

Definition 11.4.12. The Integers Modulo n.

If n is a positive integer greater than one, we define the integers modulo n to be the set{0,1,2,...,n1}.

Definition 11.4.13. Modular Addition.

If n is a positive integer, we define addition modulo n (+n) as follows. If a,bZn,
a+nb= the remainder after a+b is divided by n

Definition 11.4.14. Modular Multiplication.

If n is a positive integer, we define multiplication modulo n (×n) as follows. If a,bZn,
a×nb= the remainder after ab is divided by n

Note 11.4.15.

  1. The result of doing arithmetic modulo n is always an integer between 0 and n1, by the Division Property. This observation implies that {0,1,,n1} is closed under modulo n arithmetic.
  2. It is always true that a+nb(a+b)(modn) and a×nb(ab)(modn). For example, 4+75=29(mod7) and 4×75=620(mod7).
  3. One interpretation of Zn is that each element is a representative of its equivalence class with respect to congruence modulo n. For example, if n=7, the number 1 in Z7 really represents all numbers in [1]=1+7k:kZ. In doing modular arithmetic, we can temporarily replace elements of Zn with other elements in their equivalence class modulo n.

Example 11.4.16. Some Examples.

  1. We are all somewhat familiar with Z12 since the hours of the day are counted using this group, except for the fact that 12 is used in place of 0. Military time uses the mod 24 system and does begin at 0. If someone started a four-hour trip at hour 21, the time at which she would arrive is 21+244=1. If a satellite orbits the earth every four hours and starts its first orbit at hour 5, it would end its first orbit at time 5+244=9. Its tenth orbit would end at 5+2410×244=21 hours on the clock
  2. Virtually all computers represent unsigned integers in binary form with a fixed number of digits. A very small computer might reserve seven bits to store the value of an integer. There are only 27 different values that can be stored in seven bits. Since the smallest value is 0, represented as 0000000, the maximum value will be 271=127, represented as 1111111. When a command is given to add two integer values, and the two values have a sum of 128 or more, overflow occurs. For example, if we try to add 56 and 95, the sum is an eight-digit binary integer 10010111. One common procedure is to retain the seven lowest-ordered digits. The result of adding 56 and 95 would be 0010111 two=2356+95(mod128). Integer arithmetic with this computer would actually be modulo 128 arithmetic.

Subsection 11.4.4 Properties of Modular Arithmetic

Proof.

a+(na)=n0( mod n), since n=n1+0. Therefore, a+n(na)=0.
Addition modulo n is always commutative and associative; 0 is the identity for +n and every element of Zn has an additive inverse. These properties can be summarized by noting that for each n1, [Zn;+n] is a group.

Definition 11.4.18. The Additive Group of Integers Modulo n.

The Additive Group of Integers Modulo n is the group with domain {0,1,2,,n1} and with the operation of mod n addition. It is denoted as Zn.
Multiplication modulo n is always commutative and associative, and 1 is the identity for ×n.
Notice that the algebraic properties of +n and ×n on Zn are identical to the properties of addition and multiplication on Z.
Notice that a group cannot be formed from the whole set {0,1,2,,n1} with mod n multiplication since zero never has a multiplicative inverse. Depending on the value of n there may be other numbers that will be excluded.

Definition 11.4.19. The Multiplicative Group of Integers Modulo n.

The Multiplicative Group of Integers Modulo n is the group with domain {kZ|1kn1 and gcd(n,k)=1} and with the operation of mod n multiplication. It is denoted as Un.

Example 11.4.20. Some operation tables.

Here are examples of operation tables for modular groups. Notice that although 8 is greater than 5, the two groups U5 and U8 both have order 4. In the case of U5, since 5 is prime all of the nonzero elements of Z5 are included. Since 8 isn’t prime we don’t include integers that share a common factor with 8, the even integers in this case.
+5 0 1 2 3 4
0 0 1 2 3 4
1 1 2 3 4 0
2 2 3 4 0 1
3 3 4 0 1 2
4 4 0 1 2 3
Table 11.4.21. Operation Table for the group Z5
×5 1 2 3 4
1 1 2 3 4
2 2 4 1 3
3 3 1 4 2
4 4 3 2 1
Table 11.4.22. Operation table for the group U5
×8 1 3 5 7
1 1 3 5 7
3 3 1 7 5
5 5 7 1 3
7 7 5 3 1
Table 11.4.23. Operation table for the group U8
Computing Modular Multiplicative Inverses. Unlike the nice neat formula for additive inverses mod n, multiplicative inverses can most easily computed by applying Bézout’s lemma. If a is an element of the group Un, then by definition gcd(n,a)=1, and so there exist integers s and t such that 1=ns+at. They can be computed with the Extended Euclidean Algorithm.
1=ns+atat=1+(s)na×nt=1
Since t might not be in Un you might need take the remainder after dividing it by n. Normally, that involves simply adding n to t.
For example, in U2048, if we want the muliplicative inverse of 1001, we run the Extended Euclidean Algorithm and find that
gcd(2048,1001)=1=4572048+(935)1001
Thus, the multiplicative inverse of 1001 is 2048935=1113. See the SageMath Note below to see how to run the Extended Euclidean Algorithm.

Subsection 11.4.5 SageMath Note - Modular Arithmetic

Sage inherits the basic integer division functions from Python that compute a quotient and remainder in integer division. For example, here is how to divide 561 into 2017 and get the quotient and remainder.
In Sage, gcd is the greatest common divisor function. It can be used in two ways. For the gcd of 2343 and 4319 we can evaluate the expression gcd(2343,4319). If we are working with a fixed modulus m that has a value established in your Sage session, the expression m.gcd(k) to compute the greatest common divisor of m and any integer value k. The extended Euclidean algorithm can also be called upon with xgcd:
Sage has some extremely powerful tool for working with groups. The integers modulo n are represented by the expression Integers(n) and the addition and multiplications tables can be generated as follows.
Once we have assigned R a value of Integers(6), we can do calculations by wrapping R() around the integers 0 through 5. Here is a list containing the mod 6 sum and product, respectively, of 5 and 4:
Generating the multiplication table for the family of groups Un takes a bit more code. Here we restrict the allowed inputs to be integers from 2 to 64.

Exercises 11.4.6 Exercises

1.

Determine the greatest common divisors of the following pairs of integers without using any computational assistance.
  1. 23325 and 223527
  2. 7! and 35791113
  3. 194 and 195
  4. 12112 and 0
Answer.
  1. 2235
  2. 3257
  3. 194
  4. 12112

2.

Find all possible values of the following, assuming that m is a positive integer.
  1. gcd(m+1,m)
  2. gcd(m+2,m)
  3. gcd(m+4,m)

3.

Calculate:
  1. 7+83
  2. 7×83
  3. 4×84
  4. 10+122
  5. 6×82+86×85
  6. 6×8(2+85)
  7. 3×53×53×5334( mod5)
  8. 2×117
  9. 2×147
Answer.
  1. 2
  2. 5
  3. 0
  4. 0
  5. 2
  6. 2
  7. 1
  8. 3
  9. 0

4.

List the additive inverses of the following elements:
  1. 4, 6, 9 in Z10
  2. 16, 25, 40 in Z50

5.

In the group Z11 , what are:
  1. 3(4)?
  2. 36(4)?
  3. How could you efficiently compute m(4), mZ?
Answer.
  1. 1
  2. 1
  3. m(4)=r(4), where m=11q+r, 0r<11

6.

When defining notice that we could define this operation on all integers, but we restricted the set to Zn to satisfy the properties of a group. Can you explain what goes wrong if we define +n on all of the integers?

7.

A student is asked to solve the following equations under the requirement that all arithmetic should be done in Z2. List all solutions.
  1. x2+1=0.
  2. x2+x+1=0.
Answer.
Since the solutions, if they exist, must come from Z2, substitution is the easiest approach.
  1. 1 is the only solution, since 12+21=0 and 02+21=1
  2. No solutions, since 02+20+21=1, and 12+21+21=1

8.

Determine the solutions of the same equations as in Exercise 5 in Z5.

9.

  1. Write out the operation table for ×14 on {1,3,5,9,11,13}, and convince your self that this is a group.
  2. Let Un be the elements of Zn that have inverses with respect to ×n. Convince yourself that Un is a group under ×n.
  3. Prove that the elements of Un are those elements aZn such that gcd(n,a)=1. You may use Theorem 11.4.9 in this proof.

10.

Prove the division property, Theorem 11.4.1.
Hint.
Prove by induction on m that you can divide any positive integer into m. That is, let p(m) be “For all n greater than zero, there exist unique integers q and r such that .” In the induction step, divide n into mn.

11.

Suppose f:Z17Z17 such f(i)=a×17i+17b where a and b are integer constants. Furthermore, assume that f(1)=11 and f(2)=4. Find a formula for f(i) and also find a formula for the inverse of f.
Solution.
The given conditions can be converted to a system of linear equations:
f(1)=11a+17b=11f(2)=42×17a+17b=4
If we subtract the first equation from the second, we get a=4+17(11)=4+176=10. This implies that b=1, and f(i)=10×+17i+1. To get a formula for the inverse of f we solve f(j)=i for j, using the fact that the multiplicative inverse of 10 (mod 17) is 12.
f(j)=i10×+17j+1=i10×+17j=i+1716j=12×17(i+1716)
Therefore f1(i)=12×17(i+1716)=12×17i+175.

12.

Write out the operation table for mod 10 multiplication on T={0,2,4,6,8}. Is [T;×10] a monoid? Is it a group?
Solution.
This system is a monoid with identity 6 (surprise!). However it is not a group since 0 has no inverse.

13.

Given that 1=2021(169)+450759, explain why 450 is an element of the group U2021 and determine its inverse in that group.
Solution.
By Bézout’s lemma, 450 is an element of U2021. It’s inverse in the group is 759 because
450759=2021169+1450×2021759=1.

14.

Let n=2021. Solve 450×nx=321 for x in the group Un

15.

Let p be an odd prime. Find all solutions to the equation x2=x×nx=1 in the group Up.

16.

It was observed above that in doing modular arithmetic, one can replace an element of Zn with any other element of its equivalence class modulo n. For example, if one is computing 452×4617, the alternative to multiplying 452 time 7 and then dividing by 461 to get the remainder in Z461, we can replace 452 with 9 and get a product of 63 which is conguent of 398. Use this “trick” to compute the following without the use of a calculator.
  1. 898×1001998
  2. 7710mod81
  3. The solution to 196×197x=120

17.

We associate the set Zn={0,1,2,...,n1} with the addition modulo n, +n, because the pair [Zn;+n] form a group. Why must we use a matching modulus? Explain why by considering the following two examples.
  1. [Z3,+2]
  2. [Z3,+4]
You have attempted of activities on this page.