In this chapter, we will look into one of the most exciting part of mathematics, and it is also a crucial cornerstone for computer science. The inception of number theory can be traced back to ancient civilizations, but it began to emerge as a distinct mathematical discipline with the work of the Greek mathematician Euclid. His monumental work, “Elements,” laid the groundwork for the study of prime numbers and proved fundamental theorems, such as the infinitude of primes, which are still central to number theory today. The nature of whole numbers, especially the intriguing properties of prime numbers---the building blocks of all natural numbers---has fascinated mathematicians for centuries. Over time, the field has expanded to include a rich array of topics such as the distribution of primes, the solutions of Diophantine equations, modular arithmetic, and the exploration of number-theoretic functions like the Riemann zeta function. Number theory’s initial focus on prime numbers and divisibility has blossomed into a diverse and vibrant branch of mathematics, with applications ranging from cryptography to the theory of chaos.

Divisibility and Modular Arithmetic

We begin with divisibility because it supports the Fundamental Theorem of Arithmetic, greatest common divisors, least common multiples, modular arithmetic, and many elementary cryptographic constructions. The same ideas also appear when studying integer solutions of equations and the structure of rings and fields.

Division and Divisibility

To figure out problems related to Divisibility, we must figure out the definition and properties of Division. We were taught in the primary school that a division is an operation that divides a number to a certain part. For instance, 6÷2=36\div2=3, and 6÷4=1.56\div4=1.5. Sometimes, the division produce an integer as result, while sometimes not. We define division as follows:

Definition

The division of a number aa by a non-zero number bb is denoted by a÷ba \div b or ab\frac{a}{b}, and it gives the quotient qq and possibly a remainder rr. The operation can be written as: a=b×q+ra = b \times q + r where 0r<b0 \leq r < b and qZq \in \mathbb{Z}.

In 6÷2=36\div2=3, clearly we have 6=2×3+06=2\times3+0, with r=0r=0 and q=3q=3. For 6÷46\div4, integer division gives 6=4×1+26=4\times1+2, so q=1q=1 and r=2r=2, while ordinary real division gives 6/4=1.56/4=1.5. We will distinguish these two operations below.

Definition

True division is concerned with the quotient including the remainder as a fractional part. In true division, when aa is divided by bb, the quotient qq is a real number that can be represented as: q=abq = \frac{a}{b}

You may find that true division doesn’t have a remainder. This is just because the purpose of these two operations are different, as true division only focus on the scale of each part of aa, but division involves the divisibility and remainder, which will be discussed further in this chapter.

Now that we have defined division and true division, you may understand the following definition of divisibility easier.

Definition

If aa and bb are integers with a0a \neq 0, we say that aa divides bb if there is an integer cc such that b=a×cb = a\times c. Equivalently, b/ab/a is an integer. When aa divides bb, we say that aa is a factor or divisor of bb, and that bb is a multiple of aa. The notation aba\mid b denotes that aa divides bb. We write aba\nmid b when aa does not divide bb.

For example, we have 4204\mid 20, because 20÷420\div4 gives an integer, however, 4214\nmid 21, as the answer cannot be represented as integer. Besides, we have: ab    c(ac=b).a\mid b \iff \exists c(ac=b).

Problem

Let nn and dd be positive integers. How many positive integers not exceeding nn are divisible by dd?

Solution: The positive integers divisible by dd are all the integers of the form dkdk, where kk is a positive integer. Hence, the number of positive integers divisible by dd that do not exceed nn equals the number of integers kk with 0<dkn0 < dk \leq n, or with 0<knd0 < k \leq \frac{n}{d}. Therefore, there are nd\left\lfloor \frac{n}{d} \right\rfloor positive integers not exceeding nn that are divisible by dd.

Divisibility holds the following properties, which could be proven directly.

Theorem

Let aa, bb, and cc be integers, where a0a\neq 0, then:

  1. if aba \mid b and aca \mid c, then a(b+c)a \mid (b+c)

  2. if aba \mid b then abca \mid bc for all integers cc

  3. if aba \mid b and bcb\mid c, then aca \mid c

Proof

Since aba \mid b and aca \mid c, there exist integers mm and nn such that b=amb = am and c=anc = an. Therefore, b+c=am+an=a(m+n)b+c = am + an = a(m+n), and since m+nm+n is an integer, it follows that a(b+c)a \mid (b+c).

Given aba \mid b, there exists an integer kk such that b=akb = ak. For any integer cc, bc=a(kc)bc = a(kc). Since kckc is an integer (because the product of two integers is an integer), abca \mid bc.

If aba \mid b and bcb \mid c, then there exist integers pp and qq such that b=apb = ap and c=bqc = bq. Substituting the expression for bb into the equation for cc gives c=(ap)q=a(pq)c = (ap)q = a(pq). Since pqpq is an integer, aca \mid c.

With this we have the following conclusion.

Corollary

if aa, bb, and cc are integers, where a0a\neq 0, such that aba\mid b and aca\mid c, then amb+nca\mid mb + nc whenever mm and nn are integers.

This could be proven in direct proof, which you will finish in the exercise.

Modular Arithmetic

In this section, we will focus on the remainder of division, as well as modular arithmetic. In the definition of division, we involved qq and rr, which denotes the quotient and the remainder of the operation. We use the following notations to denote each of both.

Notation

For aa, bRb\in \mathbb{R}. a=b×q+ra = b\times q + r q=a div bq = a\ \text{\textbf{div}}\ b r=a mod br = a\ \text{\textbf{mod}}\ b

Besides, when aa is an integer and bb is a positive integer, we have a div b=a/ba\ \text{\textbf{div}}\ b = \left \lfloor a/b \right \rfloor.

Example

What are the quotient and remainder when 93 is divided by 9?

Solution: 93=9×10+393 = 9\times 10 + 3. q=10q = 10, r=3r = 3.

The quotient is 93 div 9=10=93/9=10.3333=1093\ \textbf{div}\ 9 = 10 = \lfloor93/9\rfloor = \lfloor10.3333\dots \rfloor = 10

The remainder is 93 mod 9=3=939093\ \textbf{mod}\ 9 = 3 = 93 - 90

Example

What are the quotient and remainder when -93 is divided by 9?

Solution: 93=9×(11)+6-93 = 9\times (-11) + 6. q=11q = -11, r=6r = 6.

Remember that we must make sure r0r\geq0 as we defined earlier, even though 93=9×(10)3-93 = 9\times(-10) - 3. But remainder could be positive in other division algorithm, which we will discuss in the exercise.

We have already introduced the notation a mod ma\ \textbf{mod}\ m to represent the remainder when an integer aa is divided by the positive integer mm. We now introduce a different, but related, notation that indicates that two integers have the same remainder when they are divided by the positive integer mm. But why we need to find and study the numbers with the same remainder by dividing the same positive integer? Studying numbers that yield the same remainder when divided by a given positive integer is a fundamental part of number theory; later you will understand why say so.

Definition

If aa and bb are integers and mm is a positive integer, then aa is congruent to bb modulo mm if mm divides aba - b. We use the notation ab(modm)a \equiv b (\bmod m) to indicate that aa is congruent to bb modulo mm. We say that ab(modm)a \equiv b (\bmod m) is a congruence and that mm is its modulus (plural moduli). If aa and bb are not congruent modulo mm, we write a≢b(modm)a \not\equiv b (\bmod m).

Do note that mod and mod are different notations. The first represents a relation on the set of integers, whereas the second represents a function. However, they are still related.

Theorem

Let aa and bb be integers, and let mm be a positive integer. ab(mod m)a \equiv b (\bmod \ m) if and only if a mod ma\ \textbf{mod}\ m = b mod mb\ \textbf{mod}\ m.

Proof

First, suppose ab(modm)a \equiv b \pmod{m}. By definition of congruence modulo mm, mm divides aba - b, which means there exists some integer kk such that ab=kma - b = km.

Dividing aa and bb by mm, they both leave the same remainder rr, since a=q1m+ra = q_1m + r and b=q2m+rb = q_2m + r for some integers q1q_1 and q2q_2. The remainder rr in both cases is the same because the difference aba - b is a multiple of mm, which does not affect the remainder.

Conversely, if amodm=bmodma \mod m = b \mod m, then both aa and bb leave the same remainder when divided by mm. Denote this common remainder as rr.

We can write a=q1m+ra = q_1m + r and b=q2m+rb = q_2m + r for some integers q1q_1 and q2q_2. Subtracting these two equations, we get ab=(q1q2)ma - b = (q_1 - q_2)m, which shows that aba - b is a multiple of mm.

Therefore, mm divides aba - b, and by definition of congruence modulo mm, we have ab(modm)a \equiv b \pmod{m}.

This completes the proof.

Remark

Remember, when we say if and only if, we need proof from each of the both statements to the other.

Theorem

Let mm be a positive integer. The integers aa and bb are congruent modulo mm if and only if there is an integer kk such that a=b+kma = b + km.

The proof is similar and not complex, try to prove it in the exercise.

Theorem

Let mm be a positive integer. If ab(modm)a \equiv b \pmod{m} and cd(modm)c \equiv d \pmod{m}, then a+cb+d(modm)a + c \equiv b + d \pmod{m} and acbd(modm).ac \equiv bd \pmod{m}.

Proof

We use a direct proof. Because ab(modm)a \equiv b \pmod{m} and cd(modm)c \equiv d \pmod{m}, by Theorem 4 there are integers ss and tt with b=a+smb = a + sm and d=c+tmd = c + tm. Hence, b+d=(a+sm)+(c+tm)=(a+c)+m(s+t)\begin{aligned} b + d &= (a + sm) + (c + tm) = (a + c) + m(s + t) \end{aligned} and bd=(a+sm)(c+tm)=ac+m(at+cs+stm).\begin{aligned} bd &= (a + sm)(c + tm) = ac + m(at + cs + stm). \end{aligned} Hence, a+cb+d(modm)a + c \equiv b + d \pmod{m} and acbd(modm).ac \equiv bd \pmod{m}.

Example

Because 72(mod5)7 \equiv 2 \pmod{5} and 111(mod5)11 \equiv 1 \pmod{5}, it follows that 18=7+112+1=3(mod5)18 = 7 + 11 \equiv 2 + 1 = 3 \pmod{5} and that 77=71121=2(mod5).77 = 7 \cdot 11 \equiv 2 \cdot 1 = 2 \pmod{5}.

Corollary

Let mm be a positive integer and let aa and bb be integers. Then (a+b)modm=((amodm)+(bmodm))modm(a + b) \bmod m = ((a \bmod m) + (b \bmod m)) \bmod m and abmodm=((amodm)(bmodm))modm.ab \bmod m = ((a \bmod m)(b \bmod m)) \bmod m.

Proof

By the definitions of mod\bmod and of congruence modulo mm, we know that a(amodm)(modm)a \equiv (a \bmod m) \pmod{m} and b(bmodm)(modm)b \equiv (b \bmod m) \pmod{m}. Hence, a+b(amodm)+(bmodm)(modm)a + b \equiv (a \bmod m) + (b \bmod m) \pmod{m} and ab(amodm)(bmodm)(modm).ab \equiv (a \bmod m)(b \bmod m) \pmod{m}. The equalities in this corollary follow from these last two congruence.

We can define arithmetic operations on Zm\mathbb{Z}_m, the set of nonnegative integers less than mm, that is, the set {0,1,,m1}\{0, 1, \ldots, m - 1\}. In particular, we define addition of these integers, denoted by m\oplus_m, by amb=(a+b)modm,a \oplus_m b = (a + b) \bmod m, where the addition on the right-hand side of this equation is the ordinary addition of integers, and we define multiplication of these integers, denoted by m\odot_m, by amb=(ab)modm,a \odot_m b = (a \cdot b) \bmod m, where the multiplication on the right-hand side of this equation is the ordinary multiplication of integers. The operations m\oplus_m and m\odot_m are called addition and multiplication modulo mm and when we use these operations, we are said to be doing arithmetic modulo mm.

Example

Use the definition of addition and multiplication in Zm\mathbb{Z}_m to find 71197 \oplus_{11} 9 and 71197 \odot_{11} 9.

Solution: Using the definition of addition modulo 11, we find that 7119=(7+9)mod11=16mod11=5,7 \oplus_{11} 9 = (7 + 9) \bmod 11 = 16 \bmod 11 = 5, and 7119=(79)mod11=63mod11=8.7 \odot_{11} 9 = (7 \cdot 9) \bmod 11 = 63 \bmod 11 = 8. Hence, 7119=57 \oplus_{11} 9 = 5 and 7119=87 \odot_{11} 9 = 8.

Remark

sometimes, normal notations are also used to express this calculation with subscript.

Exercises

Exercise

Prove corollary div1 that if aa, bb, and cc are integers, where a0a\neq 0, such that aba\mid b and aca\mid c, then amb+nca\mid mb + nc whenever mm and nn are integers.

Proof

By theorem Properties of Divisibility, aba\mid b and aca\mid c gives us amba\mid mb and anca\mid nc (property 2). Hence, a(mb+nc)a\mid (mb+nc) (property 1). This completes the proof.

Exercise

Prove theorem mod2, you may use other theorems or corollary in this chapter.

Proof

If ab(mod m)a \equiv b (\bmod \ m), that means we have m(ab)m\mid(a-b). There must be an integer kk, such that a=b+kma = b + km (theorem mod2). Conversely, if we have an integer kk, such that a=b+kma = b+km, we have km=abkm = a-b. Hence, m(ab)m\mid(a-b), ab(mod m)a \equiv b (\bmod \ m).

Exercise

show that for integer a, b, c Z+\displaystyle a,\ b,\ c\ \in \mathbb{Z}^{+}, a,c0\displaystyle a,c\neq 0 and acbc,\displaystyle ac\mid bc,then ab\displaystyle a\mid b.

Proof

acbc\displaystyle ac\mid bc means that bc=k(ac)\displaystyle bc=k( ac) for some integer k\displaystyle k, so we have b=ka\displaystyle b=ka, this means ab\displaystyle a\mid b.

Exercise

Prove that if a\displaystyle a and b\displaystyle b are integers and a\displaystyle a divides b\displaystyle b, then a\displaystyle a is odd or b\displaystyle b is even.

Proof

We could try proof by contrapositive. Suppose for a\displaystyle a is even and b\displaystyle b is odd, a,bZ, ab\displaystyle a,b\in \mathbb{Z} ,\ a\mid b. Then b=ka\displaystyle b=ka for some integer k\displaystyle k. Make a=2n, b=2n+1, nZ\displaystyle a=2n,\ b=2n+1,\ n\in \mathbb{Z}, then there should be 2n+1=2kn\displaystyle 2n+1=2kn. However, in this case we have k=2n+12n\displaystyle k=\frac{2n+1}{2n}, meaning that k\displaystyle k is not an integer, which contradict the assumption. This completes the proof.

Exercise

Prove that if aa is a positive integer, then 4(a2+2)4\nmid (a^2+2).

Proof

Consider any positive integer aa. We know that aa can be expressed in one of the following forms where nn is an integer:

  • a=4na = 4n

  • a=4n+1a = 4n + 1

  • a=4n+2a = 4n + 2

  • a=4n+3a = 4n + 3

Now, we examine a2a^2 modulo 4 for each case: (4n)2=16n2=44n20(mod4),(4n+1)2=16n2+8n+1=4(4n2+2n)+11(mod4),(4n+2)2=16n2+16n+4=4(4n2+4n+1)0(mod4),(4n+3)2=16n2+24n+9=4(4n2+6n+2)+11(mod4).\begin{aligned} (4n)^2 &= 16n^2 = 4 \cdot 4n^2 \equiv 0 \pmod{4}, \\ (4n + 1)^2 &= 16n^2 + 8n + 1 = 4(4n^2 + 2n) + 1 \equiv 1 \pmod{4}, \\ (4n + 2)^2 &= 16n^2 + 16n + 4 = 4(4n^2 + 4n + 1) \equiv 0 \pmod{4}, \\ (4n + 3)^2 &= 16n^2 + 24n + 9 = 4(4n^2 + 6n + 2) + 1 \equiv 1 \pmod{4}. \end{aligned} Adding 2 to a2a^2 in each case yields: a2+22(mod4)if a20(mod4),a2+23(mod4)if a21(mod4).\begin{aligned} a^2 + 2 &\equiv 2 \pmod{4} \quad \text{if } a^2 \equiv 0 \pmod{4}, \\ a^2 + 2 &\equiv 3 \pmod{4} \quad \text{if } a^2 \equiv 1 \pmod{4}. \end{aligned} In both cases, a2+2a^2 + 2 does not yield a remainder of 00 modulo 44. Thus, it cannot be divisible by 44.

Therefore, we have proven that for any positive integer aa, the expression a2+2a^2 + 2 is not divisible by 44.

Exercise

Suppose that aa and bb are integers, a11mod19a \equiv 11 \mod{19}, and b3mod19b \equiv 3 \mod{19}. Find the integer cc with 0c180 \leq c \leq 18 such that

  1. c13amod19c \equiv 13a \mod{19}.

  2. c8bmod19c \equiv 8b \mod{19}.

  3. cabmod19c \equiv a - b \mod{19}.

  4. c7a+3bmod19c \equiv 7a + 3b \mod{19}.

  5. c2a2+3b2mod19c \equiv 2a^2 + 3b^2 \mod{19}.

  6. ca3+4b3mod19c \equiv a^3 + 4b^3 \mod{19}.

Solution: This problem is equivalent to asking for the right-hand side mod 19. So we just do the arithmetic and compute the remainder upon division by 19.

  1. 1311=14310mod1913 \cdot 11 = 143 \equiv 10 \mod{19}

  2. 83=245mod198 \cdot 3 = 24 \equiv 5 \mod{19}

  3. 113=8mod1911 - 3 = 8 \mod{19}

  4. 711+33=8610mod197 \cdot 11 + 3 \cdot 3 = 86 \equiv 10 \mod{19}

  5. 2112+332=2693mod192 \cdot 11^2 + 3 \cdot 3^2 = 269 \equiv 3 \mod{19}

  6. 113+433=143914mod1911^3 + 4 \cdot 3^3 = 1439 \equiv 14 \mod{19}

Exercise

Let mm be a positive integer. Show that amodm=bmodma \mod m = b \mod m if ab(modm)a \equiv b \pmod{m}.

Proof

Assume that ab(modm)a \equiv b \pmod{m}. This means that mabm \mid a - b, say ab=mca - b = mc, so that a=b+mca = b + mc. Now let us compute amodma \mod m. We know that b=qm+rb = qm + r for some nonnegative rr less than mm (namely, r=bmodmr = b \mod m). Therefore, we can write a=qm+r+mc=(q+c)m+ra = qm + r + mc = (q + c)m + r. By definition, this means that rr must also equal amodma \mod m. That is what we wanted to prove.

Exercise

Show that if ab(modn)a \equiv b (\bmod n) and dad\mid a,dbd\mid b, dnd\mid n, then adbd(modnd)\frac{a}{d}\equiv \frac{b}{d} (\bmod \frac{n}{d})

Proof

Since abmodna \equiv b \mod n, by definition there exists an integer kk such that a=b+kna = b + kn. Now, since dad \mid a and dbd \mid b, we have a=dm1a = dm_1 and b=dm2b = dm_2 for some integers m1m_1 and m2m_2. Also, dnd \mid n implies n=dln = dl for some integer ll.

Substituting these into our first equation we get dm1=dm2+k(dl)dm_1 = dm_2 + k(dl)

Dividing through by dd we obtain m1=m2+klm_1 = m_2 + kl

This can be rewritten as ad=bd+k(nd)\frac{a}{d} = \frac{b}{d} + k \left(\frac{n}{d}\right)

Since k(nd)k \left(\frac{n}{d}\right) is an integer, this shows that ad\frac{a}{d} is congruent to bd\frac{b}{d} modulo nd\frac{n}{d}, which is precisely adbdmodnd\frac{a}{d} \equiv \frac{b}{d} \mod \frac{n}{d}

Hence, the statement is proven.

Exercise

Prove that if xx and yy are integers such that x2(mod8)x \equiv 2 \pmod{8} and y7(mod8)y \equiv 7 \pmod{8}, then 8 divides 2(x+y)13+y12(x + y)^{13} + y - 1.

Proof

Given x2(mod8)x \equiv 2 \pmod{8} and y7(mod8)y \equiv 7 \pmod{8}, we need to prove that 8 divides 2(x+y)13+y12(x + y)^{13} + y - 1.

First, we find x+ymod8x + y \mod 8:

x+y2+791(mod8)x + y \equiv 2 + 7 \equiv 9 \equiv 1 \pmod{8}

Next, we calculate (x+y)13mod8(x + y)^{13} \mod 8:

(x+y)131131(mod8)(x + y)^{13} \equiv 1^{13} \equiv 1 \pmod{8}

Then, we calculate 2(x+y)13mod82(x + y)^{13} \mod 8:

2(x+y)13212(mod8)2(x + y)^{13} \equiv 2 \cdot 1 \equiv 2 \pmod{8}

Finally, we calculate 2(x+y)13+y1mod82(x + y)^{13} + y - 1 \mod 8:

2(x+y)13+y12+7180(mod8)2(x + y)^{13} + y - 1 \equiv 2 + 7 - 1 \equiv 8 \equiv 0 \pmod{8}

Since 2(x+y)13+y10(mod8)2(x + y)^{13} + y - 1 \equiv 0 \pmod{8}, it follows that 8 divides 2(x+y)13+y12(x + y)^{13} + y - 1.

Number Representations and Algorithms

In everyday life, we use decimal notation to express integers. Though this is not for all cases, as we are using a 60 base system for time. However, in computer science, binary, octal, and hexadecimal systems are widely used for number representation. The reason is that binary system consist of only 0 and 1, as we have mentioned in Boolean algebra, this system is perfect for logic operations in side the computer. What’s more, 0 and 1 could be easily represented by the on and off of tiny switches in the integrated circuits. This chapter looks into the representation of number in different bases and their relationship, with a tress on binary representation that computer system uses. Meanwhile, we will go through algorithm of some operations between number, and analyze them accordingly.

Representations of Numbers and Base Conversion

In whichever base, a number could be expressed using the exponent of the base.

Theorem

Let bb be an integer greater than 1. Then if nn is a positive integer, it can be expressed uniquely in the form:\ n=akbk+ak1bk1++a1b+a0n=a_{k} b^{k}+a_{k-1} b^{k-1}+\cdots+a_{1} b+a_{0}\ where kk is a nonnegative integer, a0,a1,,aka_0, a_1,\cdots, a_k are negative integers less than b, and ak0a_k \neq 0

For example, 1024 could be interpreted as 1024=1×103+×102+2×101+4×1001024 = 1\times10^3+\times10^2+2\times10^1+4\times10^0. Besides, the base of number constraints the possible numbers to be used on the bits. For example, under decimal system, we cannot take 10 as one digit, instead it is a number with two digits 1 and 0, because for bits under decimal system, the maximum only goes to 9. Therefore, you could take it as a fact that for a system of base bb, the biggest value for one bit is b1b-1. Similarly, we see only 0 and 1 in binary, 1-7 in ocatl, 1-15 in hexadecimal expression.

To indicate the base of a number, we use the following subscript.

Notation

We use b_b to show the base of a number, where bb is the base number.

The other important fact is that numbers are just numbers, base is only the way we gauge them. However you change 12110121_{10}, to whatever base, it is still 121 in decimal, but just expressed in a different way.

Binary, Octal, and Hexadecimal Expression

In binary expansion, the base on any integer is 2, that is to say, the number in each digit is either 0 or 1. We can expand binary integer in the same ways as mentioned in last section.

Example

What is the decimal expansion of the integer that has (101011111)2(101011111)_2 as its binary expansion?

(101011111)2(101011111)_2 has nine digits, so we have:\ (101011111)2(101011111)_2 = 1×29+1×27+1×25+1×24+1×23+1×22+1×21+1×20=3511\times 2^9 + 1\times 2^7+1\times 2^5+1\times 2^4+1\times 2^3+1\times 2^2+1\times 2^1+1\times 2^0 = 351

Decimal expansion of octal expansions could be calculated in the same way as the binary expansion. However, Sixteen different digits are required for hexadecimal expansions. Usually, the hexadecimal digits used are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, where the letters A through F represent the digits corresponding to the numbers 10 through 15 (in decimal notation).

Example

What is the decimal expansion of the number with hexadecimal expansion (2AE0B)16(2AE0B)_{16}?

In hexadecimal cases, the only difference is that capital latters are used to represent two-digit number in one digit. We still have:\ (2AE0B)16(2AE0B)_{16} = 2×164+10×163+14×162+0×161+11×160=1756272\times 16^4 + 10\times 16^3 + 14\times 16^2 + 0 \times 16^1 + 11 \times 16^0=175627

NOTICE: Each hexadecimal digit can be represented using four bits. For instance, we see that (11100101)2(1110 0101)_2 = (E5)16(E5)_{16} because (1110)2(1110)_2 = (E)16(E)_{16} and (0101)2=(5)16(0101)_2 = (5)_{16}. Bytes, which are bit strings of length eight, can be represented by two hexadecimal digits.

Base Conversion

Base conversion of Integer

Now we have learned how to express numbers in different base systems, but is there any way to convert them from one base to the other? You may have realized that a general solution is to convert the number back to decimal form, and then to the target base. Yes, it works, but can we make it better? One way to this is to use the general base conversion algorithm. The most common algorithm to constructing the base bb expansion of an integer n is as follows:\ First, divide nn by bb to obtain a quotient and remainder, that is:\

n = bq0 + a0                     (0  a0 < b)\displaystyle n\ =\ bq_{0} \ +\ a_{0} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ( 0\ \leq \ a_{0} \ < \ b)

The remainder, a0a_0, is the rightmost digit in the base b expansion of nn. Next, divide q0q_0 by bb toobtain:\

q0 = bq1 + a1                     (0  a1 < b)\displaystyle q_0\ =\ bq_{1} \ +\ a_{1} \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ( 0\ \leq \ a_{1} \ < \ b)

We see that a1a_1 is the second digit from the right in the base b expansion of n. Then, we just continue this process until we obtain a quotient equal to zero. This algorithm produces the base bb digits of nn from the right to the left.

Example

Find the hexadecimal expansion of (177130)10(177130)_{10}.

177130=1611070+10177130 = 16 \cdot 11070 + 10\ 11070=16691+1411070 = 16 \cdot 691 + 14\ 691=1643+3691 = 16 \cdot 43 + 3\ 43=162+1143 = 16 \cdot 2 + 11\ 2=160+22 = 16 \cdot 0 + 2\

Therefore, (177130)10=(2B3EA)16(177130)_{10} = (2B3EA)_{16}

This method is equivalent to the following pseudocode.

$digits \gets$ empty list $remainder \gets n \bmod b$ Append $remainder$ to $digits$ $n \gets \left\lfloor n / b \right\rfloor$ $digits \gets$ reverse $digits$ **return** $digits$

Base Conversion of Float

The previous algorithm only deals with integers, so we need a different approach for decimals. The algorithm for converting a floating-point number from base 10 to base bb is given as follows:

$integerPart \gets \lfloor number \rfloor$ $fractionalPart \gets number - integerPart$ $baseInteger \gets \Call{ConvertIntegerToBase}{integerPart, targetBase}$ $baseFraction \gets ``"$ $fractionalPart \gets fractionalPart \times targetBase$ $baseFraction \gets baseFraction + \lfloor fractionalPart \rfloor$ $fractionalPart \gets fractionalPart - \lfloor fractionalPart \rfloor$ $baseInteger + ``." + baseFraction$
Example

Convert the floating-point number 12.375 from base 10 to base 2.

  • Integer part: 12 in base 10 is 1100 in base 2.

  • Fractional part: 0.375 in base 10 to base 2. 0.375×2=0.75integer part is 00.75×2=1.5integer part is 10.5×2=1integer part is 1\begin{aligned} 0.375 \times 2 &= 0.75 \rightarrow \text{integer part is 0} \\ 0.75 \times 2 &= 1.5 \rightarrow \text{integer part is 1} \\ 0.5 \times 2 &= 1 \rightarrow \text{integer part is 1} \\ \end{aligned}

  • The fractional part in base 2 is .011.

Therefore, 12.3751012.375_{10} is 1100.01121100.011_{2}.

Also, when a number consist of both integer and decimal parts, we just deal with them separately and them put them back together later. You will see these exercises in the problem set.

Base Conversion between Binary, Octal, and Hexadecimal number

The base conversion between Binary, Octal and Hexadecimal numbers are much easier.

Figure pending: Binary, Octal and Hexadecimal Representation

The conversion between binary, octal, and hexadecimal systems is straightforward because the bases of these systems (2 for binary, 8 for octal, and 16 for hexadecimal) are related by powers of 2. Specifically:

  • Octal digits correspond to three binary digits since 8=238 = 2^3. Each octal digit can be directly mapped to a unique combination of three binary bits.

  • Hexadecimal digits correspond to four binary digits since 16=2416 = 2^4. Each hexadecimal digit can be directly mapped to a unique combination of four binary bits.

This relationship allows for simple grouping of binary bits into sets of three or four to convert to octal or hexadecimal, respectively, without any complex calculation or division.

Example

Find the octal and hexadecimal expansions of (11 1110 1011 1100)2(11\ 1110\ 1011\ 1100)_2 and the binary expansions of (765)8(765)_{8} and (A8D)16(A8D)_{16}.

Solution: To convert (11 1110 1011 1100)2(11\ 1110\ 1011\ 1100)_2 into octal notation we group the binary digits into blocks of three, adding initial zeros at the start of the leftmost block if necessary. These blocks, from left to right, are 011, 111, 010, 111, and 100, corresponding to 3, 7, 2, 7, and 4 respectively. Consequently, we get (11 1110 1011 1100)2=(37274)8(11\ 1110\ 1011\ 1100)_2 = (37274)_8

We do the hexadecimal convention in the same way. These blocks, from left to right, are 0011, 1110, 1011, and 1100, corresponding to the hexadecimal digits 3, E, B, and C, respectively. Consequently, (11 1110 1011 1100)2=(3EBC)16(11\ 1110\ 1011\ 1100)_2 = (3EBC)_{16}

NOTE: The conversion could be done inversely in the same way. If the number of bit is not the multiple of 4 when you are converting between 2 and 16 base number, fill those missing digits/bits with 0.

Whether you noticed or that the base conversion, essentially, is about division and modular arithmetic that we just learned? The general base conversion algorithm could be simplified to the following algorithm, where nn denotes the number to be converted and bb is the target base.

$q \gets n$ $k \gets 0$ $a_k \gets q \mod b$ $q \gets \lfloor q / b \rfloor$ $k \gets k + 1$ **return** $(a_{k-1}, \ldots, a_1, a_0)$

Operation Algorithms of Number

This section discusses algorithm of basic operation between numbers, of base2. In section, we express the binary number in the form of a=(an1an2a1a0)2,b=(bn1bn2b1b0)2a=(a_{n-1}a_{n-2}\ldots a_1a_0)_2,b=(b_{n-1}b_{n-2}\ldots b_1b_0)_2 where aa and bb each have n bits.

Addition Algorithm

Consider the problem of adding two integers in binary notation. A procedure to perform addition can be based on the usual method for adding numbers with pencil and paper. This method proceeds by adding pairs of binary digits together with carries, when they occur, to compute the sum of two integers. This procedure will now be specified in detail.

To add aa and bb, first add their rightmost bits. This gives a0+b0=c02+s0,a_0 + b_0 = c_0 \cdot 2 + s_0, where s0s_0 is the rightmost bit in the binary expansion of a+ba + b and c0c_0 is the carry, which is either 0 or 1. Then add the next pair of bits and the carry, a1+b1+c0=c12+s1,a_1 + b_1 + c_0 = c_1 \cdot 2 + s_1, where s1s_1 is the next bit (from the right) in the binary expansion of a+ba + b, and c1c_1 is the carry. Continue this process, adding the corresponding bits in the two binary expansions and the carry, to determine the next bit from the right in the binary expansion of a+ba + b. At the last stage, add an1a_{n-1}, bn1b_{n-1}, and cn2c_{n-2} to obtain cn12+sn1c_{n-1} \cdot 2 + s_{n-1}. The leading bit of the sum is sn=cn1s_n = c_{n-1}. This procedure produces the binary expansion of the sum, namely, a+b=(snsn1s1s0)2a + b = (s_n s_{n-1} \ldots s_1 s_0)_2. Consider the binary addition of two numbers, 101121011_2 and 110121101_2.

The addition process is similar to that used in decimal addition, but it is performed in base 2. Below is the columnar addition process:


101121011_2 + 110121101_2 11000211000_2


Let’s perform the addition step by step:

  1. Start with the rightmost bits (least significant bits). Add 1+11 + 1. Since this is base 2, 1+1=1021 + 1 = 10_2. Write down the 00 and carry the 11 over to the next column.

  2. Move to the next column. Add 1+0+11 + 0 + 1 (including the carry). This equals 10210_2. Write down the 00 and carry the 11.

  3. In the next column, add 0+1+10 + 1 + 1. This equals 10210_2. Write down the 00 and carry the 11.

  4. For the leftmost bits (most significant bits), add 1+1+11 + 1 + 1. This equals 11211_2. Write down the 11 and carry the 11 to a new column to the left.

  5. Write down the carry.

The final result is 11000211000_2.

This algorithm’s pseudocode is as listed below.

$c \gets 0$ $d \gets \left\lfloor (a_j + b_j + c)/2 \right\rfloor$ $s_j \gets a_j + b_j + c - 2d$ $c \gets d$ $s_n \gets c$ **return** $(s_{n}s_{n-1}\ldots s_1s_0)_2$

Time Complexity: The time complexity of the binary addition algorithm is O(n)O(n), where nn is the number of bits in the binary representation of the inputs.

Proof

Consider the binary addition algorithm which consists of a single loop that iterates nn times, where nn is the number of bits in the binary representations of the two integers being added.

Within the loop, the algorithm performs a constant number of operations for each bit:

  • An addition of the jj-th bits of the two numbers aj+bja_j + b_j.

  • An addition of the carry from the previous step cc.

  • A division by 2 to compute the new carry dd.

  • A subtraction to determine the jj-th bit of the sum sjs_j.

  • An assignment of the new carry cdc \gets d.

Since each of these operations has a constant time complexity, and they are all executed once for each bit, the overall time complexity of the loop is linear with respect to the number of bits. Therefore, the time complexity of the entire algorithm is O(n)O(n).

Note that this analysis assumes that basic arithmetic operations (addition, division by 2, subtraction) can be performed in constant time.

Multiplication Algorithm

Using the same representation of binary number aa and bb, we can define binary multiplication as ab=a(b020+b121++bn12n1)=a(b020)+a(b121)++a(bn12n1)\begin{aligned}a b & =a\left(b_{0} 2^{0}+b_{1} 2^{1}+\cdots+b_{n-1} 2^{n-1}\right) \\& =a\left(b_{0} 2^{0}\right)+a\left(b_{1} 2^{1}\right)+\cdots+a\left(b_{n-1} 2^{n-1}\right)\end{aligned}. The binary multiplication algorithm works similarly to traditional pencil-and-paper multiplication, but with binary digits. Given two binary numbers, we multiply each bit of the second number by the first number, shifting the result left for each subsequent bit.

$c \gets 0$ $d \gets \left\lfloor (a_j + b_j + c)/2 \right\rfloor$ $s_j \gets a_j + b_j + c - 2d$ $c \gets d$ $s_n \gets c$ $(s_{n}s_{n-1}\ldots s_1s_0)_2$
Example

Find the product of a=(110)2a = (110)_2 and b=(101)2b = (101)_2. First note that ab020=(110)2120=(110)2,ab121=(110)2021=(0000)2,andab222=(110)2122=(11000)2.\begin{aligned} a \cdot b_0 \cdot 2^0 &= (110)_2 \cdot 1 \cdot 2^0 = (110)_2, \\ a \cdot b_1 \cdot 2^1 &= (110)_2 \cdot 0 \cdot 2^1 = (0000)_2, \\ \text{and} \\ a \cdot b_2 \cdot 2^2 &= (110)_2 \cdot 1 \cdot 2^2 = (11000)_2. \end{aligned} To find the product, add (110)2,(0000)2,(110)_2, (0000)_2, and (11000)2(11000)_2. Carrying out these additions (using Algorithm 2, including initial zero bits when necessary) shows that ab=(11110)2ab = (11110)_2.

Time Complexity: The time complexity of binary multiplication is O(n2)O(n^2), where nn is the number of bits in the binary numbers. This is because each bit of one number is multiplied by each bit of the other number, resulting in nn multiplications for each of the nn bits.

Algorithm for Div and Mod

The following algorithm is used to find the quotient and remainder of a÷ba \div b. This is a more general algorithm as it could handle the cases where a is negative

$q \gets 0$ $r \gets |a|$ $r \gets r - d$ $q \gets q + 1$ $r \gets d - r$ $q \gets -(q + 1)$ $(q, r)$

Modular Exponentiation Algorithm

An important application of modular arithmetic is finding bnmodmb^n \bmod m efficiently, which is crucial for cryptography. bnb^n could be a huge number that takes a lot of time to calculate, so we need this algorithm to speed up this process. to implement this algorithm, we need to use the binary expansion of the number n=(ak1a1a0)2n=(a_{k-1}\ldots a_1a_0)_2. Since bn=bak12k1++a12+a0=bak12k1ba12ba0.b^n=b^{a_{k-1}\cdot2^{k-1}+\cdots+a_1\cdot2+a_0}=b^{a_{k-1}\cdot2^{k-1}}\cdots b^{a_1\cdot2}\cdot b^{a_0}.

This shows that to compute bnb^n, we need only compute the values of b,b2,(b2)2=b4,(b4)2=b,b^2,(b^2)^2=b^4,(b^4)^2= b8,...,b2kb^8,...,b^{2^k}. Once we have these values, we multiply the terms b2jb^{2j} in this list, where aj=1a_j=1 .(For efficiency and to reduce space requirements, after multiplying by each term, we reduce the result modulo m.)m.)

Example

Compute 494^9\ Solution: To compute 494^9 we first note that 9=(1001)29 = (1001)_2, so that 49=4832414^9 = 4^8 3^2 4^1. By successively squaring, we find that 42=164^2 = 16, 44=162=2564^4 = 16^2 = 256, and 48=(256)2=655364^8 = (256)^2 = 65536. Consequently, 49=484241=65536164=4,194,3044^9 = 4^8 4^2 4^1 = 65536 \cdot 16 \cdot 4 = 4,194,304.

Now we have already finished the part of algorithm to find bnb^n. With this, we can define the algorithm to find bnmodmb^n \bmod m. The pseudocode of the algorithm is in the listing below.

$x := 1$ $power := b \mod m$ **then** $x := (x \cdot power) \mod m$ $power := (power \cdot power) \mod m$ **return** $x$ $x$ equals $b^n \mod m$
Example

Find the value of 3644mod6453^{644} \mod 645 using the Algorithm.

Solution: The algorithm starts by initializing x=1x = 1 and power=3mod645=3power = 3 \mod 645 = 3. It then calculates 32jmod6453^{2j} \mod 645 for j=1,2,,9j = 1, 2, \ldots, 9 by repeatedly squaring and reducing modulo 645. When the jjth bit of 644 (in binary, (1010000100)2(1010000100)_2) is 1, the algorithm multiplies the current xx by 32jmod6453^{2j} \mod 645 and reduces the product modulo 645. The steps are as follows:


i=0i = 0: a0=0a_0 = 0, so x=1x = 1 and power=32mod645=9power = 3^2 \mod 645 = 9. i=1i = 1: a1=0a_1 = 0, so x=1x = 1 and power=92mod645=81power = 9^2 \mod 645 = 81. i=2i = 2: a2=1a_2 = 1, so x=181mod645=81x = 1 \cdot 81 \mod 645 = 81 and power=812mod645=111power = 81^2 \mod 645 = 111. i=3i = 3: a3=0a_3 = 0, so x=81x = 81 and power=1112mod645=66power = 111^2 \mod 645 = 66. i=4i = 4: a4=0a_4 = 0, so x=81x = 81 and power=662mod645=486power = 66^2 \mod 645 = 486. i=5i = 5: a5=0a_5 = 0, so x=81x = 81 and power=4862mod645=126power = 486^2 \mod 645 = 126. i=6i = 6: a6=0a_6 = 0, so x=81x = 81 and power=1262mod645=396power = 126^2 \mod 645 = 396. i=7i = 7: a7=1a_7 = 1, so x=(81396)mod645=471x = (81 \cdot 396) \mod 645 = 471 and power=3962mod645=81power = 396^2 \mod 645 = 81. i=8i = 8: a8=0a_8 = 0, so x=471x = 471 and power=812mod645=111power = 81^2 \mod 645 = 111. i=9i = 9: a9=1a_9 = 1, so x=(471111)mod645=36x = (471 \cdot 111) \mod 645 = 36.


Thus, the value of 3644mod6453^{644} \mod 645 is 36.

Time Complexity of Algorithm 5

The time complexity of Algorithm modulare is determined by the number of iterations in the for loop, which is equal to the number of bits in the binary representation of the exponent nn. In each iteration, the algorithm performs a constant number of modular multiplications and squarings. Therefore, the time complexity of Algorithm modulare is O(logn)O(\log n), where nn is the exponent. This is a significant improvement over the naive method of modular exponentiation, which has a time complexity of O(n)O(n). The fast modular exponentiation algorithm is particularly useful in cryptographic applications, such as the RSA algorithm, where the exponents are typically large numbers.

To prove that the time complexity of Algorithm modulare is O(logn)O(\log n), we can analyze the number of operations performed by the algorithm in relation to the size of the input exponent nn.

Proof

Let nn be the exponent in the modular exponentiation problem, and let kk be the number of bits in the binary representation of nn. We can express nn as:

n=i=0k1ai2i, where ai{0,1}n = \sum_{i=0}^{k-1} a_i \cdot 2^i, \text{ where } a_i \in \{0, 1\}

The algorithm iterates through the bits of nn from right to left (from the least significant bit to the most significant bit). In each iteration, the algorithm performs the following operations:

  1. If ai=1a_i = 1, it performs a modular multiplication to update the value of xx.

  2. It performs a modular squaring to update the value of power\textit{power}.

The modular multiplication and squaring operations can be performed in O(1)O(1) time using a constant number of arithmetic operations modulo mm.

Since the algorithm iterates through all kk bits of nn, the total number of iterations is kk. Therefore, the time complexity of the algorithm is proportional to kk, which is the number of bits in the binary representation of nn.

We know that the number of bits in the binary representation of nn is log2n+1\lfloor \log_2 n \rfloor + 1. Thus, k=O(logn)k = O(\log n).

Consequently, the time complexity of Algorithm modulare is O(logn)O(\log n).

This logarithmic time complexity makes Algorithm modulare (fast modular exponentiation) much more efficient than the naive method of modular exponentiation, which has a linear time complexity of O(n)O(n).

Exercises

Primes and Greatest Common Divisors

In previous chapter, we learned the properties of division and divisibility, as well as modular arithmetic. These are the most essential parts of the whole number theory. In this section , we will discuss primes and its property. Prime is not something unfamiliar to us, since some may even have learned that from the kindergarten or primary school. We will look into the Algorithms that could help is find primes and delve into the algebraic properties of prime later. All these are foundations of cryptography.

To learn prime, of course we need to recap on its definition.

Definition

An integer pp greater than 1 is called prime if the only positive factors of pp are 1 and pp. A positive integer that is greater than 1 and is not prime is called composite.

Note that, by this definition, 1 is not a prime, as it is only 1 as the only factor.

The reason why prime numbers are so fascinating to mathematicians is that they have so many interesting and unique property, even except for what we have seen in its definition. Below is what we call the fundamental theorem of arithmetic.

Theorem

For every integer n>1n > 1, there exists a unique factorization into prime numbers, up to the order of the factors. Specifically, nn can be expressed as n=p1a1p2a2pkakn = p_1^{a_1} \cdot p_2^{a_2} \cdot \ldots \cdot p_k^{a_k} where p1<p2<<pkp_1 < p_2 < \ldots < p_k are prime numbers and a1,a2,,aka_1, a_2, \ldots, a_k are positive integers. This factorization is unique, apart from the order of the prime factors.

Proof

We prove the theorem in two parts: existence and uniqueness.

Existence: We prove by mathematical induction that every integer greater than 1 can be written as a product of primes.

Base Case: For n=2n = 2, the statement holds true since 2 is itself a prime number.

Inductive Step: Assume the statement holds for all integers greater than 1 and less than nn. Now consider the integer nn.

  • If nn is prime, then it is trivially a product of primes (itself).

  • If nn is not prime, it can be written as n=abn = a \cdot b where 1<a,b<n1 < a, b < n. By the inductive hypothesis, both aa and bb can be factored into a product of primes. Therefore, nn can also be expressed as a product of primes by combining the prime factorization of aa and bb.

This completes the proof of existence.

Uniqueness: Assume, for the sake of contradiction, that there are two distinct prime factorization of nn: n=p1a1p2a2pkak=q1b1q2b2qmbmn = p_1^{a_1} \cdot p_2^{a_2} \cdot \ldots \cdot p_k^{a_k} = q_1^{b_1} \cdot q_2^{b_2} \cdot \ldots \cdot q_m^{b_m} where pip_i and qjq_j are prime numbers, and ai,bja_i, b_j are positive integers. To proceed to the rest of the proof, we need to use Euclid’s lemma.

lemma:

Let pp be a prime number. If pp divides the product abab, where aa and bb are integers, then pp divides aa or pp divides bb.

Proof of Euclid’s lemma: Assume pp is a prime that divides abab but does not divide aa. We need to show that pp must divide bb.

Since pp does not divide aa, the greatest common divisor (gcd) of aa and pp is 1, i.e., gcd(a,p)=1\text{gcd}(a, p) = 1. According to Bezout’s identity, there exist integers xx and yy such that:

ax+py=1ax + py = 1

Multiplying both sides of the equation by bb, we get:

abx+pby=babx + pby = b

Remark: If this proof is not yet understandable, skip it and return to it after the discussion of linear combinations and this theorem.

Since pp divides abab (by assumption), pp divides abxabx. Also, pp obviously divides pbypby. Hence, pp divides the sum abx+pbyabx + pby, which means pp divides bb.

This completes the proof, showing that if pp divides abab and does not divide aa, then pp must divide bb, in accordance with Euclid’s lemma.

By Euclid’s lemma, if a prime divides the product of two numbers, it must divide at least one of those numbers. Thus, p1p_1 must divide some qjq_j on the right-hand side. Since qjq_j is prime, we conclude that p1=qjp_1 = q_j. Applying this argument symmetrically and repeatedly, we find that each set of prime factors must be identical to the other, contradicting the assumption of two distinct factorization.

Therefore, the prime factorization of any integer greater than 1 is unique, up to the order of the factors, which completes the proof of the Fundamental Theorem of Arithmetic.

Example

100 can be taken as 100=2255=2252100 = 2\cdot 2\cdot 5\cdot 5 = 2^2\cdot 5^2. Both 2 and 5 are primes.

Now that we know these interesting properties of prime and its significance, how do we find primes, not just correctly, but also efficiently? The most basic algorithm that anyone could find is that we can actually check numbers one by one. To find whether an integer nn is prime, what we need to do is to check every number from 2 to n1n-1, and use them to divide nn one by one. If anyone of them divides nn, then nn is not prime, and vice versa.

Trial Division

We actually can apply a more efficient way to determine whether an integer is prime or not. Considering the following theorem.

Theorem

If nn is a composite integer, then nn has a prime divisor less than or equal to n\sqrt{n}.

Proof

If nn is composite, by the definition of a composite integer, we know that it has a factor aa with 1<a<n1 < a < n. Hence, by the definition of a factor of a positive integer, we have n=abn = ab, where bb is a positive integer greater than 1. We will show that ana \leq \sqrt{n} or bnb \leq \sqrt{n}. If a>na > \sqrt{n} and b>nb > \sqrt{n}, then ab>nn=nab > \sqrt{n} \cdot \sqrt{n} = n, which is a contradiction. Consequently, ana \leq \sqrt{n} or bnb \leq \sqrt{n}. Because both aa and bb are divisors of nn, we see that nn has a positive divisor not exceeding n\sqrt{n}. This divisor is either prime or, by the fundamental theorem of arithmetic, has a prime divisor less than itself. In either case, nn has a prime divisor less than or equal to n\sqrt{n}.

Below is the pseudocode for this procedure.

**false** **false** **true**
Example

Show that 105\sqrt{105} is prime.

textbfSolution: Primes less than 105\sqrt{105} are 2, 3, 5, 7. 101 is not divisible by any of them, so it is not composite, and thus it is prime.

We also need an efficient algorithm to factorize a give number to primes. Here is how is works. It works as follows.

$factors \gets \text{an empty list}$ Add $p$ to $factors$ $n \gets n / p$ **break** Add $n$ to $factors$ $factors$
Example

Find the prime factorization of 8964.

Solution:

  1. 8964 is even, so start with 2: 8964÷2=44828964 \div 2 = 4482.

  2. 4482 is also even, divide by 2 again: 4482÷2=22414482 \div 2 = 2241.

  3. 2241 is not divisible by 2. The next primes to try are 3, 5, 7, 11, and so on. We find that 2241 is not divisible by any of these primes until we reach 31.

  4. 2241 divided by 31 gives us 71, which is a prime number.

Thus, the prime factorization of 8964 is 2×2×31×712 \times 2 \times 31 \times 71, or in exponent form, 22×31×712^2 \times 31 \times 71.

There are also other method to finding prime numbers or tell whether a number is prime or not. If you are willing to delve into it, you may check Sieve of Eratosthenes, Sieve of Ktkin. These are only small part of all possible method, which we will learn later, such as Fermat’s Little Theorem in solving congruence, as well as Monte Carlo method in probability.

Greatest Common Divisors and Least Common Multiples

Now we will discuss yet another primary school topic, GCD and LCM. Briefyly recap their definitions:

Definition

Let aa and bb be integers, not both zero. The largest integer dd such that dad \mid a and dbd \mid b is called the greatest common divisor of aa and bb. The greatest common divisor of aa and bb is denoted by gcd(a,b)(a, b).

Example

Find gcd(12,24)(12, 24) and gcd(17,22)(17, 22).

These are easy-to-solve problems. The largest number that divides 12 and 24 is 6, so gcd(12,24)=6(12,24)=6,while 17 and 22 do not have common divisor other than 1, so gcd(17,22)=1(17, 22)=1.

For 17 and 22, we have gcd(17,22)=1(17, 22)=1. In this case, we call that they are Relatively Prime, because their greatest common divisor is 1.

We can generalize this idea to pairwise Relatively prime.

Definition

The integers a1,a2,...,ana_1,a_2,...,a_n are pairwise relatively prime if gcd(ai,aj)=1\gcd(a_i,a_j)=1 whenever 1i<1\leq i< jn.j\leq n.

For example, 99, 1616, and 2323 are pairwise relatively prime because gcd(9,16)=gcd(9,23)=gcd(16,23)=1\gcd(9,16)=\gcd(9,23)=\gcd(16,23)=1. A sequence is pairwise relatively prime exactly when every pair of distinct terms has greatest common divisor 11.

The other way to find gcd of two number is using their prime factorization. Suppose that the prime factorizations of the positive integers aa and bb are a=p1a1p2a2pnan,b=p1b1p2b2pnbn,a=p_1^{a_1}p_2^{a_2}\cdots p_n^{a_n},b=p_1^{b_1}p_2^{b_2}\cdots p_n^{b_n},. where each exponent is a nonnegative integer, and where all primes occurring in the prime factorization of either aa or bb are included in both factorizations, with zero exponents if necessary. Then gcd(a,b)(a, b) is given by gcd(a,b)=p1min(a1,b1)p2min(a2,b2)pnmin(an,bn),\gcd(a,b)=p_1^{\min(a_1,b_1)}p_2^{\min(a_2,b_2)}\cdotp\cdotp\cdotp p_n^{\min(a_n,b_n)}, where min(x,y)\min(x,y) represents the minimum of the two numbers xx and y. To show that this formula for gcd(a,b)\gcd(a,b) is valid, we must show that the integer on the right-hand side divides both aa and bb, and that no larger integer also does. This integer does divide both α\alpha and bb, because the power of each prime in the factorization does not exceed the power of this prime in either the factorization of aa or that of bb. Further, no larger integer can divide both α\alpha and bb, because the exponents of the primes in this factorization cannot be increased, and no other primes can be included.

Example

Find gcd(100,250)\gcd(100, 250).

gcd(100,250)=2min(2,1)5min(2,3)=252=50.\gcd(100,250)=2^{\min(2,1)}5^{\min(2,3)}=2\cdot5^2=50.

The same prime-factorization method gives the least common multiple of two integers. The Fundamental Theorem of Arithmetic guarantees that every integer greater than 11 has a unique factorization into primes.

Definition

The least common multiple of the positive integers aa and bb is the smallest positive integer that is divisible by both aa and bb. The least common multiple of aa and bb is denoted by lcm(a,b)(a, b).

Suppose the prime factorizations of aa and bb are given by a=p1a1p2a2pnan,b=p1b1p2b2pnbn,a = p_1^{a_1} p_2^{a_2} \ldots p_n^{a_n}, \quad b = p_1^{b_1} p_2^{b_2} \ldots p_n^{b_n}, where aia_i and bib_i are the exponents of the prime factors of aa and bb, respectively.

The product abab can be expressed as a prime factorization: ab=p1a1+b1p2a2+b2pnan+bn.ab = p_1^{a_1+b_1} p_2^{a_2+b_2} \ldots p_n^{a_n+b_n}.

From this, we can deduce that any common multiple of aa and bb must be a product of their prime factors raised to at least the maximum exponent found in either aa or bb. Therefore, the lcm(a,b)\text{lcm}(a, b) can be expressed as lcm(a,b)=p1max(a1,b1)p2max(a2,b2)pnmax(an,bn).\text{lcm}(a, b) = p_1^{\max(a_1,b_1)} p_2^{\max(a_2,b_2)} \ldots p_n^{\max(a_n,b_n)}. This ensures that lcm(a,b)\text{lcm}(a, b) is divisible by both aa and bb, and it is the smallest such number with this property.

Example

Find lcm(120,500)(120, 500). lcm(120,500)=2max(2,3)3max(1,0)5max(1,3)=8×3×125=3000\text{lcm}(120,500)=2^{\max(2,3)}3^{\max(1,0)}5^{\max(1,3)} = 8\times 3 \times 125= 3000

Have you noticed, that abab is actually the product of lcm(a,b)\text{lcm}(a,b) and gcd(a,b)\gcd(a,b)? Becuase the order of each term in the prime factorization of abab is a sum of two number, what ever which number is the maximum of the minimum, we always have the order of an+bna_n+b_n. For 120 and 500 we have ab=120×500=60000=lcm(a,b)×gcd(a,b)=3000×20ab = 120\times 500 = 60000 = \text{lcm}(a,b)\times \gcd(a,b) = 3000\times 20

Theorem

Let aa and bb be positive integers. Then ab=gcd(a,b)ab= \gcd ( a, b) \cdotlcm(a,b).( a, b).

The Euclidean Algorithm

Greatest Common Divisors as Linear Combinations

The greatest common divisor of two integers aa and bb can be expressed in the form sa+tbsa + tb where ss and tt are integers. In other words, gcd(a,b)\gcd(a, b) can be expressed as a linear combination with integer coefficients of aa and bb.

This property of GCD is exactlly the Bézout’s Theorem, which states that:

Theorem

Let aa and bb be integers, not both zero. There exist integers xx and yy such that ax+by=gcd(a,b),ax + by = \gcd(a, b), where gcd(a,b)\gcd(a, b) denotes the greatest common divisor of aa and bb. The integers xx and yy are known as Bézout coefficients. the equationgcd(a,b)=sa+tb\gcd(a, b) = sa + tb is called Bézout’s identity.

Proof

The proof of Bézout’s identity uses the property that for nonzero integers aa and bb, dividing aa by bb leaves a remainder of r1r_1 strictly less than b|b| and gcd(a,b)=\gcd(a,b)=. gcd(r1,b)\gcd(r_1,b).Then by repeated applications of the Euclidean division algorithm, we have a=bx1+r1a=bx_1+r_1, 0<r1<b0<r_1<|b|, b=r1x2+r2b=r_1x_2+r_2, 0<r2<r10<r_2<r_1, rn1=rnxn+1+rn+1,0<rn+1<rnr_{n-1}=r_nx_{n+1}+r_{n+1},\quad0<r_{n+1}<r_n rn=rn+1xn+2r_n=r_{n+1}x_{n+2}

where the rn+1r_{n+1} is the last nonzero remainder in the division process. Now, as illustrated in the example above, we can use the second to last equation to solve for rn+1r_{n+1} as a combination of rnr_n and rn1r_{n-1}. Unfolding this, we can solve for rnr_n as a combination of rn1r_{n-1} and rn2r_{n-2} etc. until we eventually write rn+1r_{n+1} as alinear combination of aa and bb. Since rn+1r_{n+1} is the last nonzero remainder in the division process, it is the greatest common divisor of aa and bb,which proves Bézout’s identity.

You can access a more detailed proof here.

Remark

Bezout’s Theorem is closely related to Linear Diophantine Equations, which we will discuss further in solving linear congruence.

We use a example to explain further on the theorem. Recall that Euclidean algorithm is actually a recursive algorithm, to find gcd of aa and bb, it requires multiple usage of the algorithm.

Example

Use the Euclidean algorithm to find the greatest common divisor of 1022 and 400.

Solution: 1022=2×400+222400=1×222+178222=1×178+44178=4×44+244=22×2+0\begin{aligned}\\ 1022&=2\times400+222\\ 400&=1\times 222+178\\ 222&=1\times 178+44\\ 178&=4\times 44+2\\ 44&=22\times2+0 \end{aligned} In this way, we find gcd(1022,400)=2\gcd(1022,400)=2 easily. Writing the step-by-step of using Euclidean like this is what we call the Extended Euclidean Algorithm.

Notice further that as the terms in the extended algorithm could be expressed by previous lines, we can finally get a linear combination of the gcd over 1022 and 400. In this case, we have 2=23×4009×10222 = 23\times 400-9\times 1022.

Now consider an equivalent expression for the problem. Find integers aa and bb such that gcd(1022,400)=a×1022+b×400\gcd(1022, 400) = a \times 1022 + b \times 400. In this case, we do exactly the same thing, and straightforward, we have a=9a=-9 and b=23b=23.

There is still another equivalent expression to this, and it will be discussed with linear congruence in the next section.

Exercises

Solving Congruence

Earlier in this chapter, we introduced divisibility, and thus defined division and modular arithmetic, as well as modular congruence. We have also introduced algebraic properties of congruence, and surprisingly, many of which are just like “copying” from the real number , or we generally say, normal algebra system. Now think about how we learn math for real number. We learnt all the way from basic operations, and finally we just do not use the numbers anymore in the expression, but in stead, letters, and we call this algebra. Now that modular congruence shares so many basic properties with that, can we extend to more algebra on modular congruence? This section introduces solving equation, or only linear equations, of modular congruence, and it has much to do with Euclidean algorithm.

Linear Congruence

Definition

Congruence of the form as follows axb(modm)ax \equiv b (\bmod m) is called Linear Congruence, where a,b,mZ+a, b, m \in \mathbb{Z}^+, and xx is the variable to be solved. To solve this congruence, we need to find aˉ\bar{a}, which we call inverse of a modulo m if aa, such that aaˉ1(modm)a\bar{a} \equiv 1 (\bmod m).

If we are to find the xx of a given expression, what kind of solution we will get? Do we get specific solution or infinitely many solutions? The fact is that we will get infinitely many solutions, since modular congruence has periodicity.

Theorem

If aa and mm are relatively prime integers and m>1m > 1, then an inverse of aa modulo mm exists. Furthermore, this inverse is unique modulo mm. (That is, there is a unique positive integer aˉ\bar{a} that is an inverse of aa modulo mm and every other inverse of aa modulo mm is congruent to aˉ\bar{a} modulo mm.)

Proof

By Bezout’s Theorem, because gcd(a,m)=1\gcd(a, m) = 1, there are integers ss and tt such that sa+tm=1.sa + tm = 1. This implies that sa+tm1(modm).sa + tm \equiv 1 \pmod{m}. Because tm0(modm)tm \equiv 0 \pmod{m}, it follows that sa1(modm).sa \equiv 1 \pmod{m}. Consequently, ss is an inverse of aa modulo mm.

Now we try to prove the uniqueness of ss as an inverse of aa modulo mm. Suppose there are other integers than ss (xx) that satisfies xa1(modm) and xs+kmxa \equiv 1 \pmod{m} \text{ and } x \neq s + km So, by definition of modular congruence we have xamodm=1xa \bmod m = 1 and samodm=1sa \bmod m = 1, which gives xasa(modm)xa \equiv sa \pmod m So mxasam\mid xa - sa, and this means that xasa=kmxa - sa = km for some integer kk must be true. If that is true, then xsx-s is also multiple of mm, which means xs(modm)x\equiv s \pmod m. Now recall our assumption that xs+kmx\neq s + km, so x=s+kmx = s + km for some integer kk refutes our assumption. Therefore, there is a unique positive integer aˉ\bar{a} that is an inverse of a modulo mm and every other inverse of aa modulo mm is congruent to aˉ\bar{a} modulo m.

Example

Find an inverse of 101 modulo 5000.

Solution:

we present all steps used to compute an inverse of 101 modulo 5000. First, we use the Euclidean algorithm to show that gcd(101,5000)=1\gcd(101, 5000) = 1. Then we will reverse the steps to find Bézout coefficients aa and bb such that 101a+5000b=1101a + 5000b = 1. It will then follow that aa is an inverse of 101 modulo 5000. The steps used by the Euclidean algorithm to find gcd(101,5000)\gcd(101, 5000) are 5000=49101+51101=151+5051=150+150=501.\begin{aligned} 5000 &= 49 \cdot 101 + 51 \\ 101 &= 1 \cdot 51 + 50 \\ 51 &= 1 \cdot 50 + 1 \\ 50 &= 50 \cdot 1. \end{aligned} Because the last nonzero remainder is 1, we know that gcd(101,5000)=1\gcd(101, 5000) = 1. We can now find the Bezout coefficients for 101 and 5000 by working backwards through these steps, expressing the gcd of 101 and 5000 as 11 in terms of each successive pair of remainders. In each step we eliminate the remainder by expressing it as a linear combination of the divisor and the dividend. We obtain 1=51150=511(101151)=251101=2(500049101)101=2500099101.\begin{aligned} 1 &= 51 - 1 \cdot 50 \\ &= 51 - 1 \cdot (101 - 1 \cdot 51) \\ &= 2 \cdot 51 - 101 \\ &= 2 \cdot (5000 - 49 \cdot 101) - 101 \\ &= 2 \cdot 5000 - 99 \cdot 101. \end{aligned}

That 995000+2101=1-99 \cdot 5000 + 2 \cdot 101 = 1 tells us that 22 and 99-99 are Bezout coefficients of 5000 and 101, and 99-99 is an inverse of 101 modulo 5000.

Example

What are the solutions of the linear congruence 101x3(mod5000)101x \equiv 3 \pmod {5000}?

We know that -99 is a modular inverse of 101mod5000101 \bmod 5000. So we multiply the inverse on the both side, this gives 99101x993(mod5000)-99 \cdot 101x \equiv -99\cdot 3 \pmod {5000} 991011(mod5000)-99\cdot 101 \equiv 1 \pmod{5000}, so x993(mod5000)x\equiv -99 \cdot3 \pmod{5000}. From here we can see that xx is not a single number, but a set of numbers. We can havex=297,5293,4703x = -297, -5293, 4703\cdots. So the general solution xx is x=x0+kmx = x_0 + km, where kk is some integer, and x0x_0 is one of the specific solution we can find.

Great, now you can solve any solutions for any linear congruence, as long as it is solvable (has inverse). But can we generalize this problem? Because in the previous discussion of solving linear congruence and finding the inverse are all based on the condition that the two numbers are coprime, i.e., gcd(a,b)=1\gcd(a,b)=1. What will be the case when they are not coprime?

Actually, solving linear congruence is a subset of a greater concept, Diophantine Equation.

Definition

A Diophantine equation is an equation or a system of equations that allows for polynomial expressions in several variables and requires the solutions to be integers. These equations are studied within number theory and are named after Diophantus of Alexandria, a Greek mathematician. Specifically, a Diophantine equation can be interpreted by the following expression. a1x1b1+a2x2b2+......+anxnbn=ca_{1}x_{1}^{b_{1}}+a_{2}x_{2}^{b_{2}}+......+a_{n}x_{n}^{b_{n}}=c Where aa, bb, xx, cZc \in \mathbb{Z}.

You may realize at the first glance that this equation is hard to solve and has infinitely many solutions, because for the most basic system of linear equation, we can only get specific solution when the number of unknowns is equal to the number of equations in the system. But no worries, we discuss only the simplest form of this type of equation for now, which is ax+by=c.ax+by=c \text{.} Isn’t it familiar to you? This is exactly what were shown in Bezout’s Theorem (you may check theorem Bezout) This means that, as long as we are finding the relation between between a pair of number a,ba,b with their GCD, we are doing exactly the same thing as solving linear congruence, because the latter is a subset of the former, as Diophantine equation.

Now let’s work out the general solution of such equation. Consider the linear Diophantine equation

ax+by=cax + by = c

where aa, bb, and cc are given integers, and xx and yy are unknown integers that we want to solve for.

Step 1: Finding a particular solution

By the Extended Euclidean Algorithm, we can find integers x0x_0 and y0y_0 such that

ax0+by0=gcd(a,b)ax_0 + by_0 = \gcd(a, b)

Step 2: The general solution form

If cc is a multiple of gcd(a,b)\gcd(a, b), say c=kgcd(a,b)c = k \cdot \gcd(a, b), then by multiplying the equation ax0+by0=gcd(a,b)ax_0 + by_0 = \gcd(a, b) by kk, we get a particular solution to ax+by=cax + by = c:

ax0k+by0k=ckax_0k + by_0k = ck

Now, consider that for any integer tt, the following holds:

a(x0+tb/d)+b(y0ta/d)=ax0+by0=cka(x_0 + tb/d) + b(y_0 - ta/d) = ax_0 + by_0 = ck

where d=gcd(a,b)d = \gcd(a, b).

This is because the multiples of b/db/d added to xx and multiples of a/da/d subtracted from yy will cancel each other out when multiplied by aa and bb, respectively.

Step 3: General solution

Thus, the general solution to the equation ax+by=cax + by = c can be expressed as:

x=kx0+t(bd)x = kx_0 + t\left(\frac{b}{d}\right)

y=ky0t(ad)y = ky_0 - t\left(\frac{a}{d}\right)

where t,kt, k are any integer and d=gcd(a,b)d = \gcd(a,b). This represents an infinite set of solutions if aa and bb are coprime.

Corollary

Now consider the cases for solutions, when a,ba,b are coprime (gcd(a,b)=1\gcd(a,b)=1), there must be integer solutions for the equation, as shown in the proof, because 1 is a divisor of any integer. However, when cc is not a multiple of gcd(a,b)\gcd(a, b), we cannot find any integer solution. As for other general cases, i.e., gcd(a,b)1\gcd(a, b)\neq 1, but gcd(a,b)c\gcd(a,b)\mid c, there are integer solutions.

Now that we have known the idea of Diophantine Equation, we can relate the concept to linear congruence easily.

Proposition

Solving linear congruence is a subproblem of solving linear Diophantine equation. Suppose we are looking for linear combination of c=sa+tbc=sa+tb, which is equivalent to solving this Diophantine equation. This is equivalent to find axc(modb)ax\equiv c \pmod b.

Proof

axc(modb)ax\equiv c \pmod b means that axmodb=cmodbax \bmod b = c \bmod b. By division algorithm ax=q1b+rax = q_1b + r, c=q2b+rc = q_2b + r, where qq is the quotient and qZq\in \mathbb{Z}. cq2b=axq1bc-q_2b = ax-q_1b, and thus c=ax+(q2q1)bc = ax + (q_2-q_1)b, where a,(q2q1)Za, (q_2-q_1)\in \mathbb{Z}. So we have shown that solving linear congruence could be treated as solving a Diophantine Equation.

Example

Solve 400z8(mod1022)400z\equiv 8 (\bmod1022)

Solution:

We can straightaway treat it as solving the Diophantine equation 8=400x+1022y8 = 400x + 1022y We need to know their gcd, so we use extended Euclidean algorithm. 1022=2×400+222400=1×222+178222=1×178+44178=4×44+244=22×2+0\begin{aligned} 1022&=2\times400+222\\ 400&=1\times 222+178\\ 222&=1\times 178+44\\ 178&=4\times 44+2\\ 44&=22\times2+0 \end{aligned}

By Bezout theorem, we use substitution repetitively to get the linear combination of their gcd in terms of 400 and 1022, which is 2=23×4009×10222 = 23\times 400 -9\times 1022. Because 8 is multiple of 2, so we can get zz by 8=36×1022+92×4008 = -36\times 1022 + 92\times 400, which is a set of specific solution (x=92,y=36x = 92, y=-36).

With the specific solution, we can get the general solution: x=k92+t(1022gcd(400,1022))y=k(36)+t(400gcd(400,1022))\begin{aligned}\\ x&=k\cdot 92+t(\frac{1022}{\gcd(400,1022)})\\ y&=k\cdot(-36)+t(\frac{400}{\gcd(400,1022)} ) \end{aligned}

The Chinese Remainder Theorem

In the last section, we relate linear congruence to linear equation. Now we will try to relate it to system of linear equations. Recall that a system of linear equations is a collection of one or more linear equations involving the same variables.

Definition

In the context of real numbers, a system of nn linear equations in nn unknowns x1,x2,,xnx_1, x_2, \ldots, x_n can be written as: a11x1+a12x2++a1nxn=b1a21x1+a22x2++a2nxn=b2an1x1+an2x2++annxn=bn\begin{aligned} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n &= b_1 \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n &= b_2 \\ \vdots &\\ a_{n1}x_1 + a_{n2}x_2 + \cdots + a_{nn}x_n &= b_n \end{aligned} where aija_{ij} and bib_i are real numbers.

Remark

Just a kind reminder that “Linear” basically means the order of polynomial is at most 1, just in case some don’t know the definition.

Definition

Similarly, a system of linear congruences is a collection of linear congruences in the same variables over a ring of integers modulo mm. A system of nn linear congruences in nn unknowns x1,x2,,xnx_1, x_2, \ldots, x_n can be written as: a11x1+a12x2++a1nxnb1(modm1)a21x1+a22x2++a2nxnb2(modm2)an1x1+an2x2++annxnbn(modmn)\begin{aligned} a_{11}x_1 + a_{12}x_2 + \cdots + a_{1n}x_n &\equiv b_1 \pmod{m_1} \\ a_{21}x_1 + a_{22}x_2 + \cdots + a_{2n}x_n &\equiv b_2 \pmod{m_2} \\ \vdots &\\ a_{n1}x_1 + a_{n2}x_2 + \cdots + a_{nn}x_n &\equiv b_n \pmod{m_n} \end{aligned} where aija_{ij}, bib_i, and mim_i are integers. The goal in both cases is to find values for the unknowns that simultaneously satisfy all the equations or congruences in the system.

Actually, we cannot solve such complex problems, which requires further knowledge on matrix and linear algebra. However, just like what we do to Diophantine Equations, we are trying to find solution for a specific case of basic form, which, in this case, is discussed in Chinese Remainder Theorem. In the first century, the Chinese mathematician Sun-Tsu asked: There are certain things whose number is unknown. When divided by 3, the remainder is 2; when divided by 5, the remainder is 3; and when divided by 7, the remainder is 2. What will be the number of things?

This problem could be transfered in the a system of linear congruences that x2(mod3)x3(mod5)x2(mod7){\begin{aligned}x\equiv2\pmod{3}\\x\equiv3\pmod{5}\\x\equiv2\pmod{7}\end{aligned}} The algorithm to find the solution for such problem is known as Chinese Remainder Theorem.

Theorem

Let m1,m2,...,mnm_1,m_2,...,m_n be pairwise relatively prime positive integers greater than one and a1,a2,...,ana_1,a_2,...,a_n arbitrary integers. Then the system xa1(modm1)xa2(modm2)xan(modmn)\begin{aligned} x&\equiv a_1\pmod{m_1}\\ x&\equiv a_2 \pmod{m_2}\\ \vdots&\\ x&\equiv a_n \pmod{m_n} \end{aligned} has a unique solution modulo m=m1m2...mn.m=m_1m_2...m_n. (That is, there is a solution xx with 0x<m0\leq x<m, and all other solutions are congruent modulo mm to this solution.)

Proof

To establish this theorem, we need to show that a solution exists and that it is unique modulo MM. We will show that a solution exists by describing a way to construct this solution, and then we will prove that the solution is unique modulo MM.

Existence of a solution: To construct a simultaneous solution, first let Mk=Mmk=m1m2mk1mk+1mnM_k = \frac{M}{m_k} = m_1m_2 \cdots m_{k-1}m_{k+1} \cdots m_n for k=1,2,,nk = 1, 2, \ldots, n. That is, MkM_k is the product of the moduli except for mkm_k. Because mim_i and mkm_k have no common factors greater than 1 when iki \neq k, it follows that gcd(mk,Mk)=1\gcd(m_k, M_k) = 1. Consequently, by Bézout’s identity, we know that there exist integers yky_k and zkz_k such that Mkyk+mkzk=1M_ky_k + m_kz_k = 1 This implies that (refer to theorem exist_inverse) Mkyk1(modmk)M_ky_k \equiv 1 \pmod{m_k} Where yky_k is one of the unique modular inverse of MkM_k. To construct a simultaneous solution, form the sum x=a1M1y1+a2M2y2++anMnyn.x = a_1M_1y_1 + a_2M_2y_2 + \cdots + a_nM_ny_n.

We will now show that xx is a simultaneous solution. First, note that because Mj0(modmk)M_j \equiv 0 \pmod{m_k} whenever jkj \neq k, all terms except the kkth term in this sum are congruent to 0 modulo mkm_k. Because Mkyk1(modmk)M_ky_k \equiv 1 \pmod{m_k}, we see that xakMkykak(modmk)x \equiv a_kM_ky_k \equiv a_k \pmod{m_k} for k=1,2,,nk = 1, 2, \ldots, n. We have shown that xx is a simultaneous solution to the nn congruences.

Uniqueness of the solution modulo MM:

Suppose that xx and xx' are two solutions to the system of congruences. Then, for each k=1,2,,nk = 1, 2, \ldots, n, we have xak(modmk)andxak(modmk)x \equiv a_k \pmod{m_k} \quad \text{and} \quad x' \equiv a_k \pmod{m_k} This implies that xx(modmk)x \equiv x' \pmod{m_k} for all k=1,2,,nk = 1, 2, \ldots, n. Since m1,m2,,mnm_1, m_2, \ldots, m_n are pairwise coprime, by the Chinese Remainder Theorem for Ideals, we have xx(modM)x \equiv x' \pmod{M} where M=m1m2mnM = m_1m_2 \cdots m_n. This proves that the solution is unique modulo MM.

Example

Solve x2(mod3)x3(mod5)x2(mod7)\begin{aligned} x &\equiv 2 \pmod{3} \\ x &\equiv 3 \pmod{5} \\ x &\equiv 2 \pmod{7} \end{aligned} where m1=3m_1 = 3, m2=5m_2 = 5, m3=7m_3 = 7 are pairwise coprime

we can use the Chinese Remainder Theorem. The solution is: xa1M1y1+a2M2y2+a3M3y3(modm1m2m3)x \equiv a_1M_1y_1 + a_2M_2y_2 + a_3M_3y_3 \pmod{m_1m_2m_3} where Mi=m1m2m3miM_i = \frac{m_1m_2m_3}{m_i} and yiy_i satisfies Miyi1(modmi)M_iy_i \equiv 1 \pmod{m_i} for each ii. xa1M1y1+a2M2y2+a3M3y3=2352+3211+2151=23323 (mod 105).\begin{aligned} x\equiv a_{1}M_{1}y_{1}+a_{2}M_{2}y_{2}+a_{3}M_{3}y_{3}& =2\cdot35\cdot2+3\cdot21\cdot1+2\cdot15\cdot1 \\ &=233\equiv23\mathrm{~(mod~}105). \end{aligned} The solution x23323(mod105)x \equiv 233 \equiv 23 \pmod{105} is the smallest positive integer that leaves a remainder of 2 when divided by 3, a remainder of 3 when divided by 5, and a remainder of 2 when divided by 7.

This is indeed a general solution to such problem, however, you may have already realized that finding modular inverse for a given number is not that easy. And also, when the problem scale up, the complexity of computing is not ideal. Hence, we introduce back substitution as a shortcut, here is how it works.

Example

Solving x2(mod3)x3(mod5)x2(mod7)\begin{aligned} x &\equiv 2 \pmod{3} \\ x &\equiv 3 \pmod{5} \\ x &\equiv 2 \pmod{7} \end{aligned} by back substitution.

By the definition of modular congruence, x2(mod3)x\equiv 2 \pmod 3 is equivalent to x=3i+2x = 3i+2 for some integer ii. Substituting this into the second modular congruence gives us 3i+23(mod5).3i+2 \equiv 3 \pmod 5. Subtracting 2 on both side we have 3i1(mod5).3i\equiv 1 \pmod 5. Now we can find that ii is actually a modular inverse of 3 by 5, and the smallest positive integer that satisfies this is i=2i=2, so we have i2(mod5)i\equiv 2 \pmod 5. Similarly, we have i=5j+2i = 5j + 2 for some integer jj. Substituting i=5j+2i = 5j + 2 into x=3i+2x = 3i+2, we have x=15j+8x = 15j + 8. Apply this to the third expression we have 15j+82(mod7),15j + 8\equiv 2 \pmod{7}, and 15j1(mod7)15j \equiv 1 \pmod 7,because 81(mod7)8 \equiv 1\pmod 7. We find that jj is also a modular inverse of 15 by 7, and the smallest positive integer for this is 1, so j1(mod7)j \equiv 1 \pmod 7. Again, we have j=7k+1j = 7k + 1 for some integer kk, now substitute this into x=15j+8x = 15j + 8, we have x=15(7k+1)+8x = 15(7k + 1) + 8. So we have x=105k+23x = 105k + 23. We can translate this easily into the following congruence: x23(mod105).x \equiv 23 \pmod{105}. That is exactly the solution to the system of linear congruences.

Remark

In summary, back substitution works because multiplying both sides of a linear congruence by the modular multiplicative inverse of the coefficient allows us to isolate the variable and find its value modulo mm.

Fermat’s Little Theorem

This section discusses Fermat’s Little Theorem. This theorem is named after the French mathematician Pierre de Fermat, who first stated it in 1640. Fermat’s Little Theorem is a fundamental result in number theory and has numerous applications in cryptography, computer science, and other fields. It provides a way to reduce large powers modulo a prime number, which is essential for efficient computation in many algorithms.

Theorem

Let pp be a prime number and aa be any integer not divisible by pp. Then, the following congruence relation holds: ap11(modp)a^{p-1} \equiv 1 \pmod{p} In other words, if we raise aa to the power of p1p-1 and divide the result by pp, the remainder is always 11.

In previous chapter, we introduced the fast modular exponential algorithm to calculate exponentiation modulo mm. Fermat’s Little Theorem provides another pathway that sometimes could even be faster.

Proof

Let pp be a prime number and aa be an integer not divisible by pp. Consider the set of integers {1,2,,p1}\{1, 2, \ldots, p-1\} and multiply each element by aa modulo pp. This operation permutes the set, as no two elements will have the same product modulo pp (if axay(modp)ax \equiv ay \pmod{p}, then a(xy)0(modp)a(x-y) \equiv 0 \pmod{p}, which implies xy(modp)x \equiv y \pmod{p} since aa is not divisible by pp).

Therefore, the set {a1,a2,,a(p1)}\{a \cdot 1, a \cdot 2, \ldots, a \cdot (p-1)\} is a permutation of {1,2,,p1}\{1, 2, \ldots, p-1\} modulo pp. Multiplying all elements in each set, we get: i=1p1(ai)i=1p1i(modp)\prod_{i=1}^{p-1} (a \cdot i) \equiv \prod_{i=1}^{p-1} i \pmod{p} which can be rewritten as: ap1i=1p1ii=1p1i(modp)a^{p-1} \prod_{i=1}^{p-1} i \equiv \prod_{i=1}^{p-1} i \pmod{p} Canceling the common factor i=1p1i\prod_{i=1}^{p-1} i (which is not divisible by pp), we obtain: ap11(modp)a^{p-1} \equiv 1 \pmod{p} Thus, Fermat’s Little Theorem is proved.

To clarify the key points of this theorem, consider the following table. This table shows the case where p = 7 for ab1(modp)a^b\equiv 1 \pmod p. The row number represents aa, and column number represents b(=p1)b (=p-1), among the combination we see exactly the every six column gives a whole column that satisfies ab1(modp)a^b\equiv 1 \pmod p.

a\ba \backslash b 00 11 22 33 44 55 66


11 11 11 11 11 11 11 11 22 11 22 44 11 22 44 11 33 11 33 22 66 44 55 11 44 11 44 22 11 44 22 11 55 11 55 44 66 22 33 11 66 11 66 11 66 11 66 11

Example

Compute 3100(mod7)3^{100} \pmod{7} using Fermat’s Little Theorem.

Solution

First, we check that the conditions for Fermat’s Little Theorem are satisfied:

  • 77 is a prime number.

  • 33 is not divisible by 77.

By Fermat’s Little Theorem, we know that: 3711(mod7)3^{7-1} \equiv 1 \pmod{7} which can be rewritten as: 361(mod7)3^6 \equiv 1 \pmod{7}

Now, we can use this result to simplify the calculation of 3100(mod7)3^{100} \pmod{7}: 3100=(36)163411634(mod7)181(mod7)4(mod7)\begin{aligned} 3^{100} &= (3^6)^{16} \cdot 3^4 \\ &\equiv 1^{16} \cdot 3^4 \pmod{7} \\ &\equiv 1 \cdot 81 \pmod{7} \\ &\equiv 4 \pmod{7} \end{aligned}

Therefore, 31004(mod7)3^{100} \equiv 4 \pmod{7}.

In this example, we first check that the conditions for Fermat’s Little Theorem are met: 77 is a prime number, and 33 is not divisible by 77. Then, we use Fermat’s Little Theorem to establish that 361(mod7)3^6 \equiv 1 \pmod{7}.

To compute 3100(mod7)3^{100} \pmod{7}, we break down the exponent into smaller parts:

  • We write 31003^{100} as (36)1634(3^6)^{16} \cdot 3^4.

  • Since 361(mod7)3^6 \equiv 1 \pmod{7}, we can replace (36)16(3^6)^{16} with 1161^{16}, which is equal to 11.

  • We compute 34(mod7)3^4 \pmod{7}, which is 814(mod7)81 \equiv 4 \pmod{7}.

  • Finally, we multiply 11 and 44 modulo 77 to get the result: 31004(mod7)3^{100} \equiv 4 \pmod{7}.

Exercise

Use the Euclidean algorithm to find the greatest common divisor of 504 and 385. consider

  • Is it possible to find an integer yy such that 504y10mod385504y \equiv 10 \mod 385? If it is, find one. If it isn’t, explain why not.

  • Is it possible to find an integer zz such that 504z7mod385504z \equiv 7 \mod 385? If it is, find one. If it isn’t, explain why not.

Solution

By extended Euclidean Algorithm 504=1×385+119385=3×119+28119=4×28+728=4×7+0\begin{aligned} 504 & =1\times 385+119\\ 385 & =3\times 119+28\\ 119 & =4\times 28+7\\ 28 & =4\times 7+0 \end{aligned} We conclude that gcd(504,385)=7\gcd(504, 385)=7

Since solving linear congruence is equivalent to solving Linear Diophantine Equation. So we need to solve 10=504x+385yand7=504x+385y\begin{aligned} 10 &=504x+385y\\ \text{and}\\ 7 &= 504x+385y \end{aligned} The equation 504z7mod385504z \equiv 7 \mod 385 can be simplified to 119z7mod385119z \equiv 7 \mod 385 since 504119mod385504 \equiv 119 \mod 385. Because the gcd(119, 385) = 7 does divide 7, a solution exists. Dividing the equation by 7 gives 17z1mod5517z \equiv 1 \mod 55. Testing multiples of 17 modulo 55, we find that 17131mod5517 \cdot 13 \equiv 1 \mod 55, so z=13z = 13 is a solution.

For the second function, we cannot find any linear combination of integers for the expression, because 7107\nmid 10.

For the general solution, we can use the conclusion obtained earlier in this chapter that {x=k×x0+t(385gcd(504,385))y=k×y0+t(504gcd(504,385))\begin{cases} x = k \times x_0 + t (\frac{385}{\gcd(504,385)})\\ y = k \times y_0 + t (\frac{504}{\gcd(504,385)}) \end{cases} where k,tk,t are just some integers, and x0,y0x_0,y_0 are a set of specific solution, which could be found by back substitution. By back substitution in the lines of extended Euclidean algorithm, we have gcd(504,385)=13×50417×385\gcd(504,385) = 13\times 504-17\times385. So {x=k×13+t(385gcd(504,385))y=k×(17)+t(504gcd(504,385))\begin{cases} x = k \times 13 + t (\frac{385}{\gcd(504,385)})\\ y = k \times (-17) + t (\frac{504}{\gcd(504,385)}) \end{cases} is the general solution for the equation.