enough mathematical appetizers!
DESCRIPTION
Enough Mathematical Appetizers!. Let us look at something more interesting: Algorithms. Algorithms. What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. - PowerPoint PPT PresentationTRANSCRIPT
Fall 2002 CMSC 203 - Discrete Structures 1
Enough Mathematical Enough Mathematical Appetizers! Appetizers!
Let us look at something more interesting:Let us look at something more interesting:
AlgorithmsAlgorithms
Fall 2002 CMSC 203 - Discrete Structures 2
Algorithms Algorithms
What is an algorithm?What is an algorithm?
An algorithm is a finite set of precise An algorithm is a finite set of precise instructions for performing a computation or instructions for performing a computation or for solving a problem.for solving a problem.
This is a rather vague definition. You will get to This is a rather vague definition. You will get to know a more precise and mathematically know a more precise and mathematically useful definition when you attend CMSC441. useful definition when you attend CMSC441.
But this one is good enough for now…But this one is good enough for now…
Fall 2002 CMSC 203 - Discrete Structures 3
Algorithms Algorithms
Properties of algorithms:Properties of algorithms:
• InputInput from a specified set, from a specified set,• OutputOutput from a specified set (solution), from a specified set (solution),• DefinitenessDefiniteness of every step in the computation, of every step in the computation,• CorrectnessCorrectness of output for every possible input, of output for every possible input,• FinitenessFiniteness of the number of calculation steps, of the number of calculation steps,• EffectivenessEffectiveness of each calculation step and of each calculation step and• GeneralityGenerality for a class of problems. for a class of problems.
Fall 2002 CMSC 203 - Discrete Structures 4
Algorithm ExamplesAlgorithm Examples
We will use a pseudocode to specify algorithms, We will use a pseudocode to specify algorithms, which slightly reminds us of Basic and Pascal.which slightly reminds us of Basic and Pascal.
Example:Example: an algorithm that finds the maximum an algorithm that finds the maximum element in a finite sequenceelement in a finite sequence
procedureprocedure max(a max(a11, a, a22, …, a, …, ann: integers): integers)max := amax := a11
forfor i := 2 i := 2 toto n nifif max < a max < aii thenthen max := a max := aii
{max is the largest element}{max is the largest element}
Fall 2002 CMSC 203 - Discrete Structures 5
Algorithm ExamplesAlgorithm Examples
Another example:Another example: a linear search algorithm, a linear search algorithm, that is, an algorithm that linearly searches a that is, an algorithm that linearly searches a sequence for a particular element.sequence for a particular element.
procedureprocedure linear_search(x: integer; a linear_search(x: integer; a11, a, a22, …, , …, aann: : integers) integers)i := 1i := 1while while (i (i n and x n and x a aii))
i := i + 1i := i + 1ifif i i n n thenthen location := i location := ielseelse location := 0 location := 0{location is the subscript of the term that {location is the subscript of the term that equals x, or is zero if x is not found}equals x, or is zero if x is not found}
Fall 2002 CMSC 203 - Discrete Structures 6
Algorithm ExamplesAlgorithm Examples
If the terms in a sequence are ordered, a If the terms in a sequence are ordered, a binary search algorithm is more efficient than binary search algorithm is more efficient than linear search.linear search.
The binary search algorithm iteratively The binary search algorithm iteratively restricts the relevant search interval until it restricts the relevant search interval until it closes in on the position of the element to be closes in on the position of the element to be located.located.
Fall 2002 CMSC 203 - Discrete Structures 7
Algorithm ExamplesAlgorithm Examples
a c d f g h j l m o p r s u v x za c d f g h j l m o p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Fall 2002 CMSC 203 - Discrete Structures 8
Algorithm ExamplesAlgorithm Examples
a c d f g h j l m a c d f g h j l m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Fall 2002 CMSC 203 - Discrete Structures 9
Algorithm ExamplesAlgorithm Examples
a c d f ga c d f g h j l m h j l m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Fall 2002 CMSC 203 - Discrete Structures 10
Algorithm ExamplesAlgorithm Examples
a c d f ga c d f g h j h j l ml m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
Fall 2002 CMSC 203 - Discrete Structures 11
Algorithm ExamplesAlgorithm Examples
a c d f ga c d f g h h j j l ml m o p r s u v x zo p r s u v x z
binary search for the letter ‘j’binary search for the letter ‘j’
center center elementelement
search search intervalinterval
found !found !
Fall 2002 CMSC 203 - Discrete Structures 12
Algorithm ExamplesAlgorithm Examplesprocedureprocedure binary_search(x: integer; a binary_search(x: integer; a11, a, a22, …, , …, aann: : integers) integers)i := 1 i := 1 {i is left endpoint of search interval}{i is left endpoint of search interval}j := n j := n {j is right endpoint of search interval}{j is right endpoint of search interval} while while (i < j)(i < j)beginbegin
m := m := (i + j)/2(i + j)/2ifif x > a x > amm thenthen i := m + 1 i := m + 1elseelse j := m j := m
endendifif x = a x = aii thenthen location := i location := ielseelse location := 0 location := 0{location is the subscript of the term that {location is the subscript of the term that equals x, or is zero if x is not found}equals x, or is zero if x is not found}
Fall 2002 CMSC 203 - Discrete Structures 13
ComplexityComplexity
In general, we are not so much interested in In general, we are not so much interested in the time and space complexity for small the time and space complexity for small inputs.inputs.
For example, while the difference in time For example, while the difference in time complexity between linear and binary search complexity between linear and binary search is meaningless for a sequence with n = 10, it is meaningless for a sequence with n = 10, it is gigantic for n = 2is gigantic for n = 23030..
Fall 2002 CMSC 203 - Discrete Structures 14
ComplexityComplexity
For example, let us assume two algorithms A For example, let us assume two algorithms A and B that solve the same class of problems.and B that solve the same class of problems.
The time complexity of A is 5,000n, the one The time complexity of A is 5,000n, the one for B is for B is 1.11.1nn for an input with n elements. for an input with n elements.
For n = 10, A requires 50,000 steps, but B For n = 10, A requires 50,000 steps, but B only 3, so B seems to be superior to A.only 3, so B seems to be superior to A.
For n = 1000, however, A requires 5,000,000 For n = 1000, however, A requires 5,000,000 steps, while B requires 2.5steps, while B requires 2.510104141 steps. steps.
Fall 2002 CMSC 203 - Discrete Structures 15
ComplexityComplexity
This means that algorithm B cannot be used This means that algorithm B cannot be used for large inputs, while algorithm A is still for large inputs, while algorithm A is still feasible.feasible.
So what is important is the So what is important is the growthgrowth of the of the complexity functions.complexity functions.
The growth of time and space complexity with The growth of time and space complexity with increasing input size n is a suitable measure increasing input size n is a suitable measure for the comparison of algorithms. for the comparison of algorithms.
Fall 2002 CMSC 203 - Discrete Structures 16
ComplexityComplexity
Comparison:Comparison: time complexity of algorithms A and B time complexity of algorithms A and B
Algorithm AAlgorithm A Algorithm BAlgorithm BInput SizeInput Size
nn
1010
100100
1,0001,000
1,000,0001,000,000
5,000n5,000n
50,00050,000
500,000500,000
5,000,0005,000,000
55101099
1.11.1nn33
2.52.510104141
13,78113,781
4.84.810104139241392
Fall 2002 CMSC 203 - Discrete Structures 17
The Growth of FunctionsThe Growth of Functions
The growth of functions is usually described The growth of functions is usually described using the using the big-O notationbig-O notation..
Definition:Definition: Let f and g be functions from the Let f and g be functions from the integers or the real numbers to the real integers or the real numbers to the real numbers.numbers.We say that f(x) is O(g(x)) if there are We say that f(x) is O(g(x)) if there are constants C and k such thatconstants C and k such that
|f(x)| |f(x)| C|g(x)| C|g(x)|
whenever x > k.whenever x > k.
Fall 2002 CMSC 203 - Discrete Structures 18
The Growth of FunctionsThe Growth of Functions
When we analyze the growth of When we analyze the growth of complexity complexity functionsfunctions, f(x) and g(x) are always positive. , f(x) and g(x) are always positive.
Therefore, we can simplify the big-O Therefore, we can simplify the big-O requirement torequirement to
f(x) f(x) C Cg(x) whenever x > k.g(x) whenever x > k.
If we want to show that f(x) is O(g(x)), we only If we want to show that f(x) is O(g(x)), we only need to find need to find oneone pair (C, k) (which is never pair (C, k) (which is never unique).unique).
Fall 2002 CMSC 203 - Discrete Structures 19
The Growth of FunctionsThe Growth of FunctionsThe idea behind the big-O notation is to The idea behind the big-O notation is to establish an establish an upper boundaryupper boundary for the growth for the growth of a function f(x) for of a function f(x) for large large x.x.
This boundary is specified by a function g(x) This boundary is specified by a function g(x) that is usually much that is usually much simplersimpler than f(x). than f(x).
We accept the constant C in the requirementWe accept the constant C in the requirement
f(x) f(x) C Cg(x) whenever x > k,g(x) whenever x > k,
because because C does not grow with x.C does not grow with x.
We are only interested in large x, so it is OK ifWe are only interested in large x, so it is OK iff(x) > Cf(x) > Cg(x) for x g(x) for x k. k.
Fall 2002 CMSC 203 - Discrete Structures 20
The Growth of FunctionsThe Growth of Functions
Example:Example:
Show that f(x) = xShow that f(x) = x22 + 2x + 1 is O(x + 2x + 1 is O(x22).).
For x > 1 we have:For x > 1 we have:
xx22 + 2x + 1 + 2x + 1 x x22 + 2x + 2x22 + x + x22
xx22 + 2x + 1 + 2x + 1 4x 4x22
Therefore, for C = 4 and k = 1:Therefore, for C = 4 and k = 1:
f(x) f(x) Cx Cx22 whenever x > k. whenever x > k.
f(x) is O(xf(x) is O(x22).).
Fall 2002 CMSC 203 - Discrete Structures 21
The Growth of FunctionsThe Growth of Functions
Question: If f(x) is O(xQuestion: If f(x) is O(x22), is it also O(x), is it also O(x33)?)?
Yes.Yes. x x33 grows faster than x grows faster than x22, so x, so x33 grows also grows also faster than f(x).faster than f(x).
Therefore, we always have to find the Therefore, we always have to find the smallestsmallest simple function g(x) for which f(x) is simple function g(x) for which f(x) is O(g(x)). O(g(x)).
Fall 2002 CMSC 203 - Discrete Structures 22
The Growth of FunctionsThe Growth of Functions
““Popular” functions g(n) arePopular” functions g(n) aren log n, 1, 2n log n, 1, 2nn, n, n22, n!, n, n, n!, n, n33, log n, log n
Listed from slowest to fastest growth:Listed from slowest to fastest growth:• 11• log nlog n• nn• n log nn log n• nn22
• nn33
• 22nn
• n!n!
Fall 2002 CMSC 203 - Discrete Structures 23
The Growth of FunctionsThe Growth of Functions
A problem that can be solved with polynomial A problem that can be solved with polynomial worst-case complexity is called worst-case complexity is called tractabletractable..
Problems of higher complexity are called Problems of higher complexity are called intractable.intractable.
Problems that no algorithm can solve are Problems that no algorithm can solve are called called unsolvableunsolvable..
You will find out more about this in CMSC441.You will find out more about this in CMSC441.
Fall 2002 CMSC 203 - Discrete Structures 24
Useful Rules for Big-OUseful Rules for Big-O
For any For any polynomialpolynomial f(x) = a f(x) = annxxnn + a + an-1n-1xxn-1n-1 + … + + … + aa00, where a, where a00, a, a11, …, a, …, ann are real numbers, are real numbers,f(x) is O(xf(x) is O(xnn).).
If fIf f11(x) is O(g(x) is O(g11(x)) and f(x)) and f22(x) is O(g(x) is O(g22(x)), then (x)), then (f(f11 + f + f22)(x) is O(max(g)(x) is O(max(g11(x), g(x), g22(x)))(x)))
If fIf f11(x) is O(g(x)) and f(x) is O(g(x)) and f22(x) is O(g(x)), then(x) is O(g(x)), then(f(f11 + f + f22)(x) is O(g(x)).)(x) is O(g(x)).
If fIf f11(x) is O(g(x) is O(g11(x)) and f(x)) and f22(x) is O(g(x) is O(g22(x)), then (x)), then
(f(f11ff22)(x) is O(g)(x) is O(g11(x) g(x) g22(x)).(x)).
Fall 2002 CMSC 203 - Discrete Structures 25
Complexity ExamplesComplexity Examples
What does the following algorithm compute?What does the following algorithm compute?
procedureprocedure who_knows(a who_knows(a11, a, a22, …, a, …, ann: integers): integers)m := 0m := 0forfor i := 1 to n-1 i := 1 to n-1
forfor j := i + 1 to n j := i + 1 to nifif |a |aii – a – ajj| > m | > m thenthen m := |a m := |aii – a – ajj||
{m is the maximum difference between any two {m is the maximum difference between any two numbers in the input sequence}numbers in the input sequence}Comparisons: n-1 + n-2 + n-3 + … + 1Comparisons: n-1 + n-2 + n-3 + … + 1 = (n – 1)n/2 = 0.5n= (n – 1)n/2 = 0.5n22 – 0.5n – 0.5n
Time complexity is O(nTime complexity is O(n22).).
Fall 2002 CMSC 203 - Discrete Structures 26
Complexity ExamplesComplexity Examples
Another algorithm solving the same problem:Another algorithm solving the same problem:
procedureprocedure max_diff(a max_diff(a11, a, a22, …, a, …, ann: integers): integers)min := a1min := a1max := a1max := a1forfor i := 2 to n i := 2 to n
ifif a aii < min < min thenthen min := a min := aii
elseelse if a if aii > max > max thenthen max := a max := aii
m := max - minm := max - min
Comparisons: 2n - 2Comparisons: 2n - 2
Time complexity is O(n).Time complexity is O(n).
Fall 2002 CMSC 203 - Discrete Structures 27
Let us get into…Let us get into…
Number TheoryNumber Theory
Fall 2002 CMSC 203 - Discrete Structures 28
Introduction to Number TheoryIntroduction to Number Theory
Number theory is about Number theory is about integersintegers and their and their properties.properties.
We will start with the basic principles ofWe will start with the basic principles of
• divisibility,divisibility,• greatest common divisors,greatest common divisors,• least common multiples, andleast common multiples, and• modular arithmeticmodular arithmetic
and look at some relevant algorithms. and look at some relevant algorithms.
Fall 2002 CMSC 203 - Discrete Structures 29
DivisionDivision
If a and b are integers with a If a and b are integers with a 0, we say that 0, we say that a a dividesdivides b if there is an integer c so that b = b if there is an integer c so that b = ac.ac.
When a divides b we say that a is a When a divides b we say that a is a factorfactor of b of b and that b is a and that b is a multiplemultiple of a. of a.
The notation The notation a | ba | b means that a divides b. means that a divides b.
We write We write a a χχ b b when a does not divide b when a does not divide b(see book for correct symbol).(see book for correct symbol).
Fall 2002 CMSC 203 - Discrete Structures 30
Divisibility TheoremsDivisibility Theorems
For integers a, b, and c it is true thatFor integers a, b, and c it is true that
• if a | b and a | c, then a | (b + c)if a | b and a | c, then a | (b + c) Example:Example: 3 | 6 and 3 | 9, so 3 | 15. 3 | 6 and 3 | 9, so 3 | 15.
• if a | b, then a | bc for all integers cif a | b, then a | bc for all integers c Example:Example: 5 | 10, so 5 | 20, 5 | 30, 5 | 40, … 5 | 10, so 5 | 20, 5 | 30, 5 | 40, …
• if a | b and b | c, then a | cif a | b and b | c, then a | c Example:Example: 4 | 8 and 8 | 24, so 4 | 24. 4 | 8 and 8 | 24, so 4 | 24.
Fall 2002 CMSC 203 - Discrete Structures 31
PrimesPrimes
A positive integer p greater than 1 is called A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and prime if the only positive factors of p are 1 and p.p.Note: 1 is not a primeNote: 1 is not a prime
A positive integer that is greater than 1 and is A positive integer that is greater than 1 and is not prime is called composite.not prime is called composite.
The fundamental theorem of arithmetic:The fundamental theorem of arithmetic:
Every positive integer can be written Every positive integer can be written uniquelyuniquely as the as the product of primesproduct of primes, where the prime , where the prime factors are written in order of increasing size.factors are written in order of increasing size.
Fall 2002 CMSC 203 - Discrete Structures 32
PrimesPrimes
Examples:Examples:
3·53·5
48 =48 =
17 =17 =
100 100 ==512 512 ==515 515 ==28 =28 =
15 =15 =
2·2·2·2·3 = 22·2·2·2·3 = 244·3·3
1717
2·2·5·5 = 22·2·5·5 = 222·5·522
2·2·2·2·2·2·2·2·2 = 22·2·2·2·2·2·2·2·2 = 299
5·1035·103
2·2·72·2·7
Fall 2002 CMSC 203 - Discrete Structures 33
PrimesPrimes
If n is a composite integer, then n has a prime If n is a composite integer, then n has a prime divisor less than or equal .divisor less than or equal .
This is easy to see: if n is a composite integer, This is easy to see: if n is a composite integer, it must have at least two prime divisors. Let it must have at least two prime divisors. Let the largest two be pthe largest two be p11 and p and p22. Then p. Then p11pp22 <= n. <= n.
pp11 and p and p22 cannot both be greater than cannot both be greater than , because then p, because then p11pp22 > n. > n.
n
n
Fall 2002 CMSC 203 - Discrete Structures 34
The Division AlgorithmThe Division Algorithm
Let Let aa be an integer and be an integer and dd a positive integer. a positive integer.Then there are unique integers Then there are unique integers qq and and rr, with , with 0 0 r < d r < d, such that , such that a = dq + ra = dq + r..
In the above equation, In the above equation, • dd is called the divisor, is called the divisor, • aa is called the dividend, is called the dividend, • qq is called the quotient, and is called the quotient, and • rr is called the remainder. is called the remainder.
Fall 2002 CMSC 203 - Discrete Structures 35
The Division AlgorithmThe Division Algorithm
Example:Example:
When we divide 17 by 5, we haveWhen we divide 17 by 5, we have
17 = 517 = 53 + 2.3 + 2.
• 17 is the dividend,17 is the dividend,• 5 is the divisor,5 is the divisor,• 3 is called the quotient, and3 is called the quotient, and• 2 is called the remainder.2 is called the remainder.
Fall 2002 CMSC 203 - Discrete Structures 36
The Division AlgorithmThe Division Algorithm
Another example:Another example:
What happens when we divide -11 by 3 ?What happens when we divide -11 by 3 ?
Note that the remainder cannot be negative.Note that the remainder cannot be negative.
-11 = 3-11 = 3(-4) + 1.(-4) + 1.
• -11 is the dividend,-11 is the dividend,• 3 is the divisor,3 is the divisor,• -4 is called the quotient, and-4 is called the quotient, and• 1 is called the remainder.1 is called the remainder.
Fall 2002 CMSC 203 - Discrete Structures 37
Greatest Common DivisorsGreatest Common DivisorsLet a and b be integers, not both zero.Let a and b be integers, not both zero.The largest integer d such that d | a and d | b is The largest integer d such that d | a and d | b is called the called the greatest common divisorgreatest common divisor of a and b. of a and b.The greatest common divisor of a and b is denoted The greatest common divisor of a and b is denoted by gcd(a, b).by gcd(a, b).
Example 1:Example 1: What is gcd(48, 72) ? What is gcd(48, 72) ?The positive common divisors of 48 and 72 are The positive common divisors of 48 and 72 are 1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 24. 1, 2, 3, 4, 6, 8, 12, 16, and 24, so gcd(48, 72) = 24.
Example 2:Example 2: What is gcd(19, 72) ? What is gcd(19, 72) ?The only positive common divisor of 19 and 72 isThe only positive common divisor of 19 and 72 is1, so gcd(19, 72) = 1. 1, so gcd(19, 72) = 1.
Fall 2002 CMSC 203 - Discrete Structures 38
Greatest Common DivisorsGreatest Common Divisors
Using prime factorizations:Using prime factorizations:
a = pa = p11aa1 1 p p22
aa2 2 … p… pnnaan n , b = p, b = p11
bb1 1 p p22bb2 2 … p… pnn
bbn n ,,
where pwhere p11 < p < p22 < … < p < … < pnn and a and aii, b, bii NN for 1 for 1 i i n n
gcd(a, b) = pgcd(a, b) = p11min(amin(a11, b, b1 1 )) p p22
min(amin(a22, b, b2 2 )) … p… pnnmin(amin(ann, b, bn n ))
Example:Example:
a = 60 a = 60 = =
2222 3 311 5 511
b = 54 b = 54 = =
2211 3 333 5 500
gcd(a, b) gcd(a, b) = =
2211 3 311 5 50 0 = 6 = 6
Fall 2002 CMSC 203 - Discrete Structures 39
Relatively Prime IntegersRelatively Prime IntegersDefinition:Definition:
Two integers a and b are Two integers a and b are relatively primerelatively prime if if gcd(a, b) = 1.gcd(a, b) = 1.
Examples:Examples:
Are 15 and 28 relatively prime?Are 15 and 28 relatively prime?Yes, gcd(15, 28) = 1.Yes, gcd(15, 28) = 1.Are 55 and 28 relatively prime?Are 55 and 28 relatively prime?Yes, gcd(55, 28) = 1.Yes, gcd(55, 28) = 1.Are 35 and 28 relatively prime?Are 35 and 28 relatively prime?No, gcd(35, 28) = 7.No, gcd(35, 28) = 7.
Fall 2002 CMSC 203 - Discrete Structures 40
Relatively Prime IntegersRelatively Prime Integers
Definition:Definition:
The integers aThe integers a11, a, a22, …, a, …, ann are are pairwise pairwise
relatively primerelatively prime if gcd(a if gcd(aii, a, ajj) = 1 whenever 1 ) = 1 whenever 1
i < j i < j n. n.
Examples:Examples:
Are 15, 17, and 27 pairwise relatively prime?Are 15, 17, and 27 pairwise relatively prime?No, because gcd(15, 27) = 3.No, because gcd(15, 27) = 3.
Are 15, 17, and 28 pairwise relatively prime?Are 15, 17, and 28 pairwise relatively prime?Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 Yes, because gcd(15, 17) = 1, gcd(15, 28) = 1 and gcd(17, 28) = 1.and gcd(17, 28) = 1.
Fall 2002 CMSC 203 - Discrete Structures 41
Least Common MultiplesLeast Common MultiplesDefinition:Definition:
The The least common multipleleast common multiple of the positive of the positive integers a and b is the smallest positive integers a and b is the smallest positive integer that is divisible by both a and b.integer that is divisible by both a and b.
We denote the least common multiple of a and We denote the least common multiple of a and b by lcm(a, b).b by lcm(a, b).
Examples:Examples:
lcm(3, 7) lcm(3, 7) ==
2121
lcm(4, 6) lcm(4, 6) ==
1212
lcm(5, 10) lcm(5, 10) ==
1010
Fall 2002 CMSC 203 - Discrete Structures 42
Least Common MultiplesLeast Common Multiples
Using prime factorizations:Using prime factorizations:
a = pa = p11aa1 1 p p22
aa2 2 … p… pnnaan n , b = p, b = p11
bb1 1 p p22bb2 2 … p… pnn
bbn n ,,
where pwhere p11 < p < p22 < … < p < … < pnn and a and aii, b, bii NN for 1 for 1 i i n n
lcm(a, b) = plcm(a, b) = p11max(amax(a11, b, b1 1 )) p p22
max(amax(a22, b, b2 2 )) … p… pnnmax(amax(ann, b, bn n ))
Example:Example:
a = 60 a = 60 = =
2222 3 311 5 511
b = 54 b = 54 = =
2211 3 333 5 500
lcm(a, b) lcm(a, b) = =
2222 3 333 5 51 1 = 4 = 427275 = 5405 = 540
Fall 2002 CMSC 203 - Discrete Structures 43
GCD and LCMGCD and LCM
a = 60 a = 60 = =
2222 3 311 5 511
b = 54 b = 54 = =
2211 3 333 5 500
lcm(a, b) lcm(a, b) = =
2222 3 333 5 51 1 = 540 = 540
gcd(a, b) gcd(a, b) = =
2211 3 311 5 50 0 = 6 = 6
Theorem: aTheorem: ab b ==
gcd(a,b)gcd(a,b)lcm(a,lcm(a,b)b)
Fall 2002 CMSC 203 - Discrete Structures 44
Modular ArithmeticModular Arithmetic
Let a be an integer and m be a positive integer.Let a be an integer and m be a positive integer.We denote by We denote by a mod ma mod m the remainder when a the remainder when a is divided by m.is divided by m.
Examples:Examples:
9 mod 4 9 mod 4 ==
11
9 mod 3 9 mod 3 ==
00
9 mod 10 9 mod 10 ==
99
-13 mod 4 -13 mod 4 ==
33
Fall 2002 CMSC 203 - Discrete Structures 45
CongruencesCongruences
Let a and b be integers and m be a positive Let a and b be integers and m be a positive integer. We say that integer. We say that a is congruent to b a is congruent to b modulo mmodulo m if if m divides a – b.m divides a – b.
We use the notation We use the notation a a b (mod m) b (mod m) to indicate to indicate that a is congruent to b modulo m.that a is congruent to b modulo m.
In other words:In other words:a a b (mod m) if and only if b (mod m) if and only if a mod m = b mod a mod m = b mod mm. .
Fall 2002 CMSC 203 - Discrete Structures 46
CongruencesCongruencesExamples:Examples:Is it true that 46 Is it true that 46 68 (mod 11) ? 68 (mod 11) ?Yes, because 11 | (46 – 68).Yes, because 11 | (46 – 68).Is it true that 46 Is it true that 46 68 (mod 22)? 68 (mod 22)?Yes, because 22 | (46 – 68).Yes, because 22 | (46 – 68).For which integers z is it true that z For which integers z is it true that z 12 (mod 12 (mod 10)?10)?It is true for any zIt is true for any z{…,-28, -18, -8, 2, 12, 22, 32, {…,-28, -18, -8, 2, 12, 22, 32, …}…}
Theorem:Theorem: Let m be a positive integer. The Let m be a positive integer. The integers a and b are congruent modulo m if and integers a and b are congruent modulo m if and only if there is an integer k such that a = b + km.only if there is an integer k such that a = b + km.
Fall 2002 CMSC 203 - Discrete Structures 47
CongruencesCongruences
Theorem:Theorem: Let m be a positive integer. Let m be a positive integer. If a If a b (mod m) and c b (mod m) and c d (mod m), then d (mod m), then a + c a + c b + d (mod m) and ac b + d (mod m) and ac bd (mod m). bd (mod m).Proof:Proof: We know that a We know that a b (mod m) and c b (mod m) and c d (mod m) d (mod m) implies that there are integers s and t with implies that there are integers s and t with b = a + sm and d = c + tm. b = a + sm and d = c + tm. Therefore,Therefore,b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) b + d = (a + sm) + (c + tm) = (a + c) + m(s + t) andandbd = (a + sm)(c + tm) = ac + m(at + cs + stm).bd = (a + sm)(c + tm) = ac + m(at + cs + stm).Hence, a + c Hence, a + c b + d (mod m) and ac b + d (mod m) and ac bd (mod bd (mod m).m).
Fall 2002 CMSC 203 - Discrete Structures 48
CongruencesCongruences
Theorem:Theorem: Let m be a positive integer. a Let m be a positive integer. a b (mod b (mod m) iff a mod m = b mod m.m) iff a mod m = b mod m.
Proof:Proof: Let a = mq1 + r1, and b = mq2 + r2.Let a = mq1 + r1, and b = mq2 + r2.Only if part:Only if part: a mod m = b mod m a mod m = b mod m r1 = r2, r1 = r2, thereforetherefore
a – b = m(q1 – q2), and a a – b = m(q1 – q2), and a b (mod m). b (mod m).If part:If part: a a b (mod m) implies b (mod m) implies a – b = mqa – b = mq mq1 + r1 – (mq2 + r2) = mqmq1 + r1 – (mq2 + r2) = mq r1 – r2 = m(q – q1 + q2).r1 – r2 = m(q – q1 + q2).Since 0 Since 0 r1, r2 r1, r2 m, 0 m, 0 |r1 - r2| |r1 - r2| m. The only m. The only multiple in that range is 0. multiple in that range is 0. Therefore r1 = r2, and a mod m = b mod m. Therefore r1 = r2, and a mod m = b mod m.
Fall 2002 CMSC 203 - Discrete Structures 49
The Euclidean Algorithm The Euclidean Algorithm
The The Euclidean AlgorithmEuclidean Algorithm finds the finds the greatest greatest common divisorcommon divisor of two integers a and b. of two integers a and b.
For example, if we want to find gcd(287, 91), For example, if we want to find gcd(287, 91), we we dividedivide 287 by 91: 287 by 91:
287 = 91287 = 913 + 143 + 14
We know that for integers a, b and c,We know that for integers a, b and c,if a | b and a | c, then a | (b + c).if a | b and a | c, then a | (b + c).
Therefore, any divisor (including their gcd) of Therefore, any divisor (including their gcd) of 287 and 91 must also be a divisor of 287 - 287 and 91 must also be a divisor of 287 - 91913 = 14.3 = 14.
Consequently, gcd(287, 91) = gcd(14, 91).Consequently, gcd(287, 91) = gcd(14, 91).
Fall 2002 CMSC 203 - Discrete Structures 50
The Euclidean Algorithm The Euclidean Algorithm
In the next step, we divide 91 by 14:In the next step, we divide 91 by 14:
91 = 1491 = 146 + 76 + 7
This means that gcd(14, 91) = gcd(14, 7).This means that gcd(14, 91) = gcd(14, 7).
So we divide 14 by 7:So we divide 14 by 7:
14 = 714 = 72 + 02 + 0
We find that 7 | 14, and thus gcd(14, 7) = 7.We find that 7 | 14, and thus gcd(14, 7) = 7.
Therefore, gcd(287, 91) = 7.Therefore, gcd(287, 91) = 7.
Fall 2002 CMSC 203 - Discrete Structures 51
The Euclidean Algorithm The Euclidean Algorithm
In In pseudocodepseudocode, the algorithm can be , the algorithm can be implemented as follows: implemented as follows:
procedureprocedure gcd(a, b: positive integers) gcd(a, b: positive integers)x := ax := ay := by := bwhilewhile y y 0 0beginbegin
r := x r := x modmod y yx := yx := yy := ry := r
endend {x is gcd(a, b)}{x is gcd(a, b)}
Fall 2002 CMSC 203 - Discrete Structures 52
Representations of IntegersRepresentations of Integers
Let b be a positive integer greater than 1.Let b be a positive integer greater than 1.Then if n is a positive integer, it can be Then if n is a positive integer, it can be expressed expressed uniquelyuniquely in the form: in the form:
n = an = akkbbkk + a + ak-1k-1bbk-1k-1 + … + a + … + a11b + ab + a00,,
where k is a nonnegative integer,where k is a nonnegative integer,aa00, a, a11, …, a, …, akk are nonnegative integers less than b, are nonnegative integers less than b,and aand akk 0. 0.
Example for b=10:Example for b=10:
859 = 8859 = 8101022 + 5 + 5101011 + 9 + 9101000
Fall 2002 CMSC 203 - Discrete Structures 53
Representations of IntegersRepresentations of Integers
Example for b=2 (binary expansion):Example for b=2 (binary expansion):
(10110)(10110)22 = 1 = 12244 + 1 + 12222 + 1 + 12211 = (22) = (22)1010
Example for b=16 (hexadecimal Example for b=16 (hexadecimal expansion):expansion):
(we use letters A to F to indicate numbers 10 to (we use letters A to F to indicate numbers 10 to 15)15)
(3A0F)(3A0F)1616 = 3 = 3161633 + 10 + 10161622 + 15 + 15161600 = (14863) = (14863)1010
Fall 2002 CMSC 203 - Discrete Structures 54
Representations of IntegersRepresentations of IntegersHow can we construct the base b expansion of an How can we construct the base b expansion of an integer n?integer n?
First, divide n by b to obtain a quotient qFirst, divide n by b to obtain a quotient q00 and and remainder aremainder a00, that is,, that is,
n = bqn = bq00 + a + a00, where 0 , where 0 a a00 < b. < b.
The remainder aThe remainder a00 is the rightmost digit in the is the rightmost digit in the base b expansion of n.base b expansion of n.
Next, divide qNext, divide q00 by b to obtain: by b to obtain:
qq00 = bq = bq11 + a + a11, where 0 , where 0 a a11 < b. < b.
aa11 is the second digit from the right in the base b is the second digit from the right in the base b expansion of n. Continue this process until you expansion of n. Continue this process until you obtain a quotient equal to zero.obtain a quotient equal to zero.
Fall 2002 CMSC 203 - Discrete Structures 55
Representations of IntegersRepresentations of Integers
Example:Example: What is the base 8 expansion of (12345)What is the base 8 expansion of (12345)10 10 ??
First, divide 12345 by 8:First, divide 12345 by 8:12345 = 812345 = 81543 + 11543 + 1
1543 = 81543 = 8192 + 7192 + 7192 = 8192 = 824 + 024 + 024 = 824 = 83 + 03 + 03 = 83 = 80 + 30 + 3
The result is: (12345)The result is: (12345)1010 = (30071) = (30071)88..
Fall 2002 CMSC 203 - Discrete Structures 56
Representations of IntegersRepresentations of Integers
procedure procedure base_b_expansion(n, b: positive base_b_expansion(n, b: positive integers)integers)q := nq := nk := 0k := 0whilewhile q q 0 0beginbegin
aakk := q mod b := q mod bq := q := q/bq/bk := k + 1k := k + 1
endend {the base b expansion of n is (a{the base b expansion of n is (ak-1k-1 … a … a11aa00))b b }}
Fall 2002 CMSC 203 - Discrete Structures 57
Addition of IntegersAddition of Integers
How do we (humans) add two integers?How do we (humans) add two integers?
Example: Example: 75837583 + + 49324932
5511552211
111111 carrycarry
Binary expansions: Binary expansions: (1011)(1011)22
+ + (1010)(1010)22
1100
carrycarry11
1100
11
11(( ))22
Fall 2002 CMSC 203 - Discrete Structures 58
Addition of IntegersAddition of Integers
Let a = (aLet a = (an-1n-1aan-2n-2…a…a11aa00))22, b = (b, b = (bn-1n-1bbn-2n-2…b…b11bb00))2.2.
How can we How can we algorithmically algorithmically add these two add these two binary numbers?binary numbers?First, add their rightmost bits:First, add their rightmost bits:
aa00 + b + b00 = c = c002 + s2 + s00,,
where swhere s00 is the is the rightmost bitrightmost bit in the binary in the binary expansion of a + b, and cexpansion of a + b, and c00 is the is the carrycarry..
Then, add the next pair of bits and the carry:Then, add the next pair of bits and the carry:
aa11 + b + b1 1 + c+ c00 = c = c112 + s2 + s11,,
where swhere s11 is the is the next bitnext bit in the binary expansion in the binary expansion of a + b, and cof a + b, and c11 is the carry. is the carry.
Fall 2002 CMSC 203 - Discrete Structures 59
Addition of IntegersAddition of Integers
Continue this process until you obtain cContinue this process until you obtain cn-1n-1..
The leading bit of the sum is sThe leading bit of the sum is snn = c = cn-1n-1..
The result is:The result is:
a + b = (sa + b = (snnssn-1n-1…s…s11ss00))22
Fall 2002 CMSC 203 - Discrete Structures 60
Addition of IntegersAddition of Integers
Example:Example:Add a = (1110)Add a = (1110)22 and b = (1011) and b = (1011)22..
aa00 + b + b00 = 0 + 1 = 0 = 0 + 1 = 02 + 1, so that c2 + 1, so that c00 = 0 and s = 0 and s00 = 1. = 1.
aa11 + b + b1 1 + c+ c00 = 1 + 1 + 0 = 1 = 1 + 1 + 0 = 12 + 0, so c2 + 0, so c11 = 1 and s = 1 and s11 = 0. = 0.
aa22 + b + b2 2 + c+ c11 = 1 + 0 + 1 = 1 = 1 + 0 + 1 = 12 + 0, so c2 + 0, so c22 = 1 and s = 1 and s22 = 0. = 0.
aa33 + b + b3 3 + c+ c22 = 1 + 1 + 1 = 1 = 1 + 1 + 1 = 12 + 1, so c2 + 1, so c33 = 1 and s = 1 and s33 = 1. = 1.
ss44 = c = c33 = 1. = 1.
Therefore, s = a + b = (11001)Therefore, s = a + b = (11001)22..
Fall 2002 CMSC 203 - Discrete Structures 61
Addition of IntegersAddition of Integers
procedure procedure add(a, b: positive integers)add(a, b: positive integers)c := 0c := 0for j := 0 to n-1for j := 0 to n-1beginbegin
d := d := (a(ajj + b + bjj + c)/2 + c)/2ssjj := a := ajj + b + bjj + c – 2d + c – 2dc := dc := d
endendssnn := c := c{the binary expansion of the sum is (s{the binary expansion of the sum is (snnssn-1n-1……ss11ss00))22}}