�Discrete Mathematics (MAD101)
Chapter 3:
The Integers & Algorithms
1.3 The integers: số nguyên
Reading: a is divisible by b (a chia hết cho b)
b divides a (b chia a)
b is a factor/ divisor of a (b là nhân tử/ước của a)
a is a multiple of b (a là bội của b)
Example: a) 4∤ 5 b) 4∣ 8 c) 3∣ (12.n) (n ∈ℤ) d) 3∣ (3n+6) (n ∈ℤ)
Properties: Let a, b, c ∈ℤ, a ≠ 0. Then
1.3 The integers: số nguyên
Theorem (Euclid): Let a, d ∈ℤ , d>0. Then there exists unique q, r ∈ℤ such that
Notation: q: quotient (thương), r: remainder (số dư)
Example
The right division is -123 = -7.20 +7
1.3 The integers: số nguyên
Definition: a, b, m ∈ℤ, m >0. Then we say that a is congruent (đồng dư) to b modulo m if
a and b have the same remainder when dividing to m ↔ m∣ (a-b) ↔ a-b = k.m
Notation: a ≡ b (mod m)
Example: True or False ?
-14 ≡ 4 (mod 5) 123 ≢ 13 (mod 5) 15n - 2 ≡ 1 (mod 3) (n∈ℤ)
Properties:
then a +c ≡ b +d (mod m) and ac ≡ bd (mod m)
1.3 The integers: số nguyên
Example 1:
Example 2:
1.3 The integers: số nguyên
Hashing function: Hàm băm
Use: convert large/arbitrary sized data into small integers (memory locations)
H(k)= k mod m
key: k
new key/memory location: k mod m
Example: Consider the set of keys
{100, 420, 78, 2023, 193}
H(k)= k mod 11
Compute the hashes (giá trị băm)
1.3 The integers: số nguyên
Hashing function: Hàm băm
Collison (xung đột):
k₁ ≠ k₂ different keys but having the same
hash value
Solution:
Use Linear Probing Function (dò tuyến tính)
h(k,i)= h(k)+i (mod m)
where i runs from 1 to m-1
( dùng hàm dò tuyến tính để tìm vị trí “trống"
đầu tiên)
1.3 The integers: số nguyên
Pseudo-random numbers: Số giả ngẫu nhiên
Goal: tạo ra một dãy số (xₙ) ‘trông' ngẫu nhiên (giả ngẫu nhiên) nhưng thực ra là có quy luật
Formula: Given x₀
xₙ₊₁= axₙ +b (mod m)
Example:
2, 1, 4, 1, 4, 1, 4, …. —> pseudo- random numbers are 2, 1, 4
2, 7, 9, 5, 6, 2, 7, 9, 5, …—-> pseudo- random numbers are 2, 7, 9, 5, 6
1.3 The integers: số nguyên
Classical cryptography: Mật mã
Caesar’s Encryption (mã hoá): Let k ∈ ℤ. Use the function
f(p) = p+ k mod 26 (p= 0,1,2…,25) (k: key)
to encrypt a message.
Example: Encrypt: “SEE YOU AT THE COFFEE SHOP” by using the above function with k= 3
example: A - 0, B - 1, C - 2, ….
so “SEE YOU AT THE COFFEE SHOP” will become
18 4 4 24 14 20 0 19 19 7 4 2 14 5 5 4 4 18 7 14 15
21 7 7 1 17 23 3 22 21 10 7 5 17 8 8 7 7 21 10 17 18
“ VHH BRX DW VKH FRIIHH VKRS “
1.3 The integers: số nguyên
Classical cryptography: Mật mã
Decryption (giải mã): Use the inverse of the encryption function (that was used to encrypt)
f⁻¹ (p) = p - k (mod m) (p=0,1,2…, 25) (k: key)
Example:
APPENDIX: Alphabetical order:
0-A 1-B 2-C 3-D 4-E 5-F 6-G 7-H 8-I 9-J 10-K 11-L 12-M 13- N 14-O 15-P 16-Q 17-R 18-S 19-T 20-U 21-V 22-W 23-X 24-Y 25-Z
1.3 The integers: số nguyên
Definition:
Example:
Factorization Theorem: If n ∈ ℕ and n >1. Then n can be uniquely factorized as
Where, p₁ < p₂ < ….< pₖ are primes; m₁, m₂, …, mₖ ∈ ℕ*
1.3 The integers: số nguyên
Example: Find the prime factorization of:
11=
396=
Question: Given n ∈ ℕ, how to check if n is prime or not ?
Theorem: If n ∈ ℕ is a composite integer, then n has a prime divisor less than or equal to
Example: 101 is a prime or not ?
Test: , so if 101 is a composite integer, it has a prime divisor p ≤ 10, which can be : 2, 3, 5, 7.
It is easy to check that 101 are not divisible by 2, 3, 5, 7. So 101 is a prime !
1.3 The integers: số nguyên
Example:
b) Find the prime factorization of 7007 ?
1.3 The integers: số nguyên
How many primes ?
Theorem: There are infinitely many primes (vô hạn số nguyên tố) !
Sàng Eratosthenes:
Goal: Tìm tất cả các số nguyên tố nhỏ hơn một số cho trước.
Example: List all primes that are not exceeding 50 ?
1.3 The integers: số nguyên
How many primes ?
Theorem: There are infinitely many primes (vô hạn số nguyên tố) !
Sàng Eratosthenes:
Goal: Tìm tất cả các số nguyên tố nhỏ hơn một số cho trước.
Example: List all primes that are not exceeding 50 ?
Idea:
—---> In the list {1,2, …, 50}, delete all integers that are divisible by 2, 3, 5, 7, we will obtain all primes that are not exceeding 50 !
1.3 The integers: số nguyên
Definition:
Notation: gcd(a,b)
Notation: lcd(a,b)
Example: Find
1.3 The integers: số nguyên
How to find gcd(a,b) ?
First way: Use Euclid algorithm
Lemma: If a= q.b +r, then gcd(a,b)= gcd(b,r)
Now, suppose a ≥ b > 0, we successively apply the division algorithm: Put r₀=a, r₁=b
Then : gcd(a,b)= gcd(r₀, r₁ )= gcd (r₁, r₂) =.... = gcd( rₙ, 0)= rₙ !
1.3 The integers: số nguyên
How to find gcd(a,b) ?
First way: Use Euclid algorithm
1.3 The integers: số nguyên
How to find gcd(a,b) ?
Example: Use Euclid algorithm to find gcd(252, 198)
1.3 The integers: số nguyên
How to find gcd(a,b), lcm(a,b) ?
Second way: Use prime factorizations of a and b
Suppose:
Then
(nhân tử chung với số mũ nhỏ nhất )
and
(nhân tử chung và riêng với số mũ lớn nhất)
1.3 The integers: số nguyên
How to find gcd(a,b), lcm(a,b) ?
Second way: Use prime factorizations of a and b
Example:
b) Use prime factorization to find gcd(120, 500) and lcm(120,500)
c) What is the relation between gcd(120,500) and lcm(120,500) ?
1.3 The integers: số nguyên
Theorem: Let a, b>0 be positive integers. Then
gcd(a,b) . lcm(a,b)= a.b
—---> lcm(a,b)= (a.b) / gcd(a,b)
Definition: (số nguyên tố cùng nhau)
Notation: (a,b)= 1
Example:
1.4 Integers and Algorithms
Definition: (base b expansion)
Let b, n ∈ ℕ, b>1. Every n>0 can be written uniquely as
Where k ∈ ℕ ; 0 ≤ a₀, a₁, …, aₖ < b and aₖ ≠ 0.
Notation: (a₀a₁ … aₖ)_b : the base b expansion of n (initially in base 10)
Example: Convert into decimal expansion
1.4 Integers and Algorithms
Base conversion: (b, n ∈ ℕ*, b >1 )
Example:
(765)₈= ()_4
1.4 Integers and Algorithms
Base conversion: (b, n ∈ ℕ*, b >1 )
1.4 Integers and Algorithms
Addition:
Example: Add (101 001)₂ + (110 101)₂
1.4 Integers and Algorithms
Addition:
Procedure add( a= (aₙ₋₁aₙ₋₂…a₀)₂,b= (bₙ₋₁bₙ₋₂…b₀)₂ )
c:= 0
for j=0 to n-1
d:= ⌊(aⱼ+bⱼ+c) / 2⌋ // next carry of the next step
sⱼ:= aⱼ+bⱼ+c-2d // result bit
c:= d // update new carry to the next step
s=c // rightmost bit of the result
return (s₀sₙ₋₁sₙ₋…s₀) // the binary expansion of the sum is (ssₙ₋₁…s₀)₂
Question: Estimate the Big-O of the number of additions of bits required in the algorithm ?
1.4 Integers and Algorithms
Multiplication:
Example: Compute the product of (1010)₂ and (1100)₂
1.4 Integers and Algorithms
Multiplication:
Procedure multiply( a= (a₋₁a₋₂…a₀)₂,b= (b₋₁b₋₂…b₀)₂ )
for j:=0 to n-1
if bⱼ=1 then cⱼ:= a shifted j place
else cⱼ:= 0 // c₀, c₁,...,c₋₁ are partial products
p:= 0
for j:=0 to n-1
p:=p+ cⱼ
return p // p is the value of a.b
Question: Estimate the Big-O of the number of additions of bits and shifts of bits required in the algorithm?
1.4 Integers and Algorithms
Question: Estimate the Big-O of the number of additions of bits and shifts of bits required in the algorithm?
1.4 Integers and Algorithms
Question: Estimate the Big-O of the number of additions of bits and shifts of bits required in the algorithm?
Answer: O(n²)
1.4 Integers and Algorithms
Computing div and mod :
procedure division ( a: integer; d: positive integer)
q:=0
r:= |a|
while r ≥ d {quotient= number of times of successive subtractions}
begin
r:= r-d
q := q+1
end
If a<0 and r>0 then {updating remainder when a<0}
begin
r:= d-r
q := -(q+1)
end
{ q = a div d is the quotient, r=a mod d is the remainder}
1.4 Integers and Algorithms
Modular Exponentiation
then compute each term (mod m) in the product by successively compute
b mod m ; b² mod m, (b²)² mod m ; ((b²)²)² mod m ,...
1.4 Integers and Algorithms
Procedure mod_ex ( b: integer, n=(ak-1ak-2…a1a0)2, m: positive integer)
x:=1
power := b mod m
for i:=0 to k-1
begin
if ai=1 then x:= (x.power) mod m
power := (power.power) mod m
end
{ x equals bn mod m }
1.4 Integers and Algorithms
Modular Exponentiation
For example:
So, to compute 3¹³ mod 7, we compute
3⁰ mod 7= 1 ; 3² mod 7 = 2 ; mod 7= 2² (mod 7) = 4 ;
mod 7= 4² mod 7= 2
So 3¹³ mod 7= 1. 4. 2 (mod 7)= 1.
Here, since a₁=0, 3² mod 7 (=2) does not counted into the remainder (but still needed to compute mod 7 !
1.4 Integers and Algorithms
Modular Exponentiation
First, convert 262 to binary base:
262= (1 0000 0110)₂
Second: set x:=1, p:= 240 mod 14= 2
Next, compute step by step by successively taking square of p (mod 14) and x= x.p (mod 14) whenever aⱼ=1 :
So: 240²⁶² (mod 14)= 2
1 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | aⱼ |
2 | 8 | 8 | 8 | 8 | 8 | 8 | 4 | 1 | x=1 |
| 2 | 4 | 2 | 4 | 2 | 4 | 2 | 4 | p= 2 |
1.4 Integers and Algorithms
Modular Exponentiation
1.4 Integers and Algorithms
Euclidean Algorithm
procedure gcd(a, b) { a,b: positive integers}
x:=a
y:=b
while y ≠ 0
begin
r := x mod y
x:=y
y:= r
end {gcd(a,b) is x}
PROGRAMMING PROJECT
PROGRAMMING PROJECT
PROGRAMMING PROJECT
Write two different algorithms to compute bⁿ (mod m), then compare their complexity.