CH 3
CH 3
Appetizers!
Algorithms
search
interval
a c d f g h j l m o p r s u v x z
center
element
search
interval
a c d f g h j l m o p r s u v x z
center
element
search
interval
a c d f g h j l m o p r s u v x z
center
element
search
interval
a c d f g h j l m o p r s u v x z
center
element
search
interval
a c d f g h j l m o p r s u v x z
center
element
found !
Fall 2011 CSC 213 - Discrete Structures 10
Algorithm Examples
procedure binary_search(x: integer; a1, a2, …,
an : integers)
i := 1 {i is left endpoint of search interval}
j := n {j is right endpoint of search interval}
while (i < j)
begin
m := (i + j)/2
if x > am then i := m + 1
else j := m
end
if x = ai then location := i
else location := 0
{location is the subscript of the term that
equals
Fall 2011
x, or is zero if x is not found}
CSC 213 - Discrete Structures 11
Complexity
f(x) is O(x2).
Number Theory
• if a | b and a | c, then a | (b + c)
Example: 3 | 6 and 3 | 9, so 3 | 15.
• if a | b and b | c, then a | c
Example: 4 | 8 and 8 | 24, so 4 | 24.
17 = 53 + 2.
• 17 is the dividend,
• 5 is the divisor,
• 3 is called the quotient, and
• 2 is called the remainder.
Example:
a = 60 22 31 51
=
b = 54 21 33 50
=
gcd(a, b) 21 31 50 = 6
=Fall 2011 CSC 213 - Discrete Structures 37
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.
Fall 2011 CSC 213 - Discrete Structures 38
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) =CSC1.
Fall 2011 213 - Discrete Structures 39
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
= Fall 2011 CSC 213 - Discrete Structures 40
Least Common Multiples
Using prime factorizations:
Example:
a = 60 22 31 51
=
b = 54 21 33 50
=
lcm(a, b) 22 33 51 = 4275 = 540
=Fall 2011 CSC 213 - Discrete Structures 41
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: ab gcd(a,b)lcm(a,
= b)
Fall 2011 CSC 213 - Discrete Structures 42
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
=
Fall 2011 CSC 213 - Discrete Structures 43
Congruences
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.
In other words:
a b (mod m) if and only if a mod m = b mod
m.
Fall 2011 CSC 213 - Discrete Structures 44
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,
…}
So we divide 14 by 7:
14 = 72 + 0
We find that 7 | 14, and thus gcd(14, 7) = 7.
1 11 carry
Example: 7583
+ 4932
12515
1 1 carry
Binary expansions: (1011)2
+ (1010)2
(10101 )2
Therefore, s = a + b = (11001)2.