You are on page 1of 17

Module 5: Basic Number Theory

Theme 1: Division
Given two integers, say a and b, the quotient b=a may or may not be an integer (e.g., 16=4 = 4 but
12=5 = 2:4). Number theory concerns the former case, and discovers criteria upon which one can

decide about divisibility of two integers.

More formally, for a 6= 0 we say that a divides b if there is another integer k such that

b = ka;
and we write ajb. In short:

ajb if and only if

9 2Z
k

b = ka:

This simple definition leads to many properties of divisibility. For example, let us establish the
following lemma.
Lemma 1 If ajb and ajc, then aj(b + c).
Proof. We give a direct proof. From the definition of divisibility and the hypotheses we know that
there are integers t and s such that

b = ta; c = sa:
Hence

b + c = a(s + t):
Since s + t is an integer, we prove that aj(b + c).
Exercise 5A: Prove the following two facts:
1. If ajb, then ajbc for all integers c.

2. If ajb and bjc, then ajc.

We already noted that an integer may be or not divisible by another integer. However, when
dividing one number by another there is always a quotient and a remainder. More precisely, if a and

d are positive integers then there is a unique q and r such that

a = dq + r
where 0  r < d is a remainder. Observe that the remainder can take only d values 0; 1; : : : ; d

1.

Theme 2: Primes
Primes numbers occupy very prominent role in number theory. A prime number p is an integer
greater than 1 that is divisible only by 1 and itself. A number that is not prime is called composite.
Example 1: The primes less than 100 are:
2; 3; 5; 7; 11; 13; 17; 19; 23; 29; 31; 37; 41; 43; 47; 53; 59; 61; 67; 71; 73; 79; 83; 89; 97:

How many primes are there? We first prove that there are infinite number of primes.
Theorem 1. There are infinite number of primes.
Proof. We provide a proof by contradiction. Actually, it is due to Euclid and it is more than 2000
years old. Let us assume that there is a finite number of primes, say, 2; 3; 5; : : : ; pk where pk is the
largest prime (there is the largest prime since we assumed there are only finitely many of them).
Construct another number

M = 2  3  5p + 1
k

which is a product of all primes plus one. First, observe that none of the primes 2; 3; 5; : : : ; pk
can divide M , since the remainder of dividing M by any of the primes is equal to 1. Since every
number, including M , is divisible by at least two numbers, 1 and itself, there must be another prime,
possible M itself, that is not among the primes 2; 3; 5; : : : ; pk . This contradicts the assumption that
2; 3; 5; : : : ; pk are the only primes.

But how many primes are there smaller than n, where n is a fixed number. This is a very difficult
problem that was solved only in the last century. Basically, there are approximately about n= log(n)

primes smaller than n. For example, there are 25 primes smaller than 100, and 100= log(100)  22.

Primes are important since every integer can be represented as a product of primes. This is known
as the Fundamental Theorem of Arithmetics and we will prove it below.
Example 2: Observe that
100 = 2  2  5  5 = 22 52 ;
381 = 3  127;

888 = 23  3  37:

Theorem 2. [Fundamental Theorem of Arithmetics ] Every positive integer can be written uniquely
as the product of primes where the prime factors are written in order of increasing size, that is, if n is
a natural numbers and p1 < p2 <    < pm are distinct primes, then

n = p11  p22    p
e

e
m

where ei are exponents of pi (i.e., the number of times pi occurs in the factorization of n).
Proof. We give an indirect proof. Let us assume that there are two different prime factorizations of

n, say
n = p11  p22    p
e

n = q1 1  q2 2    q
d

d
r

where q1 <    < qr are primes. Since we factorize the same number n we must have

p11  p22    p
e

= q1d1

 q 2    q r:
d

We first prove that p1 = q1 . If p1 =

6 q1, then p1 can not divide any of the primes q1; : : : ; qr (we say

that p1 is relatively prime to all q1 ; : : : ; qr ). Indeed, since p1 and q1 ; : : : ; qr are primes, none of them

equal, then they must be relatively prime. But, then p1 cannot divide n = q1d1  q2d2    qrdr which is
nonsense since n = pe11  pe22    pemm . Thus, we must conclude that p1 = q1 .

Now we prove that e1 = d1 provided p1 = q1 that we just established above. Again, assume

contrary that e1 < d1 , say d1 = e1 + h, h > 0. Then after dividing everything by pe11 we obtain

p22    p
e

= q1h  q2d2    qrdr :

But then the right-hand side of the above is divisible by q1 while the left-hand side is not, which is
impossible since there is an equality sign between the left-hand side and the right-hand side of the
above. This completes the proof.
How to find out whether an integer is a prime or not? Unfortunately, there is no fast way of doing
it (i.e., there is no efficient algorithm), but one can use some properties of primes and composite
numbers to speed up the process. Here is one useful result.
Lemma 2.If n is a composite integer, then n has a prime divisor less than or equal to

pn.

Proof. Since n is a composite integer, it must have a factor d such that 1 < d < n, that is, n = d  r

pp

dr > n n=n

which is the desired contradiction since we assumed that n = dr . We must conclude that n has at
least one divisor not exceeding

pn.

prime divisor, which certainly must be smaller than

pn.

We can use this lemma, in its contrapositive form, to decide whether n is a prime or not. Indeed.
the above lemma is equivalent to: if n has no prime divisor less than or equal to
prime number.
3

pn, then n is a

Example 3: Let us show that 107 is a prime number. If 107 would be composite, then it has had prime
divisor smaller than

107  10:34. Primes smaller than 10 are 2; 3; 5, and 7. None of it divides 107,

thus it 107 must be a prime number.

There were several attempts to find a systematic way of computing prime numbers. Euclid suggested that (k + 1)-st prime can be computed recursively as follows:

e1 = 2;
e

k +1

e1 e2    e + 1:

For example, the first few numbers are

e2 = 2 + 1 = 3;

e3 = 2  3 + 1 = 7;

e4 = 2  3  7 + 1 = 43:
This is an example of a recurrence that we already encountered in the previous module. All numbers
computed so far are primes. But, unfortunately,

e5 = 2  3  7  43 + 1 = 1807 = 13  139
is not a prime.
In the seventeenth century, a French mathematician Marin Marsenne suggested that 2p

1 is

prime provided p is prime. Unfortunately,

211

1 = 2047 = 23  89:

From now on we shall work under the assumption that there is no easy, simple and fast algorithm
to compute prime numbers.

Theme 3: Greatest Common Divisor

The largest divisor that divides both m and n is called the greatest common divisor of n and m. It
is denoted as gcd(m; n). Formally:
gcd(m; n) := maxfk :

kjm and kjng:

Example 4: What is the greatest common divisor of 24 and 36. One way of finding it is to list all
divisors of 24 and 36 and pick up the largest common to both lists. For example,
divisors of 24 =
divisors of 36 =

f1; 2; 3; 4; 6; 8; 12; 24g;

f1; 2; 3; 4; 6; 9; 12; 18; 36g:
4

Thus gcd(24; 36) = 12. Another, more systematic way is to do prime factorization of both numbers
and pick up the largest common factors. In our case,
24 = 23  3;

36 = 22  32 :

Thus

Generalizing the last example, let

m = p11 p22    p k ;
a

n = p1 p2

p k

b1

b2

be prime factorizations with possible zero exponents. Then

gcd(m; n) = p1

min

a1 ;b1

g pminf
2

a2 ;b2

g    pminf

k k

a ;b

where minfx; y g is the minimum of x and y . Indeed, take the last example to see that
gcd(24; 36) = 2minf2;3g 3minf1;2g :

Exercise 5B: Let us define the least common multiple of m and n as the smallest positive integer
that is divisible by both m and n. It is denoted as lcm(m; n) (e.g., lcm(5; 8) = 40). Prove that for
any positive integers m and n

m  n = gcd(m; n)  lcm(m; n):

We need some more definitions. Two integers, say m and n, may be composite but the only
common divisor of both is 1. In such a case we say that m and n are relatively prime. More generally:
Definition 1. The integers a1 ; a2 ; : : : ; ak are pairwise relatively prime if

8 1  i < j  k:

gcd(ai ; aj ) = 1;

Unlike finding primes, there is an efficient algorithm (a procedure) that finds the greatest common
Example 5: Find gcd(91; 260). We first divide 260 by 91 to find
260 = 2  91 + 78:

Observe that any divisor of 91 and 260 must also be a divisor of 260

2  91 = 78, and vice versa any

divisor of 91 and 78 must be a divisor of 260 = 2  91 + 78. (Indeed, if d is a divisor of 260 and 91,

then there are integers k and l such that 260 = d  k and 91 = d  l, hence 260
so 260

2  91 = d(k

2l),

We now repeat this procedure: we divide 91 by 78 to get

91 = 1  78 + 13:

gcd(91; 260) = gcd(78; 91) = gcd(13; 78):

But

78 = 13  6;

hence finally
gcd(91; 260) = gcd(78; 91) = gcd(13; 78) = gcd(0; 13) = 13

and we conclude that gcd(91; 260) = 13.

From the last example, we should conclude that the greatest common divisor of m and n > m
is the same as the greatest common divisor of m and the remainder of the division of n by m (i.e.,

n = q  m + r, where q is an integer and 0  r < m). Indeed, if d is a divisor of m and n, then it

must also divides r = n q  m, and vice versa if d divides m and r , then it divides n = m  q + r .
Therefore,
gcd(m; n) = gcd(r; m):

In previous modules we have used an abbreviation for a remainder. Indeed, we write

r := n mod m
where n = q  m + r . This is called modular arithmetic and we will be devoted the next section

it. For now, we just use the fact that the remainder r can be also written as n mod m. Then the last
equation, can be expressed as
gcd(m; n) = gcd(n mod m; m):

(1)

From the example above, we conclude that we can use (1) successively until we reach gcd(0; m) = m.
In summary, we design the following algorithm that computes gcd(m; n):
A LGORITHM: The Euclidean Algorithm
6

x := m
y := n
while y 6= 0 do r := x mod y
x := y
y := r
end
gcd(m; n) := x.

Example 6: Find gcd(414; 662). According to the Euclidean algorithm we proceed as follows:
gcd(414; 662) = gcd(248; 414) = gcd(166; 248) = gcd(82; 166) = gcd(82; 2) = gcd(2; 0) = 2:

Theme 4: Modular Arithmetic

We have already seen in previous modules modular arithmetic. It is about the remainder of an integer
when it is divided by another specific natural integer. It occurs in many applications (e.g., when
counting time over a 24-hour clock since after 24:00 we have 1 am, 2 am, etc.).
Definition 2. (i) Let n be an integer and M be a positive integer. We denote by

r := n mod M
the remainder r when n is divided by M , that is,

n=qM +r
where q is an integer and 0  r < M .
(ii) Let n and m be integers and M a positive integer. We say that n is congruent to m modulo M
if M divides n

m. We shall write
n  m mod M

if and only if

M j(n

m):

If n are m are not congruent modulo M , then we write n 6 m mod M .

Example 7: We have
18 mod 5 = 3;

We also have

145 mod 9 = 8:

17  5 mod 6;

24 6 14 mod 6:

23 mod 4 = 4?

7

Theorem 3. Let a  b mod m and c  d mod m. Then

a+c
ac




b + d mod m;

(2)

bd mod m:

(3)

Proof. Since a  b mod m and c  d mod m, hence there are integers s and t such that

b = sm + a;
d = c + tm:
Therefore

b + d = (a + c) + m(s + t);
bd = (a + sm)(c + tm) = ac + m(at + cs + stm)
which prove (2) and (3).
Example 8: Let 7  2 mod 5 and 11  1 mod 5. Then
18 = 7 + 11  2 + 1 mod 5

and

77 = 7  11  2  1 mod 5:

From Theorem 3 we conclude that

(a + b) mod m = [(a mod m) + (b mod m)] mod m;
(a  b) mod m = [(a mod m)  (b mod m)] mod m:

(4)
(5)

Identities (4)(5) are useful when one needs to compute modulo m over large numbers or products of
large numbers. For example, let a = 123 and b = 234. Then
120  234 mod 5 = (123 mod 5)  (234 mod 5) mod 5 = 3  4 mod 5 = 2:

s

Let us compute 57229

likely occur since 572

mod 713.

29

If one tries to estimate this directly on a computer, overflow will

is a huge number. But let us use (5). We first represent the exponent 29 as
29 = 24 + 23 + 22 + 20 :

We now compute 572 to each of the powers 16; 8; 4 and 1 modulo 713. Here is the calculation
(observe how easy it is!):

mod 713
5724 mod 713
5728 mod 713
57216 mod 713
57229 mod 713
5722

mod 713)2 mod 713 = 6302 mod 713 = 472;

(5724 mod 713)2 mod 713 = 4722 mod 713 = 328;
(5728 mod 713)2 mod 713 = 3282 mod 713 = 634;
(57216 mod 713)  (5728 mod 713)  (5724 mod 713)  (572 mod 713) mod 713
634  328  472  572 mod 713 = 113:

= (5722
=
=
=
=

Theme 5: Applications
We shall discuss here some applications of numbers theory, namely, hashing, pseudo random generators, and cryptosystems based on modular arithmetic.

Hashing
Often one needs a fast methods of locating a given record in a huge set of records. Hashing is a
possible solution. It works as follows. Every record has a key, k , which uniquely identifies it. A
hashing function h(k ) maps the set of keys into the available memory locations.
In practice, the most common hashing function is

h(k) = k mod m
where m is the size of the memory.
Example 9: Let m = 111 and let keys be social security numbers of students. In particular,

h(064212848) = 064212848 mod 111 = 14

h(037149212) = 037149212 mod 111 = 65:
Observe that hashing is not one-to-one function, hence some records may be hashed into the same
location. For example,

h(107405723) = 107405723 mod 111 = 14:

Thus two records are mapped into the location 14. Since this location was already occupied by the
previous record, the new collided record is moved to the next empty location modulo m = 111. In
our case, it is at memory location 15.

Pseudo Random Number Generators

In many applications, including hashing, one needs to generate numbers that look randomly. For
example, in hashing we want to spread out uniformly all records over the memory so to minimize the
number of collisions. We should point out that most random generators compute deterministically
numbers, therefore, we call them pseudo random generators. We require, however, that a statistical
test applied to them will not distinguish these numbers from randomly generated numbers.
The most common procedure to generate pseudo random numbers is the linear congruential
method. In this method we choose (very carefully) the modulus m, multiplier a, increment c, and
seed x0 with 2  a < m, 0  c < m, and 0  x0 < m. Then we generate recursively a sequence xn

as

n+1

with x0 given. Observe that 0

= (axn + c) mod m

x

< m, hence at most after m generations a repetition occurs. Of

course, this is not good for random generations, and one must select very carefully the parameters a,
c and m (which should be large) to obtain a long sequence without a repetition.
n

The following result is known.

Theorem 4. [T. Hull and A. Dobel, 1962]The linear congruential generator has a full period (i.e.,
there is no repetition in the first m generations) if and only if the following three conditions hold:
(i) Both m and c are relatively prime, that is, gcd(m; c) = 1.
(ii) If q is a prime number that divides m, then q divides a
(iii) If 4 divides m, then 4 divides a

1.

1.

Cryptology
One of the most important application of congruences is in cryptology, which is a study of secret
messages. The first encryption algorithms were very simple. For example, Julius Caesar designed an
encryption system by shifting each letter three letters in the alphabet. Mathematically speaking, in
this case the encryption function f (p) is defined as

f (p) = (p + 3) mod 26:

Then decryption is merely finding the inverse function f

(p) = (p

, which in this case is

3) mod 26:

The above encryption system is too easy to break. Therefore, in mid-1970 the concept of public
key cryptosystem was introduced. In such a system, every person can have a publicly known encryption key to send encrypted message, but only those who have secret key can decrypt the message. We
10

describe below a system known as the RSA encryption system (RSA name is built from the initials
of the inventors Rivest, Shamir and Adleman).
In the RSA system, the message M to be sent is first transformed into an integer representing
it (with some abuse of notation we denote such an integer by M ). The RSA is based on modular
exponentiation modulo of the product of two large primes, say p and q . Define n = pq and  =
(p

1)(q

1). In practice,

p and q have 100 digits each, thus n has 200 digits. Define now an

exponent e as
gcd(e; ) = 1;

1)(q

1). The cipher text C of the original message M is

computed as follows

C = M mod n
e

(6)

The RSA decryption works as follows: We first find a number d defined as

de = 1 mod  = (p

1)(q

1):

The number d is called inverse of e modulo . It should be underlined that d can be found fast (based
on the Euclidean algorithm) only if one knows both primes p and q , not the product pq . Then, it can
be proved (see below) that

 M mod n = pq:

(7)

Example 10: Let us encrypt the message ST OP using the RSA with p = 43 and q = 59. Thus

n = 43  59 = 2537, and one finds e = 13 since gcd(13; 42  58) = 1.

We now transform the message ST OP into its numerical equivalent (where A = 00, B =
02; : : : Z = 25) and group them in pairs. We obtain
1819 1415:

mod 2537
141513 mod 2537
181913

= 2081;
= 2182:

Hence, the encrypted message is C = 2081 2182.

Now, to decrypt it, we first find the inverse d. Using the Euclidean algorithm (and knowing

2081937

2182937

and

11

Mathematics behind RSA

In this subsection, we present in some details mathematical ideas used in the construction of the RSA
algorithm. Our main goal is to justify mathematically the decoding procedure (7).
Let us start with introducing an inverse modulo m. We say that a
 is an inverse of a modulo m if

aa  1 mod m:
In order to compute the inverse, we must plunge into another aspect of number theory. We claim
that for any positive a and b there exist integers s and t such that
gcd(a; b) = sa + tb:

(8)

We explain how to construct these two numbers on an example.

Example 11: Let us use Euclidean algorithm to compute gcd(396; 504). We proceed according to
the algorithm as follows:
504 = 396 + 108

396 = 3  108 + 72
108 = 72 + 36
72 = 2  36;

Thus gcd(396; 504) = 36. To find the representation (8) we work backward the Euclidean algorithm
starting from the next-to-last devision above, that is,
gcd(396; 504) = 36 = 108

72

= 108

(396

= 4(504

396)

3  108) = 4  108
396 = 4  504

396

5  396

= ( 5)a + (4)b

to prove (8) in general terms.

Now we can go back to the inverse modulo m construction. Let us assume that gcd(a; m) = 1.
Then from the fact just proved we conclude that there must exist integers s and t such that

sa + tm = 1:
This certainly implies that

sa + tm  1 mod m:

12

But since m divides tm we conclude that

sa  1 mod m:
Consequently s = a
 is the inverse of a modulo m.
In summary, weve just established the following result.
Theorem 5. If m > 1 and gcd(a; m) = 1 (i.e., a and m are relatively prime), then an inverse of a
modulo m exists and it is equal to s in the following representation of gcd(a; m) = 1

sa + tm = 1
which can be found efficiently by the Euclidean algorithm.
Example 12: Lets find the inverse of 3 modulo 7. Since gcd(3; 7) = 1, the inverse exists, and the
Euclidean algorithm gives:

7 =23+1

hence

23+17 =1

and the inverse of 3 modulo 7 is equal to

2.

We need two more results before we can explain the decryption algorithm of RSA. The first one
goes back to ancient Chinese and Hindu mathematicians and it is known as the Chinese Remainder
Theorem. Here is the problem: let m1 ; m2 ; : : : ; mn be pairwise relatively prime positive integers.
Find a solution x modulo m = m1 m2    mn of the following system;

x
x
..
.




a1 mod m1 ;

a mod m ;

a2 mod m2 ;

We now construct a solution to the above system of congruences. Let us define for k = 1; : : : ; n

M =
k

m
= m1    m
m

k +1

m

that is,

My
k

Let us now define

 1 mod m :
k

x = a1 M1 y1 + a2 M2 y2 +    + a M y mod m:
n

13

(9)

We claim it is a simultaneous solution of the above system modulo m. Indeed, we first observe that

 0 mod m

for j =
6 k. But

xa M y
k

since Mk yk

 1 mod m

 a mod m
k

congruences. This is called the Chinese Remainder Theorem.

Example 13: Solve





x
x
x

2 mod 3;
3 mod 5;
2 mod 7:

of M1 = 35 modulo 3, y2 = 1 is inverse of M2 modulo 5, and y3 = 1 is an inverse of M3 modulo 7.

Thus the solution of the above system of congruences

2  35  2 + 3  21  1 + 2  15  1 mod 105

thus the solution x = 23.

Finally, we quote (without a proof) the Fermat Little Theorem.
Theorem 6. [Fermats Little Theorem] If p is a prime number and a is an integers not divisible by

p, then
a

or equivalently

 1 mod p

 a mod p:

Now, we are ready to explain the decryption procedure (7) of the RSA algorithm. We recall that

d is inverse of e modulo  = (p

1)(q

1), that is,

de  1 mod :
This implies that there is an integer k such that

de = 1 + k:
Therefore by the Fermat theorem

C =M
d

de

= M  (M p

)k (q

14

1)

 M  1  M mod p;

and

C = M  (M
d

since M p

 1 modp and M

)k(p

1)

 M  1  M mod q

 M mod pq

as desired.

15

Assignment 5.1: Basic Number Theory Problems

Each assignment is worth 10 points.
1. Show that if a; b and c 6= 0 are integers such that (ac)j(bc), then ajb.
2. Find the prime factorization of 10!.
3. Use the Euclidean algorithm to find
(a) gcd(1529; 14039),
(b) gcd(1111; 11111).
4. Find an inverse of 2 modulo 17.
5. Encrypt the message ATTACK using the RSA system with n = 43  59 and e = 13, translating

each letter into integers (where A = 00, B = 01; : : : Z = 25) and grouping pairs of integers, as we

did in our Example 10.

16

Solutions to Exercises
Solution to Exercise 5A

We first prove that if ajb, then ajbc for all integers c. Indeed, since ajb there must be an integer k

such that b = k  a. This implies b  c = k  c  a, hence ajbc for any integer c.

Now we prove if ajb and bjc, then ajc. From the hypotheses we conclude that there are integers k

17