Skip to main content

Modular arithmetic and Euclid

The following statement is important for modular arithmetic:

Theorem 13 (Division theorem)

(a)(b1)(!q,r:0rb1)(a=bq+r)(\forall a)(\forall b \geq 1)(\exists! q, r: 0 \leq r \leq b-1)(a = b\cdot q+ r).1

The number qq is called integer quotient and rr the remainder. This just says something very intuitive: Dividing aa by bb gives us the quotient qq and remainder rr.

We say that ff is a common divisor of aa and bb if faf \mid a2 and fbf \mid b. Integer dd is a greatest common divisor of aa and bb if dd is a common divisor of aa and bb, and every common divisor of aa and bb divides dd. We remark that the existence of a greatest common divisor is not obvious (everyone has learned them since grade school and took it for granted that they exist). But really, how can we know that there is one common divisor where everyone else divides it?

Notice that dd is a greatest common divisor of aa and bb iff d-d is also one (and these are the only two!). Denote by gcd(a,b){\sf gcd}(a,b) the unique non-negative greatest common divisor of aa and bb.

Theorem 14

For all a,ba,b, there exists a greatest common divisor of aa and bb.

We will again show two proofs. The first proof is purely mathematical and does not directly imply an algorithm that computes a greatest common divisor. The second proof is algorithmic and the existence of a greatest common divisor in fact follows from the fact that the algorithm will be proved to return it.

First proof via algebra

For aZa \in {\mathbb Z} denote by aZa {\mathbb Z} the set of all multiples of aa, e.g., 3Z={0,±3,±6,}3 {\mathbb Z} = \{0, \pm 3, \pm 6, \ldots\}. Fix integers a,ba, b. We will explicitly define a greatest common divisor of aa and bb. First, we claim that every set that is closed under subtraction (i.e. a subtraction of two elements in the set also belongs to the set) must be of a certain form.

Lemma 8

If AZA \subseteq {\mathbb Z} is nonempty and closed under subtraction, then there exists dd such that A=dZA = d {\mathbb Z}.

Proof:

If AA is closed under subtraction, notice that

  • 0A0 \in A: Since aaAa - a \in A (here we use that AA is nonempty, do you see why?)
  • (aA)aA(\forall a \in A) -a \in A (do you see why?)
  • (a,bA)a+bA(\forall a,b \in A) a+b \in A (why?)
  • aA    aZAa \in A \implies a {\mathbb Z} \subseteq A (this can be proven by induction!)

Now, we define dd to be the smallest positive integer in AA. From the last property above, we have that dZAd {\mathbb Z} \subseteq A. We claim that in fact A=dZA = d {\mathbb Z}. Consider any number aAa \in A. Apply the division theorem (to divide aa by dd) to write a=qd+ra = q \cdot d + r where 0rd10\leq r \leq d-1. Notice that, since a,qdAa, q \cdot d \in A, we know that rAr \in A. If r>0r >0, this would be impossible since rr is positive and smaller than dd, so we must have r=0r = 0, which means that dad \mid a, and thus adZa \in d {\mathbb Z}.

For subsets A,BZA, B \subseteq {\mathbb Z}, we define A+B={a+b:aA,bB}A + B = \{a+b: a \in A, b \in B\}. For instance aZ+bZa {\mathbb Z} + b {\mathbb Z} would be exactly the set that contains all numbers of the form {ax+by:x,yZ}\{a x + b y: x,y \in {\mathbb Z}\}. The following property is simple to verify.

Exercise 30

The set aZ+bZa {\mathbb Z} + b{\mathbb Z} is closed under subtraction.

The lemma implies that dZ=aZ+bZd {\mathbb Z} = a {\mathbb Z} + b {\mathbb Z} for some dd. We claim that dd is a greatest common divisor of aa and bb. First, notice that dad \mid a because aaZ+bZ=dZa \in a {\mathbb Z} + b {\mathbb Z} = d {\mathbb Z}; by the same reasoning dbd \mid b. So, dd is a common divisor. Now consider any common divisor ff of aa and bb. Since daZ+bZd \in a{\mathbb Z} + b {\mathbb Z}, we know that d=ax+byd = ax + by for some integers x,yx, y. Since faf \mid a and fbf \mid b, we have that fdf \mid d as well. This concludes the proof.

Notice that this proof implies the following fundamental result in elementary number theory.

Theorem 15 (Bézout's lemma)

For all a,ba,b there exist u,vu,v such that gcd(a,b)=au+bv{\sf gcd}(a,b)= a u + bv.

Exercise 31

Let a1,,anZa_1, \ldots, a_n \in {\mathbb Z}. Prove that a1Z+a2Z++anZ=kZa_1 {\mathbb Z} + a_2 {\mathbb Z} + \ldots + a_n {\mathbb Z} = k {\mathbb Z} for some kZk \in {\mathbb Z}. Determine the value of kk (in terms of other aia_i's).

Second Proof via Euclid's algorithm

Interestingly, the second proof is much older than the first one. The following efficient algorithm is due to Euclid (from around 350 B.C.)

Euclidean(a,b)\mathrm{Euclidean}(a,b)

  1. AaA\leftarrow a and BbB \leftarrow b
  2. while B1B\geq 1 do
    1. RA(modB)R \leftarrow A \pmod{B}
    2. AB,BRA \leftarrow B, B \leftarrow R
  3. return AA
Exercise 32

Given a,ba,b, the algorithm always terminates in finitely many steps.

Theorem 16

The above algorithm correctly returns gcd(a,b){\sf gcd}(a,b).

We would need to develop some preliminary notation and facts to facilitate the analysis of this algorithm. For integer aZa \in {\mathbb Z}, denote by div(a){\sf div}(a) the set of all integers that divide aa, i.e., div(6)={±1,±2,±3,±6}{\sf div}(6) = \{\pm 1, \pm 2, \pm 3, \pm 6\}.

Lemma 9

dd is a greatest common divisor of aa and bb if and only if div(d)=div(a)div(b){\sf div}(d) = {\sf div}(a) \cap {\sf div}(b).

Proof:

It is straightforward to show that div(f)div(a)div(b){\sf div}(f) \subseteq {\sf div}(a) \cap {\sf div}(b) for every common divisor ff of aa and bb. The implication     \implies follows from this. To see the converse, suppose div(d)=div(a)div(b){\sf div}(d) = {\sf div}(a) \cap {\sf div}(b). Consider any common divisor ff of aa and bb, so we must have that fdiv(a)div(b)f \in {\sf div}(a) \cap {\sf div}(b). Therefore, fdiv(d)f \in {\sf div}(d), which means that every common divisor divides dd. Moreover, since ddiv(d)=div(a)div(b)d \in {\sf div}(d) = {\sf div}(a) \cap {\sf div}(b), dd is a common divisor of aa and bb, so dd is a greatest common divisor.

For simplicity of notation, denote by div(a,b)=div(a)div(b){\sf div}(a,b) = {\sf div}(a) \cap {\sf div}(b).

Exercise 33

Let a,bZa, b \in {\mathbb Z}. For all integer qq, div(a,b)=div(aqb,b){\sf div}(a,b) = {\sf div}(a-q \cdot b, b).

Proof (of Theorem 16:

We argue that the following is loop invariant: div(a,b)=div(A,B){\sf div}(a,b) = {\sf div}(A,B) Notice that the algorithm updates AA to BB and BB to RR in the while-loop, so it suffices to argue that div(A,B)=div(B,R){\sf div}(A,B)= {\sf div}(B,R). But since R=A(modB)R = A \pmod{B}, we have that R=AqBR = A - q \cdot B for some integer qq. From Exercise 33, we have that div(A,B)=div(AqB,B){\sf div}(A,B) = {\sf div}(A- q B, B), so the statement is indeed a loop invariant. Using the loop invariant, when the algorithm leaves the while loop, we have that B=0B =0 (recall the reminder is always nonnegative). Therefore, div(a,b)=div(A,B)=div(A,0)=div(A){\sf div}(a,b) = {\sf div}(A,B) = {\sf div}(A, 0) = {\sf div}(A). Lemma 9 implies that AA is a greatest common divisor of aa and bb.

Exercise 34

Let BiB_i be the value of BB after the ii-th iteration of the while loop in Euclid's algorithm, starting with B0=bB_0= b. Prove that Bi+2Bi/2B_{i+2} \leq B_i/2.

Exercise 35

Use the previous exercise, deduce that the Euclid's algorithm terminates in at most 2m2m iterations where mm is the number of binary digits of bb.

Exercise 36

Given integers A,BA,B, the operation A(modB)A \pmod{B} can be computed in time O(n2)O(n^2) where nn is the number of digits of AA and BB.

These exercises imply that Euclid's algorithm is very efficient, implementable to run in time O(n3)O(n^3) where nn is the number of digits of the input. The Euclidean algorithm is a great showcase of an intimate connection between algorithms and mathematics from more than 2000 years ago (Euclid seemed to think like a computer scientist when coming up with the gcd algorithm ☺️).

Exercise 37 (Euclid's gcd lemma)

Prove that gcd(a,b)=gcd(ab,b){\sf gcd}(a,b) = {\sf gcd}(a-b,b).

Exercise 38

The Fibonacci number is given by Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2} where F0=0F_0= 0 and F1=1F_1 =1. Prove that gcd(Fn,Fn1)=1{\sf gcd}(F_n, F_{n-1}) = 1 for all n1n \geq 1.

Exercise 39

We say that a sequence a1,a2,a_1,a_2, \ldots is eventually periodic with period kk if there exists n0n_0 such that for all in0i\ge n_0, we have ai=ai+ka_i = a_{i+k}. In other words, the sequence can be seen as infinite copies of a finite sequence. Prove: For every mm, the Fibonacci sequence modulo mm is eventually periodic. The period is at most m21m^2-1.

Exercise 40

Define the sequence gn=3gn1+2gn2+gn3g_n = 3g_{n-1} + 2g_{n-2}+ g_{n-3} and g0=0,g1=g2=1g_0 = 0, g_1 = g_2= 1. Prove that, for every integer mm, the sequence gng_n modulo mm is eventually periodic.

Exercise 41

Let d(n)d(n) be the number of positive divisors of nn, e.g., d(1)=1,d(2)=d(3)=d(5)=2,d(4)=3d(1)=1, d(2) = d(3)=d(5) = 2, d(4)=3. Prove your answers on the following questions:

  • For what values of nn do we have d(n)=2d(n) =2?
  • For what values of nn do we have d(n)=3d(n) =3?

Footnotes

  1. All these numbers are integers, and in this chapter numbers are integers unless stated otherwise.

  2. Recall that this denotes "ff divides aa".