0% found this document useful (0 votes)
62 views63 pages

DS Chapter 04

This document discusses number theory and cryptography as covered in Chapter 4 of the textbook "Discrete Structures". It begins with an introduction to number theory and its basic principles such as divisibility, greatest common divisors, least common multiples, and modular arithmetic. It then covers various topics in number theory including the division algorithm, congruences, modular arithmetic, and different number systems for representing integers such as binary, octal, and hexadecimal. Examples are provided to illustrate key concepts throughout the chapter.

Uploaded by

Asim Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views63 pages

DS Chapter 04

This document discusses number theory and cryptography as covered in Chapter 4 of the textbook "Discrete Structures". It begins with an introduction to number theory and its basic principles such as divisibility, greatest common divisors, least common multiples, and modular arithmetic. It then covers various topics in number theory including the division algorithm, congruences, modular arithmetic, and different number systems for representing integers such as binary, octal, and hexadecimal. Examples are provided to illustrate key concepts throughout the chapter.

Uploaded by

Asim Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 63

Discrete Structures

Chapter 4
Number Theory and Cryptography

Prof. Dr. Malik Sikander Hayat Khiyal


Faculty of Computer Science,
Preston University, Islamabad Campus,
85, Street 3, Sector H-8/1,
Islamabad, PAKISTAN.
[email protected]
01/31/24 Discrete Mathematics Chapter 4 1
Discrete Structures Contents
Chapter 1. The Foundations: Logic and Proofs
Chapter 2. Basic Structures: Sets, Functions, Sequences, and Sums
Chapter 3. The Fundamentals: Algorithms, the Integers, and Matrices
Chapter 4. Number Theory and Cryptography
Chapter 5. Induction and Recursion
Chapter 6. Counting
Chapter 7. Discrete Probability
Chapter 8. Advanced Counting Technique
Chapter 9. Relations
Chapter 10. Graphs
Chapter 11. Trees
Chapter 12. Boolean Algebra

01/31/24 Discrete Mathematics Chapter 4 2


Introduction to Number Theory

Number theory is about integers and their properties.

We will start with the basic principles of


divisibility,
greatest common divisors,
least common multiples, and
modular arithmetic

and look at some relevant algorithms.

01/31/24 Discrete Mathematics Chapter 4 3


Division
If a and b are integers with a  0, we say that
a divides b if there is an integer c so that b = ac.
When a divides b we say that a is a factor of b and that b
is a multiple of a.
The notation a | b means that a divides b.
We write a χ b when a does not divide b

EXAMPLE. Determine whether 3 ∣ 7 and whether 3 ∣ 12.


Solution: We see that 3 χ 7, because 7∕3 is not an integer.
On the other hand, 3 ∣ 12 because 12∕3 = 4.

01/31/24 Discrete Mathematics Chapter 4 4


Divisibility Theorems
For integers a, b, and c it is true that

• if a | b and a | c, then a | (b + c)
Example: 3 | 6 and 3 | 9, so 3 | 15.

• if a | b, then a | bc for all integers c


Example: 5 | 10, so 5 | 20, 5 | 30, 5 | 40, …

• if a | b and b | c, then a | c
Example: 4 | 8 and 8 | 24, so 4 | 24.

01/31/24 Discrete Mathematics Chapter 4 5


The Division Algorithm
Let a be an integer and d a positive integer. Then
there are unique integers q and r, with 0  r < d, such
that a = dq + r.

In the above equation,


• d is called the divisor,
• a is called the dividend,
• q is called the quotient, and
• r is called the remainder.

01/31/24 Discrete Mathematics Chapter 4 6


The Division Algorithm
Definition: In the equality given in the division
algorithm, d is called the divisor, a is called the
dividend, q is called the quotient, and r is called
the remainder. This notation is used to express the
quotient and remainder:
q = a div d, r = a mod d.

01/31/24 Discrete Mathematics Chapter 4 7


The Division Algorithm
Example: When we divide 17 by 5, we have
17 = 53 + 2.
• 17 is the dividend,
• 5 is the divisor,
• 3 is called the quotient, and
• 2 is called the remainder.
Example: What are the quotient and remainder when 101
is divided by 11?
Solution: We have 101 = 11 ⋅ 9 + 2.
Hence, the quotient when 101 is divided by 11 is 9 = 101
div 11, and the remainder is 2 = 101 mod 11.
01/31/24 Discrete Mathematics Chapter 4 8
Congruence
Let a and b be integers and m be a positive integer. We
say that a is congruent to b modulo m if
m divides a – b.
We use the notation a  b (mod m) to indicate that a is
congruent to b modulo m.

THEOREM: Let a and b be integers, and let m be a


positive integer. Then a ≡ b (mod m) if and only if a
mod m = b mod m.

01/31/24 Discrete Mathematics Chapter 4 9


Congruence
EXAMPLE : Determine whether 17 is
congruent to 5 modulo 6 and whether 24
and 14 are congruent modulo 6.

Solution: Because 6 divides 17 − 5 = 12, we


see that 17 ≡ 5 (mod 6). However, because
24 − 14 = 10 is not divisible by 6, we see
that 24 ≢ 14 (mod 6).

01/31/24 Discrete Mathematics Chapter 4 10


Congruences
Examples:
Is it true that 46  68 (mod 11) ?
Yes, because 11 | (46 – 68).
Is it true that 46  68 (mod 22)?
Yes, because 22 | (46 – 68).
For which integers z is it true that z  12 (mod 10)?
It is true for any z{…,-28, -18, -8, 2, 12, 22, 32, …}

Theorem: Let m be a positive integer. The integers a and b


are congruent modulo m if and only if there is an integer k
such that a = b + km.

01/31/24 Discrete Mathematics Chapter 4 11


Congruence
Theorem: Let m be a positive integer.
If a  b (mod m) and c  d (mod m), then
a + c  b + d (mod m) and ac  bd (mod m).
Proof:
We know that a  b (mod m) and c  d (mod m) implies
that there are integers s and t with
b = a + sm and d = c + tm.
Therefore,
b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) and
bd = (a + sm)(c + tm) = ac + m(at + cs + stm).
Hence, a + c  b + d (mod m) and ac  bd (mod m).

01/31/24 Discrete Mathematics Chapter 4 12


Congruence

EXAMPLE 6 Because 7 ≡ 2 (mod 5) and


11 ≡ 1 (mod 5), it follows from Theorem
that
18 = 7 + 11 ≡ 2 + 1 = 3 (mod 5)
and that
77 = 7 ⋅ 11 ≡ 2 ⋅ 1 = 2 (mod 5).

01/31/24 Discrete Mathematics Chapter 4 13


Congruence
Theorem: Let m be a positive integer. a  b (mod m) iff a mod
m = b mod m.
Proof:
Let a = mq1 + r1, and b = mq2 + r2.
Only if part: a mod m = b mod m  r1 = r2, therefore
a – b = m(q1 – q2), and a  b (mod m).
If part: a  b (mod m) implies
a – b = mq
mq1 + r1 – (mq2 + r2) = mq
r1 – r2 = m(q – q1 + q2).
Since 0  r1, r2  m, 0  |r1 - r2 |  m. The only multiple in that
range is 0.
Therefore r1 = r2, and a mod m = b mod m.
01/31/24 Discrete Mathematics Chapter 4 14
Congruence
EXAMPLE: Find the value of (193 mod 31)4 mod 23.
Solution: To compute (193 mod 31)4 mod 23, we will
first evaluate 193 mod 31. Because 193 = 46859
and 68459 = 221 ⋅ 31 + 8, we have
193 mod 31 = 6859 mod 31 = 8. So,
(193 mod 31)4 mod 23 = 84 mod 23.
Next, note that 84 = 4096. Because 4096 = 178 ⋅ 23 +
2, we have 4096 mod 23 = 2.
Hence, (193 mod 31)4 mod 23 = 2.

01/31/24 Discrete Mathematics Chapter 4 15


Modular Arithmetic
Let a be an integer and m be a positive integer.
We denote by a mod m the remainder when a is divided
by m.

Examples:
9 mod 4 = 1
9 mod 3 = 0
9 mod 10 = 9

-13 mod 4 = 3

01/31/24 Discrete Mathematics Chapter 4 16


Representations of Integers
Let b be a positive integer greater than 1. Then if n is a
positive integer, it can be expressed uniquely in the
form:

n = akbk + ak-1bk-1 + … + a1b + a0,

where k is a nonnegative integer, a0, a1, …, ak are


nonnegative integers less than b, and ak  0.

Example for b=10:


859 = 8102 + 5101 + 9100

01/31/24 Discrete Mathematics Chapter 4 17


Representations of Integers
BINARY EXPANSIONS Choosing 2 as the base gives
binary expansions of integers. In binary notation each
digit is either a 0 or a 1. In other words, the binary
expansion of an integer is just a bit string. Binary
expansions are used by computers to represent and do
arithmetic with integers
EXAMPLE: What is the decimal expansion of the integer
that has (1 0101 1111)2 as its binary expansion?
Solution: We have
(1 0101 1111)2 = 1 ⋅ 28 + 0 ⋅ 27 + 1 ⋅ 26 + 0 ⋅ 25 + 1 ⋅ 24
+ 1 ⋅ 23 + 1 ⋅ 22 + 1 ⋅ 21 + 1 ⋅ 20 = 351.

01/31/24 Discrete Mathematics Chapter 4 18


Representations of Integers
OCTAL AND HEXADECIMAL EXPANSIONS Among the
most important bases in computer science are base 2, base 8,
and base 16. Base 8 expansions are called octal expansions
and base 16 expansions are hexadecimal expansions.
EXAMPLE: What is the decimal expansion of the number with
octal expansion (7016)8?
Solution: Using the definition of a base b expansion with b = 8
tells us that (7016)8 = 7 ⋅ 83 + 0 ⋅ 82 + 1 ⋅ 8 + 6 = 3598.
EXAMPLE: What is the decimal expansion of the number
with hexadecimal expansion (2AE0B)16?
Solution: Using the definition of a base b expansion with b = 16
tells us that (2AE0B)16 = 2 ⋅ 164 + 10 ⋅ 163 + 14 ⋅ 162 + 0 ⋅ 16
+ 11 = 175627.
01/31/24 Discrete Mathematics Chapter 4 19
Representations of Integers

Example for b=2 (binary expansion):


(10110)2 = 124 + 122 + 121 = (22)10

Example for b=16 (hexadecimal expansion):


(we use letters A to F to indicate numbers 10 to 15)
(3A0F)16 = 3163 + 10162 + 15160 = (14863)10

01/31/24 Discrete Mathematics Chapter 4 20


Representations of Integers
Base Conversion:
How can we construct the base b expansion of an integer n?
First, divide n by b to obtain a quotient q0 and remainder a0,
that is,
n = bq0 + a0, where 0  a0 < b.
The remainder a0 is the rightmost digit in the base b expansion
of n.
Next, divide q0 by b to obtain:
q0 = bq1 + a1, where 0  a1 < b.
a1 is the second digit from the right in the base b expansion of
n. Continue this process until you obtain a quotient equal to
zero.
01/31/24 Discrete Mathematics Chapter 4 21
Representations of Integers
Example:
What is the (Octal) base 8 expansion of (12345) 10 ?
First, divide 12345 by 8:
12345 = 81543 + 1
1543 = 8192 + 7
192 = 824 + 0
24 = 83 + 0
3 = 80 + 3
The successive remainders that we have found, 1, 7, 0,
0, and 3, are the digits from the right to the left of
12345 in base 8. Hence, the result is:
(12345)10 = (30071)8.
01/31/24 Discrete Mathematics Chapter 4 22
Representations of Integers
Example: Find the hexadecimal expansion of (177130)10.
Solution: First divide 177130 by 16 to obtain 177130 = 16 ⋅
11070 + 10.
Successively dividing quotients by 16 gives
11070 = 16 ⋅ 691 + 14,
691 = 16 ⋅ 43 + 3,
43 = 16 ⋅ 2 + 11,
2 = 16 ⋅ 0 + 2.
The successive remainders that we have found, 10, 14, 3, 11, 2,
give us the digits from the right to the left of 177130 in the
hexadecimal (base 16) expansion of (177130)10. It follows that
(177130)10 = (2B3EA)16.
01/31/24 Discrete Mathematics Chapter 4 23
Representations of Integers
Example: Find the binary expansion of (241)10.
Solution: First divide 241 by 2 to obtain 241 = 2 ⋅ 120 + 1.
Successively dividing quotients by 2 gives
120 = 2 ⋅ 60 + 0,
60 = 2 ⋅ 30 + 0,
30 = 2 ⋅ 15 + 0,
15 = 2 ⋅ 7 + 1,
7 = 2 ⋅ 3 + 1,
3 = 2 ⋅ 1 + 1,
1 = 2 ⋅ 0 + 1.
The successive remainders that we have found, 1, 0, 0, 0, 1, 1, 1, 1, are
the digits from the right to the left in the binary (base 2) expansion
of (241)10. Hence,
(241)10 = (1111 0001)2.
01/31/24 Discrete Mathematics Chapter 4 24
Representations of Integers
TABLE 1 Hexadecimal, Octal, and Binary Representation of the Integers 0 through 15.
Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Hexadecimal 0 1 2 3 4 5 6 7 8 9 A B C D E F

Octal 0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17

Binary 0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111

Example: Find the octal and hexadecimal expansions of (11 1110


1011 1100)2 and the binary expansions of (765)8 and (A8D)16.
Solution: To convert (11 1110 1011 1100)2 into octal notation we
group the binary dig- its 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,
(11 1110 1011 1100)2 = (37274)8.

01/31/24 Discrete Mathematics Chapter 4 25


Representations of Integers
To convert (11 1110 1011 1100)2 into hexadecimal notation we
group the binary digits into blocks of four, adding initial
zeros at the start of the leftmost block if necessary. 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.
To convert (765)8 into binary notation, we replace each octal
digit by a block of three binary digits. These blocks are 111,
110, and 101. Hence, (765)8 = (1 1111 0101)2.
To convert (A8D)16 into binary notation, we replace each
hexadecimal digit by a block of four binary digits. These
blocks are 1010, 1000, and 1101. Hence, (A8D)16 = (1010
1000 1101)2.
01/31/24 Discrete Mathematics Chapter 4 26
Representations of Integers
procedure base_b_expansion(n, b: positive integers)
q := n
k := 0
while q  0
begin
ak := q mod b
q := q/b
k := k + 1
end
{the base b expansion of n is (ak-1 … a1a0)b }

01/31/24 Discrete Mathematics Chapter 4 27


Addition of Integers
How do we (humans) add two integers?

1 11 carry
Example: 7583
+ 4932
12515
1 1 carry
Binary expansions: (1011)2
+ (1010)2

( 1 0 1 0 1 )2

01/31/24 Discrete Mathematics Chapter 4 28


Addition of Integers
Let a = (an-1an-2…a1a0)2, b = (bn-1bn-2…b1b0)2.
How can we algorithmically add these two binary numbers?
First, add their rightmost bits:
a0 + b0 = c02 + s0,
where s0 is the rightmost bit in the binary expansion of a +
b, and c0 is the carry.
Then, add the next pair of bits and the carry:
a1 + b1 + c0 = c12 + s1,
where s1 is the next bit in the binary expansion of a + b, and
c1 is the carry.
01/31/24 Discrete Mathematics Chapter 4 29
Addition of Integers

Continue this process until you obtain cn-1.

The leading bit of the sum is sn = cn-1.

The result is:


a + b = (snsn-1…s1s0)2

01/31/24 Discrete Mathematics Chapter 4 30


Addition of Integers
Example:
Add a = (1110)2 and b = (1011)2.

a0 + b0 = 0 + 1 = 02 + 1, so that c0 = 0 and s0 = 1.


a1 + b1 + c0 = 1 + 1 + 0 = 12 + 0, so c1 = 1 and s1 = 0.
a2 + b2 + c1 = 1 + 0 + 1 = 12 + 0, so c2 = 1 and s2 = 0.
a3 + b3 + c2 = 1 + 1 + 1 = 12 + 1, so c3 = 1 and s3 = 1.
s4 = c3 = 1.

Therefore, s = a + b = (11001)2.
01/31/24 Discrete Mathematics Chapter 4 31
Addition of Integers
procedure add(a, b: positive integers)
c := 0
for j := 0 to n-1
begin
d := (aj + bj + c)/2
sj := aj + bj + c – 2d
c := d
end
sn := c
{the binary expansion of the sum is (snsn-1…s1s0)2}

01/31/24 Discrete Mathematics Chapter 4 32


Multiplication of Integers
Algorithm: Multiplication of Integers.
procedure multiply(a, b: positive integers)
{the binary expansions of a and b are (an−1an−2 … a1a0)2
and (bn−1bn−2 … b1b0)2, respectively}
for j := 0 to n − 1
if bj = 1 then cj := a shifted j places
else cj := 0
{c0, c1, … , cn−1 are the partial products}
p := 0
for j := 0 to n − 1
p := add(p, cj)
return p {p is the value of ab}
01/31/24 Discrete Mathematics Chapter 4 33
Multiplication of Integers
Example: Find the product of a = (110)2 and b =
(101)2.
Solution: First note that
ab0 ⋅ 20 = (110)2 ⋅ 1 ⋅ 20 = (110)2,
ab1 ⋅ 21 = (110)2 ⋅ 0 ⋅ 21 = (0000)2,
and
ab2 ⋅ 22 = (110)2 ⋅ 1 ⋅ 22 = (11000)2.
To find the product, add (110)2, (0000)2, and (11000)2.
Carrying out these additions (using Algorithm,
including initial zero bits when necessary) shows that
ab = (1 1110)2.
01/31/24 Discrete Mathematics Chapter 4 34
Multiplication of Integers
110
×101
110
000
110
11110
Multiplying (110)2 and (101)2.

01/31/24 Discrete Mathematics Chapter 4 35


Primes
A positive integer p greater than 1 is called prime if the
only positive factors of p are 1 and p.
Note: 1 is not a prime
A positive integer that is greater than 1 and is not prime
is called composite.
Example: The integer 7 is prime because its only
positive factors are 1 and 7, whereas the integer 9 is
composite because it is divisible by 3.
The fundamental theorem of arithmetic:
Every positive integer can be written uniquely as the
product of primes, where the prime factors are written
in order of increasing size.
01/31/24 Discrete Mathematics Chapter 4 36
Primes
Theorem: The Fundamental Theorem of Arithmetic:
Every integer greater than 1 can be written uniquely as a
prime or as the product of two or more primes, where the
prime factors are written in order of nondecreasing size.

Example : The prime factorizations of 100, 641, 999, and


1024 are given by
100 = 2 ⋅ 2 ⋅ 5 ⋅ 5 = 22 52,
641 = 641,
999 = 3 ⋅ 3 ⋅ 3 ⋅ 37 = 33 ⋅ 37 ,
1024 = 2 ⋅ 2 ⋅ 2 ⋅ 2 ⋅ 2 ⋅ 2 ⋅ 2 ⋅ 2 ⋅ 2 ⋅ 2 = 210.

01/31/24 Discrete Mathematics Chapter 4 37


Primes
Examples:
15 = 3·5

48 = 2·2·2·2·3 = 24·3
17 = 17
100 = 2·2·5·5 = 22·52

512 = 2·2·2·2·2·2·2·2·2 = 29
515 = 5·103
28 = 2·2·7

01/31/24 Discrete Mathematics Chapter 4 38


Primes
Theorem: If n is a composite integer, then n has a prime
divisor less than or equal . n
This is easy to see: if n is a composite integer, it must
have at least two prime divisors. Let the largest two be
p1 and p2. Then p1p2 <= n.
p1 and p2 cannot both be greater than n , because then
p1p2 > n.
Example: Show that 101 is prime.
Solution: The only primes not exceeding √101 are 2, 3,
5, and 7. Because 101 is not divisible
by 2, 3, 5, or 7 (the quotient of 101 and each of these
integers is not an integer), it follows that
101 is prime.
01/31/24 Discrete Mathematics Chapter 4 39
Greatest Common Divisors
Let a and b be integers, not both zero.
The largest integer d such that d | a and d | b is called the
greatest common divisor of a and b.
The greatest common divisor of a and b is denoted by
gcd(a, b).
Example 1: What is gcd(48, 72) ?
The positive common divisors of 48 and 72 are
1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 24.
Example 2: What is gcd(19, 72) ?
The only positive common divisor of 19 and 72 is
1, so gcd(19, 72) = 1.
01/31/24 Discrete Mathematics Chapter 4 40
Greatest Common Divisors
Using prime factorizations:

a = p 1 a1 p 2 a2 … p n an , b = p 1 b 1 p 2 b 2 … p n b n ,
where p1 < p2 < … < pn and ai, bi  N for 1  i  n

gcd(a, b) = p1min(a1, b1 ) p2min(a2, b2 ) … pnmin(an, bn )

Example:
a = 60 = 22 31 51

b = 54 = 21 33 50
gcd(a, b) = 21 31 50 = 6

01/31/24 Discrete Mathematics Chapter 4 41


Relatively Prime Integers
Definition:
Two integers a and b are relatively prime if
gcd(a, b) = 1.

Examples:
Are 15 and 28 relatively prime?
Yes, gcd(15, 28) = 1.
Are 55 and 28 relatively prime?
Yes, gcd(55, 28) = 1.
Are 35 and 28 relatively prime?
No, gcd(35, 28) = 7.
01/31/24 Discrete Mathematics Chapter 4 42
Relatively Prime Integers
Definition:
The integers a1, a2, …, an are pairwise relatively
prime if gcd(ai, aj) = 1 whenever 1  i < j  n.

Examples:
Are 15, 17, and 27 pairwise relatively prime?
No, because gcd(15, 27) = 3.
Are 15, 17, and 28 pairwise relatively prime?
Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 and
gcd(17, 28) = 1.
01/31/24 Discrete Mathematics Chapter 4 43
Least Common Multiples
Definition:
The least common multiple of the positive integers a
and b is the smallest positive integer that is divisible by
both a and b.
We denote the least common multiple of a and b by
lcm(a, b).
Examples:
lcm(3, 7) = 21
lcm(4, 6) = 12
lcm(5, 10) = 10
01/31/24 Discrete Mathematics Chapter 4 44
Least Common Multiples
Using prime factorizations:

a = p 1 a1 p 2 a2 … p n an , b = p 1 b 1 p 2 b 2 … p n b n ,
where p1 < p2 < … < pn and ai, bi  N for 1  i  n

lcm(a, b) = p1max(a1, b1 ) p2max(a2, b2 ) … pnmax(an, bn )

Example:
a = 60 = 22 31 51

b = 54 = 21 33 50
lcm(a, b) = 22 33 51 = 4.27.5 = 540

01/31/24 Discrete Mathematics Chapter 4 45


GCD and LCM

a = 60 = 22 31 51

b = 54 = 21 33 50

gcd(a, b) = 21 31 50 =6

lcm(a, b) = 22 33 51 = 540

Theorem: a.b = gcd(a,b).lcm(a,b)

01/31/24 Discrete Mathematics Chapter 4 46


The Euclidean Algorithm
The Euclidean Algorithm finds the greatest common
divisor of two integers a and b.
For example, if we want to find gcd(287, 91), we divide
287 by 91:
287 = 913 + 14
We know that for integers a, b and c,
if a | b and a | c, then a | (b + c).
Therefore, any divisor (including their gcd) of 287 and 91
must also be a divisor of 287 - 913 = 14.
Consequently, gcd(287, 91) = gcd(14, 91).

01/31/24 Discrete Mathematics Chapter 4 47


The Euclidean Algorithm
In the next step, we divide 91 by 14:
91 = 146 + 7
This means that gcd(14, 91) = gcd(14, 7).

So we divide 14 by 7:
14 = 72 + 0
We find that 7 | 14, and thus gcd(14, 7) = 7.

Therefore, gcd(287, 91) = 7.


01/31/24 Discrete Mathematics Chapter 4 48
The Euclidean Algorithm
Example: Find the greatest common divisor of
414 and 662 using the Euclidean algorithm.
Solution: Successive uses of the division
algorithm give:
662 = 414 ⋅ 1 + 248
414 = 248 ⋅ 1 + 166
248 = 166 ⋅ 1 + 82
166 = 82 ⋅ 2 + 2
82 = 2 ⋅ 41.

01/31/24 Discrete Mathematics Chapter 4 49


The Euclidean Algorithm
In pseudocode, the algorithm can be implemented as
follows:

procedure gcd(a, b: positive integers)


x := a
y := b
while y  0
begin
r := x mod y
x := y
y := r
end {x is gcd(a, b)}

01/31/24 Discrete Mathematics Chapter 4 50


Solving Congruences
A congruence of the form ax ≡ b (mod m),
where m is a positive integer, a and b are
integers, and x is a variable, is called a linear
congruence.
Theorem: If a and m are relatively prime
integers and m > 1, then an inverse of a
modulo m exists. Furthermore, this inverse is
unique modulo m. (That is, there is a unique
positiveainteger less than m that is an inverse
of a modulo m and every other inverse of a
modulo m is congruent
a to modulo m.)
01/31/24 Discrete Mathematics Chapter 4 51
Solving Congruences
Proof: By Theorem, because gcd(a, m) = 1, there are
integers s and t such that
sa + tm = 1.
This implies that
sa + tm ≡ 1 (mod m).
Because tm ≡ 0 (mod m), it follows that
sa ≡ 1 (mod m).
Consequently, s is an inverse of a modulo m. That this
inverse is unique modulo m

01/31/24 Discrete Mathematics Chapter 4 52


Solving Congruences
Example: Find an inverse of 3 modulo 7 by first finding
Bezout coefficients of 3 and 7.
Solution: Because gcd(3, 7) = 1, Theorem tells us that an
inverse of 3 modulo 7 exists. The
Euclidean algorithm ends quickly when used to find the
greatest common divisor of 3 and 7:
7 = 2 ⋅ 3 + 1.
From this equation we see that
−2 ⋅ 3 + 1 ⋅ 7 = 1.
This shows that −2 and 1 are Be´zout coefficients of 3
and 7. We see that −2 is an inverse of 3 modulo 7. Note
that every integer congruent to −2 modulo 7 is also an
inverse of 3, such as 5, −9, 12, and so on.
01/31/24 Discrete Mathematics Chapter 4 53
Solving Congruences
Example: Find an inverse of 101 modulo 4620.
Solution: For completeness, we present all steps used to
compute an inverse of 101 modulo 4620.
First, we use the Euclidean algorithm to show that gcd(101,
4620) = 1. Then we will reverse the steps to find Be´zout
coefficients a and b such that 101a + 4620b = 1. It will then
follow that a is an inverse of 101 modulo 4620. The steps
used by the Euclidean algorithm to find gcd(101, 4620) are
4620 = 45 ⋅ 101 + 75
101 = 1 ⋅ 75 + 26
75 = 2 ⋅ 26 + 23
26 = 1 ⋅ 23 + 3
23 = 7 ⋅ 3 + 2
3=1⋅2+1
2 = 2 ⋅ 1.
01/31/24 Discrete Mathematics Chapter 4 54
Solving Congruences
Because the last nonzero remainder is 1, we know that gcd(101, 4620) =
1. We can now find the Be´zout coefficients for 101 and 4620 by
working backwards through these steps, expressing gcd(101, 4620) = 1
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=3−1⋅2
= 3 − 1 ⋅ (23 − 7 ⋅ 3) = −1 ⋅ 23 + 8 ⋅ 3
= −1 ⋅ 23 + 8 ⋅ (26 − 1 ⋅ 23) = 8 ⋅ 26 − 9 ⋅ 23
= 8 ⋅ 26 − 9 ⋅ (75 − 2 ⋅ 26) = −9 ⋅ 75 + 26 ⋅ 26
= −9 ⋅ 75 + 26 ⋅ (101 − 1 ⋅ 75) = 26 ⋅ 101 − 35 ⋅ 75
= 26 ⋅ 101 − 35 ⋅ (4620 − 45 ⋅ 101) = −35 ⋅ 4620 + 1601 ⋅ 101.
That −35 ⋅ 4620 + 1601 ⋅ 101 = 1 tells us that −35 and 1601 are Be´zout
coefficients of 4620 and 101, and 1601 is an inverse of 101 modulo
4620.

01/31/24 Discrete Mathematics Chapter 4 55


Solving Congruences
Example: What are the solutions of the linear congruence 3x ≡ 4 (mod
7)?
Solution: By Example above we know that −2 is an inverse of 3
modulo 7. Multiplying both sides of the congruence by −2 shows
that
−2 ⋅ 3x ≡ −2 ⋅ 4 (mod 7).
Because −6 ≡ 1 (mod 7) and −8 ≡ 6 (mod 7), it follows that if x is a
solution, then x ≡ −8 ≡ 6 (mod 7).
We need to determine whether every x with x ≡ 6 (mod 7) is a
solution. Assume that x ≡ 6 (mod 7). Then, it follows that
3x ≡ 3 ⋅ 6 = 18 ≡ 4 (mod 7),
which shows that all such x satisfy the congruence. We conclude
that the solutions to the congruence are the integers x such that x ≡ 6
(mod 7), namely, 6, 13, 20, … and −1, −8, −15, … .

01/31/24 Discrete Mathematics Chapter 4 56


Solving Congruences
Theorem: THE CHINESE REMAINDER
THEOREM. Let m1, m2, … , mn be pairwise
relatively prime positive integers greater than one
and a1, a2, … , an arbitrary integers. Then the system
x ≡ a1 (mod m1),
x ≡ a2 (mod m2),
⋅⋅⋅
x ≡ an (mod mn)
has a unique solution modulo m1m2…mn.

01/31/24 Discrete Mathematics Chapter 4 57


Solving Congruences
Example: 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 puzzle can be translated into the following
question: What are the solutions of the systems of
congruences
x ≡ 2 (mod 3),
x ≡ 3 (mod 5),
x ≡ 2 (mod 7)?

01/31/24 Discrete Mathematics Chapter 4 58


Solving Congruences
Example: To solve the system of congruences in above
Example, first let m = 3 ⋅ 5 ⋅ 7 = 105, M1 = m∕3 = 35, M2 =
m∕5 = 21, and M3 = m∕7 = 15. We see that 2 is an inverse of
M1 = 35 modulo 3, because 35 ⋅ 2 ≡ 2 ⋅ 2 ≡ 1 (mod 3); 1 is
an inverse of M2 = 21 modulo 5, because 21 ≡ 1 (mod 5);
and 1 is an inverse of M3 = 15 (mod 7), because 15 ≡ 1 (mod
7). The solutions to this system are those x such that
x ≡ a1M1y1 + a2M2y2 + a3M3y3 = 2⋅35⋅2 + 3⋅21⋅1 + 2⋅15⋅1 =
233 ≡ 23 (mod 105).
It follows that 23 is the smallest positive integer that is a
simultaneous solution. We conclude that 23 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.
01/31/24 Discrete Mathematics Chapter 4 59
Solving Congruences

Computer Arithmetic with Large Integers


Suppose that m1, m2, … , mn are pairwise
relatively prime moduli and let m be their product.
By the Chinese remainder theorem, we can show
that an integer a with 0 ≤ a < m can be uniquely
represented by the n-tuple consisting of its
remainders upon division by mi, i = 1, 2, … , n.
That is, we can uniquely represent a by
(a mod m1, a mod m2, … , a mod mn).

01/31/24 Discrete Mathematics Chapter 4 60


Solving Congruences
Example: What are the pairs used to represent the
nonnegative integers less than 12 when they are
represented by the ordered pair where the first
component is the remainder of the integer upon
division by 3 and the second component is the
remainder of the integer upon division by 4?
Solution: We have the following representations,
obtained by finding the remainder of each
integer when it is divided by 3 and by 4:
0 = (0, 0) 4 = (1, 0) 8 = (2, 0)
1 = (1, 1) 5 = (2, 1) 9 = (0, 1)
2 = (2, 2) 6 = (0, 2) 10 = (1, 2)
3 = (0, 3) 7 = (1, 3) 11 = (2, 3).
01/31/24 Discrete Mathematics Chapter 4 61
Solving Congruences
Randomly chosen numbers are often needed for computer
simulations. Different methods have been devised for
generating numbers that have properties of randomly chosen
numbers. Because numbers generated by systematic methods
are not truly random, they are called pseudorandom numbers.
The most commonly used procedure for generating pseudorandom
numbers is the linear congruential method. We choose four
integers: the modulus m, multiplier a, increment c, and seed
x0 with 2 ≤ a < m, 0 ≤ c < m, and 0 ≤ x0 < m. We generate a
sequence of pseudorandom numbers {xn}, with 0 ≤ xn < m for
all n, by successively using the recursively defined function
xn+1 = (a xn + c) mod m.

01/31/24 Discrete Mathematics Chapter 4 62


Solving Congruences
Example : Find the sequence of pseudorandom numbers generated by the linear
congruential method with modulus m = 9, multiplier a = 7, increment c = 4, and seed x0 = 3.
Solution: We compute the terms of this sequence by successively using the recursively
defined function xn+1 = (7xn + 4) mod 9, beginning by inserting the seed x0 = 3 to find x1. We
find that
x1 = 7x0 + 4 mod 9 = 7 ⋅ 3 + 4 mod 9 = 25 mod 9 = 7,
x2 = 7x1 + 4 mod 9 = 7 ⋅ 7 + 4 mod 9 = 53 mod 9 = 8,
x3 = 7x2 + 4 mod 9 = 7 ⋅ 8 + 4 mod 9 = 60 mod 9 = 6,
x4 = 7x3 + 4 mod 9 = 7 ⋅ 6 + 4 mod 9 = 46 mod 9 = 1,
x5 = 7x4 + 4 mod 9 = 7 ⋅ 1 + 4 mod 9 = 11 mod 9 = 2,
x6 = 7x5 + 4 mod 9 = 7 ⋅ 2 + 4 mod 9 = 18 mod 9 = 0,
x7 = 7x6 + 4 mod 9 = 7 ⋅ 0 + 4 mod 9 = 4 mod 9 = 4,
x8 = 7x7 + 4 mod 9 = 7 ⋅ 4 + 4 mod 9 = 32 mod 9 = 5,
x9 = 7x8 + 4 mod 9 = 7 ⋅ 5 + 4 mod 9 = 39 mod 9 = 3.
Because x9 = x0 and because each term depends only on the previous term, we see that the
sequence
3, 7, 8, 6, 1, 2, 0, 4, 5, 3, 7, 8, 6, 1, 2, 0, 4, 5, 3, …
is generated. This sequence contains nine different numbers before repeating .

01/31/24 Discrete Mathematics Chapter 4 63

You might also like