Algorithm and Number Theory
Algorithm and Number Theory
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 !
IT201 - Discrete Structures 11
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
x, or is zero if x is not found}
IT201 - Discrete Structures 12
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
IT201 - Discrete Structures 38
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.
IT201 - Discrete Structures 39
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.
IT201 - Discrete Structures 40
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
IT201 - Discrete Structures 41
Least Common Multiples
Using prime factorizations:
a = p1a1 p2a2 … pnan , b = p1b1 p2b2 … pnbn ,
where p1 < p2 < … < pn and ai, bi N for 1 i n
Example:
a = 60 = 22 31 51
b = 54 = 21 33 50
lcm(a, b) = 22 33 51 = 4275 = 540
IT201 - Discrete Structures 42
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
Examples:
9 mod 4 = 1
9 mod 3 = 0
9 mod 10 = 9
-13 mod 4 = 3
In other words:
a b (mod m) if and only if a mod m = b mod m.