Discrete structures 2 lec 1
Discrete structures 2 lec 1
Mwangi H. (Ph.D.)
CS Yr 3.1
Department of Computing
J.K.U.A.T.
Definition 1.1
Theorem 1.1
Proof
Notice that we always re-write a division with remainder relation in terms
of positive integers. Indeed if a < 0 then b = aq + r can be re-written as
b = a′ q ′ + r with a′ = −a and q ′ = −q. The case of b < 0 is similar.
Therefore, we only have to consider the case where a, b, q, r are all
non-negative integers. Now, we prove the existence of quotient and
remainder. We will show q = ⌊ ba ⌋. By definition we have that 0 ≤ r < a
and b = aq + r Therefore
aq ≤ aq + r < a(q + 1)
aq ≤ b < a(q + r )
b
q ≤ <q+1
a
b
q=⌊ ⌋
a
Proof Cont’d
Since a, b, q, r are integers with a ̸= 0 and 0 ≤ r < b, r can be uniquely be
determined from a, b, and q Next we prove that the quotient and
remainder are unique. Let b = aq1 + r 1 = aq2 + r 2 such that a, b, q, r are
non-negative integers and 0 ≤ r 1 < a and 0 ≤ r 2 < a. Proceed by
contradiction and assume r 1 ̸= r 2. W.L.O.G. assume r 2 > r 1. Then
aq1 − aq2 = r 2 − r 1
a(q1 − q2) = r 2 − r 1
Proof Cont’d
Yet, the multiples of a are 0, ±a, ±2a, ±3a, . . .. Since r 2 − r 1 < a. it must
be that r 2 − r 1 = 0 and hence r 2 = r 1. A contradiction. Since r 2 = r 1, it
follows that q2 = q1 from a ̸= 0 and
aq1 + r 1 = aq2 + r 2
aq1 = aq2
a(q1 − q2) = 0 =⇒ q1 − q2 = 0
Definition 1.2
Theorem 1.3
Proof.
If a ≡ b mod m then, by definition m|a − b. Hence, there exist an integer
k such a − b = km. Rearranging, a = b + km. Conversely, if there exists
and integer k such that a = b + km, then we of course have km = a − b
and thus m|a − b and a ≡ b mod m
Note that mod and mod are not exactly the same thing. Although highly
related, one is a function a mod b = r is a function taking a and b as
arguments and returning the remainder r of a divided by b while the other
is a binary relation. It defines an equivalence relation.
Theorem 1.4
Proof.
By a previous theorem, we have that the congruences implies existence of
integers k, l and the equations b = a + km and c = d + lm results. From
these equations we have b + d = (a + km) + (c + lm) = (a + c) + (k + l)m.
Therefore b + d ≡ a + c mod m
m = 5 The set of all equivalence classes modulo m are {0̄, 1̄, 2̄, 3̄, 4̄}
This set is similar to Z5 = {0, 1, 2, 3, 4}. The set of congruence
classes modulo 5 and Z5 are essentially the same. In other words they
are isomorphic
0 =m X
1 =m X + 1
2 =m X + 2
.
.
.
m − 1 =m X + (m + 1)
Congruence - Exercises
1 Proof the following theorem
Theorem 1.5
Congruence - Exercise
Examples
7 is prime because only 1 and 7 divide 7. On the other hand, 3 divides
9 and so 9 is not prime
Recall that another way of describing divisors is as factors. Therefore,
an equivalent definition of a prime number p is one whose factors are
only 1 and p
The fundamental theorem of arithmetic strengthens the idea of
primarity to a prime factorization
Proposition 2.1
Proof.
If n is a positive composite then there exists two integers a, b greater than
√ √
1 such that n = ab. Certainly a ≤ n or b ≤ n. If n is a perfect square
√ √
then a = b = n. Otherwise, one of a or b must be smaller than n.
Multiples of 2
Multiples of 2
2 3 4 5 6 7 8 9 10
10 11 12 13 14 15 16 17 18 19 20
20 21 22 23 24 25 26 27 28 29 30
30 31 32 33 34 35 36 37 38 39 40
40 41 42 43 44 45 46 47 48 49 50
100
Multiples of 3
Multiples of 3
2 3 - 5 - 7 - 9 -
- 11 - 13 - 15 - 17 - 19 -
- 21 - 23 - 25 - 27 - 29 -
- 31 - 33 - 35 - 37 - 39 -
- 41 - 43 - 45 - 47 - 49 -
Multiples of 5
Multiples of 5
2 3 - 5 - 7 - - -
- 11 - 13 - - - 17 - 19 -
- - - 23 - 25 - - 29 -
- 31 - - - 35 - 37 - - -
- 41 - 43 - 45 - 47 - 49 -
Multiples of 7
Multiples of 7
2 3 - 5 - 7 - - -
- 11 - 13 - - - 17 - 19 -
- - - 23 - - - - 29 -
- 31 - - - - - 37 - - -
- 41 - 43 - - - 47 - 49 -
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 -
Theorem 2.2
Probablilistic Method
For any +integer n, we can deduce that n is not prime if we can find
a number a such that an−1 ̸≡ 1 mod n. Such an a is called Fermat
witness
Fermat probability test leads to a probablilistic method to determine
if a number is prime.
Probabilistic algorithm is one which produces the correct result ”with
high probability” but not necessarily all the time.
It tries to find Fermat witness. If after a certain number of attempts
it cannot find such witness, then the algorithm terminates and
assumes that the number is prime.
The assumption is what makes the algorithm probabilistic. The
number of times to perform the test is not clear.
d e f randomPrime ( n ) :
w h i l e ( True ) :
p=r a n d i n t ( 2 ∗ ∗ ( n −1) , 2∗∗( n ) −1):
i f isPrime (p ,128)
return p ;
#p r i n t a random 32− b i t p r i m e
p r i n t ( randomPrime ( 1 6 ) )
o u t p u t : 49103
Prime Conjectures
1470 = 2.3.5.72
350 = 2.52 .7
The GCD of 1470 and 350 is thus 2min(1,1) .5min(1,2) .7min(2,1) =
70
The LCM of two positive integers a and b is the smallest positive integer
that is divisible by both a and b
1470 = 2.3.5.72
350 = 2.52 .7
The LCM of 1470 and 350 is thus 2max(1,1) .5max(1,2) .7max(2,1) = 7350
Theorem 2.4
a · b = gcd(a, b) · lcm(a, b)
Lemma 2.1
r0 = 287
r1 = 91
r2 = 14
r3 = 7
r4 = 0
Algorithm gcd(a,b)
Algorithm gcd(a,b)
d e f gcd ( a , b ) :
Input: a, b ∈ Z+ , a > b x = a
Output x, the GCD of a and b y = b
1. x ←− a p r i n t ( ” r 0 : %d” % x )
2. y ←− b p r i n t ( ” r 1 : %d” % y )
3. while y ̸= 0 do i = 2;
4. r ←− x mod y w h i l e y != 0 :
r = x % y
5. x ←− y
p r i n t ( ” r%d : %d” % ( i , r ) )
6. y ←− r i += 1
7. end while x = y
8. return x y = r
return x
p r i n t ( ”GCD( 1 5 2 1 5 2 , 1 5 4 7 0 0 ) =
% gcd ( 1 5 2 1 5 2 , 1 5 4 7 0 0 ) )
r0: 152152
r1: 154700
r2: 152152
r3: 2548
r4: 1820
r5: 728
r6: 364
r7: 0
gcd(6, 14) = 2
gcd(6, 14) = (−2).6 + 1.14 = 2
Consequences of Bezout
Lemma 2.2
Proof.
Since a and b are relatively prime then gcd(a, b) = 1
By Hypothesis, assume a | bc
By Bezout theorem, then sa + tb = gcd(a, b) = 1
Multiplying both side c
csa + ctb = c
Since
a | bc, a | ctb
That is there exists q such that ctb = qa
csa + ctb = c
csa + qa = c
a(cs + q) = c
Hence, a | c as required
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 55 / 117
Greatest Common Divisors and Primes Euclidean Algorithm
Consequences of Bezout
Lemma 2.3
Theorem 2.6
Proof.
By Hypothesis ac ≡ bc mod m hence m | ac − bc = c(a − b). And from the previous
lemma, m must therefore divide c or (a − b). Since, by assumption, gcd(c, m) = 1, it
must be that m | a − b. That is a ≡ b mod m
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 56 / 117
Greatest Common Divisors and Primes Euclidean Algorithm
Exercises
Exercises
Example 3.1
2x ≡ 1 mod 5
Modular Inverses
Modular Inverses
Example 3.2
3a ≡ 1 mod 7
15 ≡ 1 mod 7
→a≡5 mod 7
Since in the above example, 5 is the modular inverse 3 mod 7, any number in the
congruence class of 5 modulo is a multiplicative inverse.
Modulo inverses can be used to solve linear congruences. Let a′ be the inverse of a
modulo m, then we have the following relations
ax ≡ b mod m
′ ′
a ax ≡ a bx mod m
x ≡ a′ b mod m
2·0≡0 mod 6
2·1≡2 mod 6
2·2≡4 mod 6
2·3≡0 mod 6
2·4≡2 mod 6
2·5≡4 mod 6
But when do they not exist? This is because there is no identity and also
gcd(2, 6) ̸= 1 and also the fact that, 2 is called the a zero-divisor for arith-
metic modulo m
Theorem 3.1
If a and m are relatively prime integers with m > 1, then there exists
a unique modular inverse x of a mod m satisfying 0 < x < m
Proof
Proof.
First we proof the existence of x.
By Hypothesis we have gcd(a, m) = 1. Therefore by Bezout theorem there exists
integers s and t such that sa + tm = 1
We therefore have:
1 − sa = tm
m | (1 − sa)
1 ≡ sa mod m
Examples
1 = 13 − 4 · 3
951 = 6 · 151 + 45
151 = 3 · 45 + 16 1 = 13 − 4(16 − 1 · 13) = −4 · 16 + 5 · 13
45 = 2 · 16 + 13 1 = −4 · 16 + 5(45 − 2 · 16) = 5 · 45 − 14 · 16
16 = 1 · 13 + 3
1 = 5 · 45 − 14(151 − 3 · 45) = −14 · 151 + 47 · 45
13 = 4 · 3 + 1
3=3·1+0 1 = −14 · 151 + 47(951 − 6 · 151) = −296 · 151 + 47 · 951
Example
Solution
Solution
67 = 1 · 57 + 10
57 = 5 · 10 + 7
10 = 1 · 7 + 3
7=2·3+1
3=3·1+0
Solution
Solution
1=7−2·3
1 = 7 − 2(10 − 1 · 7) = −2 · 10 + 3 · 7
1 = −2 · 10 + 3(57 − 5 · 10) = 3 · 57 − 17 · 10
Solution
Solution
57x ≡ 13 mod 67
20(57x) ≡ 20 · 13 mod 67
1 · x ≡ 260 mod 67
x ≡ 59 mod 67
Proof.
Since m and n are co-prime then by bezout theorem, there exists integers s, t such that;
sm + tn = 1
Consider uniqueness. Let x = y and x = z be two solutions of this system. Then, y and
z must give the same remainder when divided by m or n. ∴ m | y − z and n | y − z.
Since m and n are co-prime, it follows that m · n | y − z ∴ y ≡ z mod m · n
CRT - Examples
Since 3 and 5 are co-prime, CRT states that there exists a unique
solution modulo 15
∴ exactly one solution x with 0 ≤ x < 15
Applying the Euclidean algorithm to find s, t such that 3s + 5t = 1
Or, by inspection 3(2) + 5(−1) = 1 ∴ s = 2, t = −1
Thus x = 2(3s) + 1(5t) = 2(3)(2) + 1(5)(−1) = 7 ≡ 7 mod 15
CRT - Examples
Lemma 3.1
Proof.
By Hypothesis m | x and n | x
∴, ∃ qm , qn such that x = mqm and x = nqn hence mqm = nqn
Since m and n are co-primes, ∃ s, t such that sm + tn = 1
∴, smqm + tnqm = qm and combining with our hypothesis gives
snqn + tnqm = qm and thus
n(sqn + tqm ) = qm
From x = mqm we get
x = mn(sqn + tqm ) Hence
mn | x
Excises Cont’d
3 Solve the following linear congruences. Give the unique positive
solution which is less than the modulus.
i x ≡ 12 mod 7
ii 2x ≡ 12 mod 7
iii 13x ≡ 15 mod 23
4 Solve the following system of linear congruences for 0 ≤ x < 77
3x ≡ 2 mod 11
4x ≡ 6 mod 7
However, there are many ways we could represent and understand numbers
Other than using roman numeral and or series of dots to encode the
things being counted it is much more convenient to use a positional
number system
In the roman numerals I mean 1 v means 5 · · · , the position of the
symbol does not change its value
Decimal representation of number is positional. Putting two 1
symbols together to form 11 means something differenct from II in
Roman numerals.
Decimal System of numbers has radix or base 10.
Position of a digit in a number represents a multiple of a certain
power of 10
Radix-r Representations
The positional number system can be represented with any choice of radix.
In modtern times base 10 has been used as the main representation because humans
have 10 fingers.
Historically, different groups used different bases. The Mayans used base 20 (10 toes
and 10 fingers). The Babylonians used base 60. This is why time and angles are
measured in groups of 60 seconds, 60 minutes.
Given some radix r , we can construct a number system using r as the radix or base.
This results from the following theorem.
Theorem 4.1
Let r be a positive integer greater than 1. Any positive integer n can be expressed
uniquely in the form:
n = ak r k + ak−1 r k−1 + · · · + a2 r 2 + a1 r + a0
where k is a non-negative integer, aj (0 ≤ j ≤ k) belongs to the set
{0, 1, . . . , r − 1} and ak ̸= 0
Radix-r Representations
Radix-r Representations
(1010)2 = (1 · 23 ) + (0 · 22 ) + (1 · 21 ) + (0 · 20 ) = 8 + 2 = 10
(10101)2 = (1 · 24 ) + (0 · 23 ) + (1 · 22 ) + (0 · 21 ) + (1 · 20 ) = 16 + 4 + 1 = 21
15
X
(1111111111111111)2 = 2i = 216 − 1 = 65535
i=0
Radix-r Representations
= 770339
Converting to radix-r
Radix-r representations are an expansion of a number using powers of as the
base. This suggests that converting from decimal to a radix- r
representation can be performed by (repeated) division.
Let r be some radix and n be some integer number to convert to radix- r .
By Euclidean division we have:
n = q0 r + a0
with 0 ≤ a0 < r
Notice that a0 is thus a digit in the radix-r number system. In fact, a0 is the
first digit (counting from the right) of the radix-r representation or n.
Continue dividing q0 by r
q0 = q1 r + a1
with 0 ≤ a1 < r Again, a1 is a digit in the radix-r number system and a1 is
the second digit of the radix-r representation of n.
This continues until a qk is 0, using the successive remainders as the digits
of the radix-r representation of n
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 88 / 117
Integer Representations Converting to radix-r
Converting to radix-r
Converting to radix-r
93752 = 5859(16) + 8
5859 = 366(16) + 3
366 = 22(16) + 14
22 = 1(16) + 6
1 = 0(16) + 1
Converting to radix-r
How does this work?
↔ n ≡ a1 r + a0 mod r 2
..
.
Recall that binary, octal, and hexadecimal numbers all have roots in
electronics and computing.
Each of these representations have their own merits in computer
science.
Therefore very useful to be able to convert between these
representations. Doing such a conversion is very easy.
The binary system uses one digit to represent each bit in a computer
system.
The octal system, with digits 0 through 7 represents three binary
digits at once: 7 = (111)2 , 6 = (110)2 , etc.
Therefore, we can easily convert from binary to octal by grouping
binary digits into threes and then converting each group to its
corresponding decimal (octal) digit.
→1 5 2 4 2 5 1 2
→ (15242512)8
→3 5 4 5 4 A
→ (35454A)16
Binary Arithmetic
Regardles of which number systems, arithmetic like addition,
multiplication, division, etc. always give the same result.
Whether in binary, hexadecimal, octal, or decimal, the sum of two
numbers is still its sum.
The only thing that changes is the way we write down the numbers
being added and their sum.
Doing addition and subtraction in the binary number system is not so
different from doing it in the decimal system.
The key is to understand how we add individual digits, just like in
“long addition” in the decimal system.
There are three possibilities for adding single bits: both are 0, both
are 1, or one is 1 and the other is 0
(0)2 + (0)2 = (0)2
(1)2 + (0)2 = (1)2
(1)2 + (1)2 = (10)2
henry.mwangi@jkuat.ac.ke (SCIT) Computer Science February 29, 2024 94 / 117
Integer Representations Binary Arithmetic
There are four cases for adding 3 bits together: there are zero 1s, there is
one 1, there are two 1s, there are three 1s.
Using this basic addition of three bits and the ideas of “carrying” digits,
we can compute the addition of any two binary numbers.
Binary Multiplication
Binary multiplication can be derived from binary addition with a simple observation. Let
m = (ak ak−1 · · · a2 a1 )2 and n = (bℓ bℓ−1 · · · b2 b1 )2
Then
m · n = m · (bℓ 2ℓ + bℓ−1 2ℓ−1 + · · · + b2 22 + b1 2 + b0 )
Notice
m2ℓ = 2ℓ (ak 2k + ak−1 2k−1 + · · · + a2 22 + a1 2 + a0 )
Binary Multiplication
Binary Shift
When multiplying a binary number by a power of 2, the result is simply a shift of digits to the
left, with the corresponding number of 0 digits added on the right.
(1101011)2 · 25 = 110101100000
p = (11001)2 23 + (11001)2 21
= (11001000)2 + (110010)2
Exercises
Exercises