Discrete Mathematics: Unit-I-Chapter-2 Algorithms-Integers-Matrices
Discrete Mathematics: Unit-I-Chapter-2 Algorithms-Integers-Matrices
Unit-I-Chapter-2
Algorithms-Integers-Matrices
Algorithms
In general, an algorithm just means a defined procedure for performing and completing
some sort of task (aka a sequence of steps)
Algorithms are the foundation of computer programming.
Example: Describe an algorithm for finding the maximum (largest) value in a finite
sequence of integers
Perform the following steps
Set up temporary maximum equal to the first integer in the sequence
Compare the next integer in the sequence to the temporary maximum, and if it is larger than the
temporary maximum, set the temporary maximum equal to this
Repeat the previous step if there are more integers in the sequence
Stop when there are no integers left in the sequence. The temporary maximum at this point is the
largest integer in the sequence.
30
Binary Search Time Complexity
procedure binary_search (x:integer, a1, a2, …, an: distinct integers,
sorted smallest to largest)
i := 1
j := n
while i < j begin t1
m := ⎣(i + j)/2⎦
if x > am then i := m + 1 else j := m t2
end
if x = ai then location := i else location := 0 t3
return location
31
Binary Search Time Complexity Analysis
Suppose that n is a power of 2, i.e., ∃k: n = 2k.
Original range from i = 1 to j = n contains n items.
Each iteration: Size j - i + 1 of range is cut in half.
Size decreases as 2k, 2k-1, 2k-2,…
Loop terminates when size of range is 1 = 20 (i = j).
Therefore, the number of iterations is: k = log2n
t (n)= t1+t2+t3=(k+1)+k+1=2k+2=2log2n +2= Θ(log2n)
32
Bubble Sort Time Complexity Analysis
procedure bubble_sort (a1, a2, …, an: real numbers with n ≥ 2)
for i := 1 to n – 1
for j := 1 to n – i
if aj > aj+1 then interchange aj and aj+1
{a1, a2, …, an is in increasing order}
Worst-case complexity in terms of the number of comparisons: Θ(n2)
33
Insertion Sort Time Complexity Analysis
procedure insertion_sort (a1, a2, …, an: real numbers; n ≥ 2)
for j := 2 to n
begin
i := 1
while aj > ai
i := i + 1
m := aj
for k := 0 to j – i – 1
aj-k := aj-k-1
ai := m
end {a1, a2, …, an are sorted in increasing order}
Worst-case complexity in terms of the number of comparisons: Θ(n2)
34
Common Terminology for the Complexity of Algorithms
35
The Integers and Division
Number theory: It is the branch of mathematics which involves
integers and their properties
Number theory is vital in many important algorithms today (hash
functions, cryptography, digital signatures,…).
If a and b are integers with a≠0, we say that a divides b if there is an
integer c such 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 denotes a divides b. We write a ∤ b when a does not
divide b
BE III Sem-DM - CSE - 2020-21 - MJCET 36
Theorem and corollary
Theorem: Let a, b, and c be integers, then
If a | b and a | c, then a | (b+c)
If a | b, then a | bc for all integers c
If a | b and b | c, then a | c
Corollary: If a, b, and c are integers such that a | b and a | c,
then a | mb+nc whenever m and n are integers
43
The Division algorithm
Find the Quotient and remainder when
i)101 is divided by 11 ii) -11 is divided by 3
101 = 11·9 + 2 (dividend: 101, divisor: 11)
101 div 11 = 9 101 mod 11 = 2
–11 = 3·(–4) + 1 or –11 = 3·(–3) – 2 ? (dividend: –11, divisor: 3)
–11 div 3 = –4 –11 mod 3 = 1
(quotient: –4, remainder: 1)
Note that the remainder must not be negative.
Find the Quotient and remainder when
i)19 is divided by 7 ii) -111 is divided by 11
44
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 (GCD) of a and b, often
denoted as gcd(a,b).Ex: gcd(24,36)=12.
The integers a and b are called relative prime or coprime if their GCD is 1
Ex: gcd(10, 17)=1, gcd(10, 21)=1, gcd(10,24)=2
The integers a1, a2, …, an are pairwise relatively prime if gcd(ai, aj)=1 whenever
1≤i<j≤n
Ex: 10,17,21 are pairwise relatively prime. Since gcd(10,17)=1, gcd(10,21)=1 and
gcd(17,21)=1
Check whether the following integers are pairwise relatively prime
i)11,15,19 ii)14,15,21
45
GCD Shortcut
If the prime factorizations are written as
a=p1a p2a …pna and b=p1b p2b …pnb , then the GCD is given by:
1 2 n 1 2 n
Ex: gcd(120,500)
Prime factorization of 120= 23.3.5 and 500=22.53
gcd(120,500)=2min(3,2)3min(1,0)5min(1,3) =223051 =20
Ex: gcd(84,96)
84 = 22·3·7 96 = 25·3
gcd(84,96) = 2min(2,5)3min(1,1)7min(1,0) = 22·31·70 = 12.
Find the GCD of 37.53.73, 211.35.59
46
Least Common Multiple
Least common multiple of the positive integers a and b is the
smallest positive integer that is divisible by both a and b, denoted as
lcm(a,b). E.g. lcm(6,10) = 30
Example: lcm(24,36) = ?
Positive multiples of 24: 24, 48, 72, 96, 120, 144,…
Positive multiples of 36: 36, 72, 108, 144,…
Positive common multiples: 72, 144,…
The smallest one of these is 72.
47
LCM Shortcut
If the prime factorizations are written as
a=p1a p2a …pna and b=p1b p2b …pnb , then the LCM is given by
1 2 n 1 2 n
Example: lcm(84,96)
a = 84 = 2·2·3·7 = 22·31·71
b = 96 = 2·2·2·2·2·3 = 25·31·70
lcm(84,96) = 2max(2,5)·3max(1,1)·7max(1,0) = 25·31·71 =32·3·7= 672
Find the LCM of 37.53.73, 211.35.59
48
GCD and LCM
Theorem: Let a and b be positive integers. Then
ab = gcd(a,b) × lcm(a,b)
Example
a = 84 = 22·31·71
b = 96 = 25·31·70
ab = (22·31·71) ・ (25·31·70) = 22 ・ 31 ・ 70 ・ 25 ・ 31 ・ 71
= 2min(2,5) ・ 3min(1,1) ・ 7min(1,0) ・ 2max(2,5) ・ 3max(1,1) ・ 7max(1,0)
= gcd(a,b) × lcm(a,b)
49
Modular arithmetic
If a and b are integers and m is a positive integer, then 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
Note: this is a different use of “≡” than the meaning “equivalent” or “is defined
as” used before in logic.
If a and b are not congruent modulo m, we write a ≢b (mod m)
Ex: Determine whether 17 is congruent to 5 modulo 6, and whether 24
is congruent to14 modulo 6
Since 6 divides 17-5=12, we say 17≡5 (mod 6)
24-14=10 is not divisible by 6, and thus 24≢14 (mod 6)
50
Useful Congruence Theorems
Let a and b be integers, m be a positive integer.
Then a≡b (mod m) if and only if a mod m = b mod m
Let m be a positive integer. The integer a and b are congruent
modulo m if and only if there is an integer k such that a=b+km
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)
7 ≡ 2 (mod 5) and 11 ≡ 1 (mod 5), so
18=7+11 ≡ 2+1=3 (mod 5)
77=7∙11 ≡2∙1=2(mod 5)
51
Applications of Congruences
Hashing Functions: We want to quickly store and retrieve records in memory locations.
A hashing function takes a data item to be stored or retrieved and computes the first
choice for a location for the item.
h(k) = k mod m
A hashing function h assigns memory location h(k) to the record that has k as its key.
h(064212848) = 064212848 mod 111 = 14
h(037149212) = 037149212 mod 111 = 65
h(107405723) = 107405723 mod 111 = 14 ⇒ collision!
Find the first unoccupied memory location after the occupied memory.
In this case, assign memory location 15.
If collision occurs infrequently, and if when one does occur it is resolved quickly, then
hashing provides a very fast method of storing and retrieving data.
52
Applications of Congruences
Pseudorandom Numbers: Numbers that are generated deterministically, but that
appear random for all practical purposes.
The most commonly used procedure for generating pseudorandom numbers is the
linear congruential method (uses the mod operator)
Requires four natural numbers:
The modulus m, multiplier a, increment c, and seed x0.
where 2≤a<m, 0 ≤c<m, and 0≤x0<m
Generates a sequence of pseudorandom numbers {xn} with 0 ≤ xn < m for all n, by
xn+1=(axn+c) mod m
Tends to work best when a, c, m are prime, or at least relatively prime.
If c = 0, the method is called a pure multiplicative generator.
53
Applications of Congruences
Pseudorandom Numbers Example: Let m=9, a=7, c=4, x0=3. Then the sequence of
pseudorandom numbers generated are
x1=7x0+4 mod 9=(21+4) mod 9=25 mod 9 = 7
x2=7x1+4 mod 9=(49+4) mod 9=53 mod 9 = 8
x3=7x2+4 mod 9=(56+4) mod 9=60 mod 9 = 6 xn+1=(axn+c) mod m
x4=7x3+4 mod 9=(42+4) mod 9=46 mod 9 = 1
x5=7x4+4 mod 9=(7+4) mod 9=11 mod 9 = 2
x6=7x5+4 mod 9=(14+4) mod 9=18 mod 9 = 0
x7=7x6+4 mod 9=(0+4) mod 9=4 mod 9 = 4
x8=7x7+4 mod 9=(28+4) mod 9=32 mod 9 =5
x9=7x8+4 mod 9=(35+4) mod 9=11 mod 9 = 3
A sequence of 3, 7, 8, 6, 1, 2, 0, 4, 5, 3, 7, 8, 6, 1, 2, 0, 4, 5, 3 , …
Contains 9 different numbers before repeating
54
Applications of Congruences
Cryptology: The study of secret messages
Encryption is the process of making a message secret. Decryption is the
process of determining the original message from the encrypted message.
Some simple early codes include Caesar’s cipher:
Assign an integer from 0 to 25 to each letter based on its position in the alphabet.
Caesar's encryption method: f(p) = (p + 3) mod 26
“MEET YOU IN THE PARK”
12 4 419 24 14 20 8 13 19 7 4 15 0 17 10
15 7 7 22 1 17 23 11 16 22 10 7 18 3 20 13
“PHHW BRX LQ WKH SDUN”
Caesar's decryption method: f –1(p) = (p – 3) mod 26
55
Applications of Congruences
Cryptology: Caesar's encryption method does not provide a high
level of security
A slightly better approach: f(p) = (ap + b) mod 26
Example:
What letter replaces the letter K when the function f(p)= (7p + 3) mod 26
is used for encryption?
10 represents K
f(10) = (7×10 + 3) mod 26 = 73 mod 26 = 21
21 represents V
Therefore, K is replaced by V in the encrypted message
56
Integers and Algorithms
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 non negative integer less than b, and ak!=0.
This representation is called the base b expansion of n
The base b expansion of n is denoted by (akak-1…..a1a0)b
For instance, (245)8=2·82+4·8+5=165
Particular Bases of Interest
Base b = 10 (decimal): 10 digits: 0,1,2,3,4,5,6,7,8,9.
Base b = 2 (binary): 2 digits: 0,1. (“Bits”=“binary digits.”)
Base b = 8 (octal): 8 digits: 0,1,2,3,4,5,6,7.
Base b = 16 (hexadecimal): 16 digits: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
57
Base Conversions
Example 1: Decimal expansion of the integer with binary expansion
(101011111)2?
(101011111)2
= 1 ・ 28 + 0 ・ 27 + 1 ・ 26 + 0 ・ 25 + 1 ・ 24 + 1 ・ 23 + 1 ・ 22 +
1・2+1
= (351)10
Example 2: Decimal expansion of the integer with hexadecimal
expansion (2AE0B)16?
(2AE0B)16 = 2 ・ 164 + 10 ・ 163 + 14 ・ 162 + 0 ・ 16 + 11
= (175627)10
58
Converting to Base b
To convert any integer n to any base b > 1:
To find the value of the rightmost (lowestorder) digit, simply compute n mod
b.
Now, replace n with the quotient.
Repeat above two steps to find subsequent digits, until n is gone (= 0).
Example: Find the base 8, i.e. octal, expansion of (12345) 10
12345 = 8 ・ 1543 + 1
1543 = 8 ・ 192 + 7
192 = 8 ・ 24 + 0
24 = 8 ・ 3 + 0
3=8・0+3
Therefore, (12345)10 = (30071)8
59
Binary<-> Hexadecimal
Hexadecimal expansion of (11 1110 1011 1100)2
∴ (11 1110 1011 1100)2 = (3EBC)16
Binary expansion of (A8D)16
(A)16 = (1010)2, (8)16 = (1000)2, (D)16 = (1101)2
∴ (A8D)16 = (1010 1000 1101)2
60
Addition of Binary Numbers
procedure add(an−1…a0, bn-1…b0: binary representations of non-negative integers a, b)
carry := 0
for j := 0 to n−1
begin
d := ⎣(aj+bj+carry )/2 ⎦
sj := aj+bj+carry-2d
carry := d
end
sn := carry
return sn … s0: binary representation of integers
61
Addition of Binary Numbers
Ex: Add a=(1110)2 and b= (1011)2
Sol: a0+b0= 0+1=0·2+1, so that c0=0 and s0=1. Then since
a1+b1+c0=1+1+0=1·2+0, it follows that c1=1 and s1=0. Continuing
a2+b2+c1=1+0+1=1·2+0, so that c2=1 and s2=0. Finally, since
a3+b3+c3=1+1+1=1·2+1, it follows that c3=1 and s3=1. This means that s4=c3=1.
∴ s=a+b=(11001)2
62
Multiplication of Binary Numbers
ab = a(b0 ・ 20 + b1 ・ 21 + ・・・ + bn-1 ・ 2n-1)
= a(b0 ・ 20 ) + a(b1 ・ 21 ) + ・・・ + a(bn-1 ・ 2n-1)
procedure multiply(an−1…a0, bn−1…b0: binary representations of positive integers a,b)
for j := 0 to n−1
begin
if bj = 1 then cj:= a shifted j places
else cj:=0
end
{c0,c1,…cn-1 are the partial products}
p=0
for j := 0 to n−1
p:=p+cj
{p is the value of ab}
63
Multiplication of Binary Numbers
ab = a(b0 ・ 20 + b1 ・ 21 + ・・・ + bn-1 ・ 2n-1)
= a(b0 ・ 20 ) + a(b1 ・ 21 ) + ・・・ + a(bn-1 ・ 2n-1)
Ex: Find the product of a=(110)2 and b=(101) 2
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 shows that ab= (11110)2
110 a
x 101 b
110
000
110
111 10
64
Modular Exponentiation
Problem: Given large integers b (base), n (exponent), and m (modulus), efficiently compute bn mod m.
Note that bn itself may be completely infeasible to compute and store directly.
E.g. if n is a 1,000-bit number, then bn itself will have far more digits than there are atoms in the
universe!
Yet, this is a type of calculation that is commonly required in modern cryptographic algorithms!
Note that: bnn=(b k 1 ) k-1x(b
2k-1 n 2k-2)n x… x(b20)n
k-2 0
b n
b k 1 2 n1 2 n0
We can compute b to various powers of 2 by repeated squaring.
Then multiply them into the partial product, or not, depending on whether the corresponding ni bit is 1.
Crucially, we can do the mod m operations as we go along, because of the various identity laws of
modular arithmetic.
All the numbers stay small.
To compute bn , first find the values of b mod m, b2 mod m, …,b2k-1 mod m
Next multiplies those terms with b2j mod m where aj=1, finding the remainder of the product when
divided by m after each multiplication.
65
Modular Exponentiation
To compute 311
11=(1011)2 ,So 311=38 32 31 .
First compute 32=9,
and then 34=92=81,
and 38=(34)2=(81)2=6561,
So 311=6561*9*3=177147
66
Modular Exponentiation
procedure modular exponentiation (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}
67
Modular Exponentiation
Compute 3644 mod 645
First note that 644=(1010000100)2
At the beginning, x=1, power=3 mod 645 = 3
i=0, a0=0, x=1, power=32 mod 645=9
i=1, a1=0, x=1, power=92 mod 645=81
i=2, a2=1, x=1*81 mod 645=81, power=812 mod 645=6561 mod 645=111
i=3, a3=0, x=81, power=1112 mod 645=12321 mod 645=66
i=4, a4=0, x=81, power=662 mod 645=4356 mod 645=486
i=5, a5=0, x=81, power=4862 mod 645=236196 mod 645=126
i=6, a6=0, x=81, power=1262 mod 645=15876 mod 645=396
i=7, a7=1, x=(81*396) mod 645=471, power=3962 mod 645=156816 mod 645=81
i=8, a8=0, x=471, power=812 mod 645=6561mod 645=111
i=9, a9=1, x=(471*111) mod 645=36
69
Euclidean algorithm
Lemma: Let a=bq+r, where a, b, q, and r are integers. Then gcd(a,b)=gcd(b,r)
Suppose a and b are positive integers, a≥b. Let r0=a and r1=b, we successively
apply the division algorithm
r0 r1 q 1 r 2 , 0 r 2 r1
r1 r 2 q 2 r3 , 0 r3 r 2
...
rn 2 rn 1 q n 1 rn , 0 rn rn 1
rn 1 rn q n
gcd( a , b ) gcd( r0 , r1 ) gcd( r1 , r 2 ) gcd( r n 2 , r n 1 )
gcd( r n 1 , r n ) gcd( r n , 0 ) r n
Hence, the gcd is the last nonzero remainder in the sequence of divisions
70
Euclidean algorithm
procedure gcd(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}
71
Euclidean algorithm
Find the GCD of 414 and 662 using the Euclidean algorithm
662=414 ∙ 1+248
414=248 ∙ 1+166
248=166 ∙ 1+82
166=82 ∙ 2 + 2
82=2 ∙ 41
gcd(414,662)=2 (the last nonzero remainder)
Use Euclidean algorithm to find the
i) gcd(123,277) ii) gcd(1001,1331)
72
Applications of Number Theory
Miscellaneous Results
Theorem 1: If a and b are positive integers, then there exist integers s and t such
that gcd(a,b)=sa + tb
∀a,b ∈ Z+: ∃s,t ∈Z: gcd(a,b) = sa + tb
Lemma 1: If a, b and c are positive integers such that gcd(a,b)=1 and a|bc, then a|c.
∀a,b,c ∈ Z+: gcd(a,b)=1 ∧ a | bc → a|c
Lemma 2: If p is a prime and p|a1a2… an where each ai is an integer, then p| ai for
some i.
If p is prime and p|a1a2… an (integers ai) then ∃i: p| ai.
Theorem 2: Let m be a positive integer and let a, b and c be integers.
If ac ≡ bc (mod m) and gcd(c,m)=1, then a ≡ b (mod m).
73
Theorem 1: Example
Theorem 1: If a and b are positive integers, then there exist integers s and t such
that gcd(a,b)=sa + tb
∀a,b ∈ Z+: ∃s,t ∈Z: gcd(a,b) = sa + tb
Example: Express gcd(252, 198) = 18 as a linear combination of 252 and 198.
252 = 1 ⋅ 198 + 54
198 = 3 ⋅ 54 + 36
54 = 1 ⋅ 36 + 18
36 = 2 ⋅ 18
18 = 54 – 1 ⋅ 36 = 54 – 1 ⋅ (198 – 3 ⋅ 54) = 4 ⋅ 54 – 1 ⋅ 198
= 4 ⋅ (252 – 1 ⋅ 198) – 1 ⋅ 198
= 4 ⋅ 252 – 5 ⋅ 198
Therefore, gcd(252, 198) = 18 = 4 ⋅ 252 – 5 ⋅ 198
74
Theorem 2: Example
Theorem 2: Let m be a positive integer and let a, b and c be integers.
If ac ≡ bc (mod m) and gcd(c,m)=1, then a ≡ b (mod m).
Examples
20 ≡ 8 (mod 3) i.e. 5 ⋅ 4 ≡ 2 ⋅ 4 (mod 3)
Since gcd(4, 3) = 1, 5 ≡ 2 (mod 3)
14 ≡ 8 (mod 6) i.e. 7 ⋅ 2 ≡4 ⋅2(mod 6) but 7 ≡ 4 (mod 6) (As gcd(2,6) ≠ 1)
75
Linear Congruences
A congruence of the form ax ≡ b (mod m) is called a linear congruence. (m∈Z+,
a,b∈Z,and x: variable)
To solve the congruence is to find the x’s that satisfy it.
An inverse of a, modulo m is any integer a-1 such that a-1a ≡ 1 (mod m).
If we can find such an a-1, notice that we can then solve ax ≡ b (mod m) by multiplying
through by it, giving a-1ax ≡ a-1b (mod m), thus
1 ・ x ≡ a-1b (mod m), thus x ≡ a-1b (mod m).
76
Theorem 3
Theorem 3: If gcd(a,m)=1 (i.e. a and m are relatively prime) and m > 1,
then a has an inverse a-1 unique modulo m.
Proof: By theorem 1, since gcd(a,m)=1, ∃s,t: sa + tm = 1, so sa + tm ≡ 1 (mod
m).
Since tm ≡ 0 (mod m), it follows that sa ≡ 1 (mod m).
Thus s is an inverse of a (mod m).
Find an inverse of 3 modulo 7
Since gcd(3, 7) = 1, by Theorem 3 there exists an inverse of 3 modulo 7.
7 = 2 ・ 3 + 1 by Euclidean algo.
From the above equation, –2 ・ 3 + 1 ・ 7 = 1
Therefore, –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.)
Find an inverse of i) 4 modulo 9 ii) 19 modulo 141
77
Example
What are the solutions of the linear congruence 3x ≡ 4 (mod 7)?
–2 is an inverse of 3 modulo 7 (previous slide)
Multiply both side by –2: –2 ・ 3x ≡ –2 ・ 4 (mod 7)
–6 ・ x ≡ x ≡ –8 ≡ 6 (mod 7)
Therefore, the solutions to the congruence are the integers x such that x ≡ 6 (mod 7), i.e. 6, 13, 20,
27,… and –1, –8, –15,…
e.g. 3 ・ 13 = 39 ≡ 4 (mod 7)
What are the solutions of the linear congruence 4x ≡ 5(mod 9)?
78
Chinese Remainder Theorem
Theorem: (Chinese remainder theorem)Let m1,…,mn > 0 be pairwise relatively prime
and ai ,…,an arbitrary integers.
Then the equations system x ≡ ai (mod mi) (for i=1,..,n) has a unique solution modulo m =
m1 m2 ・・・ mn.
Proof:
Let Mk = m/mk. (Thus gcd(mk , Mk)=1.)
So by Theorem 3, ∃yk=Mk such that Mk yk≡1 (mod mk).
Now let x = Σi aiyiMi = a1M1y1 + a2M2 y2+ ・・・ + anMnyn.
Since mj|Mk for j≠k, Mj≡0 (mod mk), so
x≡ ak Mk yk ≡ak (mod mk). Thus, the congruences hold.
79
Chinese Remainder Theorem
Ex: There are certain things whose numbers 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 can be translated as x ≡ 2(mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7).
Soln: Here k=3, m1=3,m2=5,m3=7
a1=2, a2=3,a3=2. To solve the system of congruences.
m=m1⋅m2⋅m3=3⋅5⋅7=105.
M1=m/m1=105/3=35, M2=m/m2=105/5=21, M3=m/m3=105/7=15
M1=35 modulo 3, M2=21 modulo 5, M3=15 modulo 7
As Mkyk=1(mod mk)=> Mk=y-1 (mod mk)
y1=M1-1 mod m1, y2= M2-1 mod m2, y3= M3-1 mod m3
y1=2, as 2 is an inverse of M1= 35 modulo 3, since 35 ≡ 2 (mod 3);
y2=1, as 1 is an inverse of M2= 21 modulo 5, since 21 ≡ 1(mod 5); and
y3=1, as 1 is an inverse of M3= 15 modulo 7, since 15 ≡ 1(mod 7).
The solution 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)
23 is the smallest positive integer that is a simultaneous solution.
80
Chinese Remainder Theorem
Example: Which integer leaves a remainder of 1 when divided by 2 and also remainder of 1 when divided by 3.
81
Pseudoprimes & Carmichael Numbers
Ancient Chinese mathematicians noticed that whenever n is prime, 2n−1≡1 (mod n).
Some also claimed that the converse was true.
However, it turns out that the converse is not true!
If 2n−1≡1 (mod n), it doesn’t follow that n is prime.
For example, 341=11·31, but 2340≡1 (mod 341).
Composites n with this property are called pseudoprimes.
More generally, if bn−1≡1 (mod n) and n is composite, then n is called a pseudoprime to the
base b.
Carmichael Numbers : These are sort of the “ultimate pseudoprimes.”
A Carmichael number is a composite integer n such that bn−1≡1 (mod n) for all b relatively prime to n.
The smallest few are 561, 1105, 1729, 2465, 2821, 6601, 8911, 10585, 15841, 29341.
82
Fermat’s Little Theorem
Fermat generalized the ancient observation that 2 p−1≡1 (mod p) for primes p to the
following more general theorem:
Theorem: (Fermat’s Little Theorem.)
If p is prime and a is an integer not divisible by p, then ap−1≡1 (mod p).
Furthermore, for every integer a we have ap ≡ a (mod p).
Example (Exponentiation MOD a Prime)
Find 2301 mod 5:
By FLT, 24 ≡ 1 (mod 5). Hence,
2300 = (24)75 ≡ 1 (mod 5).
Therefore, 2301=(2300) ・ 2 ≡ 1 ・ 2 (mod 5)≡2 (mod 5)
83
Public Key Cryptography
In private key cryptosystems, the same secret “key” string is used to both encode and
decode messages.
This raises the problem of how to securely communicate the key strings.
In public key cryptosystems, there are two complementary keys instead.
One key decrypts the messages that the other one encrypts.
This means that one key (the public key) can be made public, while the other (the private
key) can be kept secret from everyone.
Messages to the owner can be encrypted by anyone using the public key, but can only be
decrypted by the owner using the private key.
Or, the owner can encrypt a message with their private key, and then anyone can decrypt it, and
know that only the owner could have encrypted it. This is the basis of digital signature systems.
The most famous public-key cryptosystem is RSA.
It is based entirely on number theory
84
Public Key Cryptography
Rivest-Shamir-Adleman (RSA)
Choose a pair p, q of large random prime numbers with about the same number of
bits
Let n = pq
Choose exponent e that is relatively prime to (p−1)(q−1) and 1 < e <(p−1)(q−1)
Compute d, the inverse of e modulo (p−1)(q−1).
The public key consists of: n, and e.
The private key consists of: n, and d.
85
Public Key Cryptography
RSA Encryption
To encrypt a message encoded as an integer:
Translate each letter into an integer and group them to form larger integers, each representing a
block of letters. Each block is encrypted using the mapping
C = Me mod n.
Example: RSA encryption of the message STOP with p = 43, q = 59, and e = 13
n = 43 x 59 = 2537
gcd(e, (p–1)(q–1)) = gcd(13, 42 ・ 58) = 1
STOP -> 1819 1415
C = Me mod n.= M13 mod 2537
181913 mod 2537 = 2081; 141513 mod 2537 = 2182
Encrypted message: 2081 2182
86
Public Key Cryptography
RSA Decryption
To decrypt the encoded message C,
Compute M = Cd mod n
Recall that d is an inverse of e modulo (p−1)(q−1).
Ex: RSA decryption of the message 0981 0461 encrypted with p = 43, q = 59, and e = 13
n = 43 x 59 = 2537; d =e-1 modulo((p-1)(q-1))
ed=1 mod((p-1)(q-1)) =>13d= 1 modulo (42 ・ 58) => 13d= 1 modulo 2436=> d= 937
0981937 mod 2537 = 0704
0461937 mod 2537 = 1115
Decrypted message: 0704 1115
Translation back to English letters: HELP
87
Matrices
A matrix is a rectangular array of objects (usually numbers).
An m × n (“m by n”) matrix has exactly m horizontal rows, and n vertical columns.
Plural of matrix = matrices
An n × n matrix is called a square matrix
Row and Column Order
The rows in a matrix are usually indexed 1 to m from top to bottom.
The columns are usually indexed 1 to n from left to right.
Elements are indexed by row, then by column.
88
Matrix Equality
Two matrices A and B are considered equal iff they have the same number of rows, the
same number of columns, and all their corresponding elements are equal.
Matrix Sums: The sum A + B of two matrices A, B (which must have the same number of rows, and
the same number of columns) is the matrix (also with the same shape) given by adding corresponding
elements of A and B. ->A + B = [aij + bij ]
89
Matrix Products
For an m × k matrix A and a k × n matrix B, the product AB is the m × n matrix:
I.e., the element of AB indexed (i, j) is given by the vector dot product of the i-th row of A
and the j-th column of B.
Ex:
90
Matrix Product Example
Because A is a 2×3 matrix and B is a 2×2 matrix, the product AB is not defined.
Matrix multiplication is not commutative!
A: m × n matrix and B: r × s matrix
AB is defined when n = r
BA is defined when s = m
When both AB and BA are defined, generally they are not the same size unless m = n = r = s
If both AB and BA are defined and are the same size, then A and B must be square and of the
same size
Even when A and B are both n × n matrices, AB and BA are not necessarily equal
91
Matrix Multiplication Algorithm
procedure matmul(matrices A: m × k, B: k × n)
for i := 1 to m
for j := 1 to n
begin
cij := 0
for q := 1 to k
cij := cij + aiqbqj
end
{C = [cij] is the product of A and B}
92
Identity Matrices
The identity matrix of order n is the n x n matrix, In, is the rank-n square matrix with 1’s
along the upper-left to lower-right diagonal, and 0’s everywhere else.
93
Matrix Inverses
For some (but not all) square matrices A, there exists a unique multiplicative inverse A−1 of
A, a matrix such that A−1 A = In.
If the inverse exists, it is unique, and A−1 A = A A−1 .
Powers of Matrices:
If A is an n × n square matrix and p ≥ 0, then:
Ap = AAA···A (and A0 = In)
p times
94
Matrix Transposition
If A = [aij] is an m × n matrix, the transpose of A (often written At or AT) is the n × m matrix
given by
At = B = [bij] = [aji] (1 ≤ i ≤ n,1 ≤ j ≤ m)
95
Symmetric Matrices
A square matrix A is symmetric iff A = At. I.e., ∀i, j ≤ n: aij = aji .
Which of the below matrices is symmetric?
96
Zero-One Matrices
Useful for representing other structures.
E.g., relations, directed graphs
All elements of a zero-one matrix are either 0 or 1.
E.g., representing False & True respectively.
The join of A, B (both m × n zero-one matrices):
A ∨ B = [aij ∨ bij]
The meet of A, B:
A ∧ B = [aij ∧ bij] = [aij bij]
97
Boolean Products
Let A = [aij] be an m × k zero-one matrix and B = [bij] be a k × n zero-one matrix,
The Boolean product of A and B is like normal matrix multiplication, but using ∨ instead
of +, and ∧ instead of × in the row-column “vector dot product”:
98
Boolean Powers
For a square zero-one matrix A, and any k ≥ 0, the k-th Boolean power of A is simply the
Boolean product of k copies of A.
A[k] = A⊙A ⊙ ・・ ⊙ A
k times
A[0] = In
99
Exercises 22 11 0 4
Find AB if A= 33
and B=
22
0
11
4
33
11 4
4 11 22
100