DISMATH
Discrete Mathematics
Methods of Proof, Algorithms,
and Number Theory
De La Salle University
September 2014
Overview
- Methods of Proof
Overview
- Methods of Proof
- Algorithms
Overview
- Methods of Proof
- Algorithms
- The Growth of Functions
Overview
- Methods of Proof
- Algorithms
- The Growth of Functions
- Complexity of Algorithms
Overview
- Methods of Proof
- Algorithms
- The Growth of Functions
- Complexity of Algorithms
- Integers and Division
Overview
- Methods of Proof
- Algorithms
- The Growth of Functions
- Complexity of Algorithms
- Integers and Division
- Number Theory and Applications
Why Study Proofs?
Computing systems are doing so much:
How can we guarantee they work?
Why Study Proofs?
Why not just testing?
- Integrates well with programming
- No new languages, tools required
- Conclusive evidence for bugs
Why Study Proofs?
Why not just testing?
- Integrates well with programming
- No new languages, tools required
- Conclusive evidence for bugs
Because. . .
- Difficult to assess coverage
- Cannot demonstrate absence of bugs
- No guarantees for safety-critical systems
Why Study Proofs?
Formal Verification
1. SOFTWARE:
If you want to debug a program beyond a doubt,
prove that it’s bug-free! Deduction and proof
provide universal guarantees.
2. HARDWARE:
Proof-theory has recently also been shown to be
useful in discovering bugs in pre-production
hardware.
Methods of Proof
- Direct Proof
Methods of Proof
- Direct Proof
- Proof by Contraposition
(Indirect)
Methods of Proof
- Direct Proof
- Proof by Contraposition
(Indirect)
- Vacuous and Trivial Proof
Methods of Proof
- Direct Proof
- Proof by Contraposition
(Indirect)
- Vacuous and Trivial Proof
- Proof by Contradiction (Indirect)
Methods of Proof
- Direct Proof
- Proof by Contraposition
(Indirect)
- Vacuous and Trivial Proof
- Proof by Contradiction (Indirect)
- Proof by Equivalence
Direct Proof
- In a conditional statement p → q, assume that
p is true and use definitions and previously
proven theorems, to show that q must also be
true.
Direct Proof
- In a conditional statement p → q, assume that
p is true and use definitions and previously
proven theorems, to show that q must also be
true.
- Ex. Give a direct proof of the theorem:
"If n is an odd integer, then n2
is odd."
Proof by Contraposition
- We take ¬q as a hypothesis, and using
definitions, and previously proven theorems, we
show that ¬p must follow.
Proof by Contraposition
- We take ¬q as a hypothesis, and using
definitions, and previously proven theorems, we
show that ¬p must follow.
- Ex. Prove that if n is an integer and 3n + 2 is
odd, then n is odd.
Exercise
- Prove that if n = ab, where a and b are positive
integers, then a ≤
√
n or b ≤
√
n.
Vacuous and Trivial Proofs
- Vacuous proof
Show that p is false, because p → q must be
true when p is false.
- ¬p → (p → q)
Vacuous and Trivial Proofs
- Vacuous proof
Show that p is false, because p → q must be
true when p is false.
- ¬p → (p → q)
- Trivial proof
Show that q is true, it follows that p → q must
also be true.
- q → (p → q)
Exercise
- Prove the statement: If there are 30 students
enrolled in this course this semester, then
62
= 36.
Exercise
- Prove the statement: If there are 30 students
enrolled in this course this semester, then
62
= 36.
- Prove the statement. If 6 is a prime number,
then 62
= 30.
Exercise
- Prove that if n is an integer and n2
is odd, then
n is odd.
Proof by Contradiction
- Show that assuming ¬p is true leads to a
contradiction.
Proof by Contradiction
- Show that assuming ¬p is true leads to a
contradiction.
- Ex. Prove that
√
2 is irrational by giving a
proof by contradiction.
Exercise
- Give a proof by contradiction of the theorem
If 3n + 2 is odd, then n is odd.
Proof by Equivalence
- To prove a theorem that is a biconditional
statement, p ↔ q, we show that p → q and
q → p are both true.
Proof by Equivalence
- To prove a theorem that is a biconditional
statement, p ↔ q, we show that p → q and
q → p are both true.
- (p ↔ q) ↔ [(p → q) ∧ (q → p)]
Exercise
- Prove the theorem
If n is a positive integer, then n is odd if and
only if n2
is odd.
Exercise
- Show that these statements about the integer n
are equivalent:
P1 : n is even.
P2 : n – 1 is odd.
P3 : n2
is even.
Exercise
- T/F: Every positive integer is the sum of the
squares of two integers.
Algorithm
Algorithm
- Algorithm
a finite set of precise instructions for
performing a computation or for solving a
problem.
Algorithm
- Algorithm
a finite set of precise instructions for
performing a computation or for solving a
problem.
- Ex. Describe an algorithm for finding the
maximum (largest) value in a finite sequence of
integers.
Finding the Maximum Element
Algorithm
1. Set the temporary maximum equal to the
first integer in the sequence.
Finding the Maximum Element
Algorithm
1. Set the temporary maximum equal to the
first integer in the sequence.
2. Compare the next integer in the sequence to
the temporary maximum, if larger, set the
temporary maximum equal to this integer.
Finding the Maximum Element
Algorithm
1. Set the temporary maximum equal to the
first integer in the sequence.
2. Compare the next integer in the sequence to
the temporary maximum, if larger, set the
temporary maximum equal to this integer.
3. Repeat the previous step if there are more
integers in the sequence.
Finding the Maximum Element
Algorithm
1. Set the temporary maximum equal to the
first integer in the sequence.
2. Compare the next integer in the sequence to
the temporary maximum, if larger, set the
temporary maximum equal to this integer.
3. Repeat the previous step if there are more
integers in the sequence.
4. Stop when there are no integers
left in the sequence.
Pseudocode
- high-level description of an algorithm that uses
the structural conventions of a programming
language, but is intended for human reading.
Pseudocode
- high-level description of an algorithm that uses
the structural conventions of a programming
language, but is intended for human reading.
- computer programs can be produced in any
computer language using the pseudocode
description as a starting point.
Finding the Maximum Element
Pseudocode
PROCEDURE max(a1, a2, . . ., an : integers)
max = a1
for i = 2 to n
if max  aj then max = aj
{Output: max is the largest element}
Preconditions and Postconditions
- Preconditions
statements that describe valid input
Preconditions and Postconditions
- Preconditions
statements that describe valid input
- Ex. (a1, a2, . . ., an) ∈ Z
Preconditions and Postconditions
- Preconditions
statements that describe valid input
- Ex. (a1, a2, . . ., an) ∈ Z
- Postconditions
conditions that the ouput should satisfy when
the program has run
Preconditions and Postconditions
- Preconditions
statements that describe valid input
- Ex. (a1, a2, . . ., an) ∈ Z
- Postconditions
conditions that the ouput should satisfy when
the program has run
- Ex. Output: max is the largest element
Properties of Algorithm
- Input
An algorithm has input values from a specified
set.
Properties of Algorithm
- Input
An algorithm has input values from a specified
set.
- Output
From each set of input values an algorithm
produces output values from a specified set.
Properties of Algorithm
- Input
An algorithm has input values from a specified
set.
- Output
From each set of input values an algorithm
produces output values from a specified set.
- Definiteness
The steps of an algorithm must be defined
precisely.
Properties of Algorithm
- Correctness
An algorithm should produce the correct
output values for each set of input values.
Properties of Algorithm
- Correctness
An algorithm should produce the correct
output values for each set of input values.
- Finiteness
An algorithm should produce the desired
output after a finite number of steps.
Properties of Algorithm
- Correctness
An algorithm should produce the correct
output values for each set of input values.
- Finiteness
An algorithm should produce the desired
output after a finite number of steps.
- Generality
The procedure should be applicable for all
problems of the desired form, not just for a
particular set of input.
Finding the Maximum Element
Algorithm Sample Program
Searching Algorithms
- The problem of locating an element in an
ordered list.
Searching Algorithms
- The problem of locating an element in an
ordered list.
- Locate an element x in a list of distinct
elements a1, a2, . . ., an, or determine that it is
not in the list.
Searching Algorithms
- The problem of locating an element in an
ordered list.
- Locate an element x in a list of distinct
elements a1, a2, . . ., an, or determine that it is
not in the list.
- Solution: the location of the term in the list
that equals x (that is, i is the solution if x = ai
) and is 0 if x is not in the list.
Linear Search Algorithm
PRECONDITION: Linear search (x : integer, a0, a1,. . . , an :
distinct integers)
- 1. Compare x and a0. If x = a0, location = 0,
else proceed to next element.
POSTCONDITION: Output the location.
Linear Search Algorithm
PRECONDITION: Linear search (x : integer, a0, a1,. . . , an :
distinct integers)
- 1. Compare x and a0. If x = a0, location = 0,
else proceed to next element.
- 2. Repeat step 1 while a match has not been
found and there are still elements.
POSTCONDITION: Output the location.
Linear Search Algorithm
PRECONDITION: Linear search (x : integer, a0, a1,. . . , an :
distinct integers)
- 1. Compare x and a0. If x = a0, location = 0,
else proceed to next element.
- 2. Repeat step 1 while a match has not been
found and there are still elements.
- 3. Output the location if a match is found, else
location = -1 signifying not found.
POSTCONDITION: Output the location.
Linear Search Pseudocode
PROCEDURE linear search
(PRECONDITION: x : integer, a0, a1, . . ., an–1 :
distinct integers)
i = 0
while (i  n and x = ai )
i = i + 1
if i  n then location = i
else location = –1
{POSTCONDITION: location is the subscript of
the term that equals x , or is –1 if x is not found}
Linear Search Sample Program
Binary Search Algorithm
PRECONDITION: Binary search (x : integer, a0, a1,. . . , an–1 :
sorted integers)
- 1. Compare x to the middle term of the list. If
x is larger, choose the upper half, else choose
the lower half.
POSTCONDITION: Output the location.
Binary Search Algorithm
PRECONDITION: Binary search (x : integer, a0, a1,. . . , an–1 :
sorted integers)
- 1. Compare x to the middle term of the list. If
x is larger, choose the upper half, else choose
the lower half.
- 2. Repeat step 1 while a match has not been
found and there are still elements.
POSTCONDITION: Output the location.
Binary Search Algorithm
PRECONDITION: Binary search (x : integer, a0, a1,. . . , an–1 :
sorted integers)
- 1. Compare x to the middle term of the list. If
x is larger, choose the upper half, else choose
the lower half.
- 2. Repeat step 1 while a match has not been
found and there are still elements.
- 3. Output the location if a match is found, else
location = -1 signifying not found.
POSTCONDITION: Output the location.
Example
- Search for 19 in the list
1 2 3 5 6 7 8 10 12 13 15 16 18
19 20 22
Binary Search Pseudocode
PRECONDITION: Binary search (x : integer, a0, a1,. . . , an–1 :
sorted integers)
i = 0 {i is left endpoint of search interval}
j = n – 1 {j is right endpoint of search interval} while i  j
{
m = (i + j)/2
if x  am then i = m + 1
else j = m
}
if x = ai then location = i
else location := –1
POSTCONDITION: Output the location.
Binary Search Sample
Program
Sorting Algorithms
- The problem of putting elements in increasing
order.
Sorting Algorithms
- The problem of putting elements in increasing
order.
- Given a list of elements of a set, sort in
increasing order.
Sorting Algorithms
- The problem of putting elements in increasing
order.
- Given a list of elements of a set, sort in
increasing order.
- Solution: bubble sort, insertion sort, etc.
Bubble Sort Algorithm
PRECONDITION: Bubble Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Successively compare adjacent elements.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Bubble Sort Algorithm
PRECONDITION: Bubble Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Successively compare adjacent elements.
- 2. Interchange elements if they are in the
wrong order.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Bubble Sort Algorithm
PRECONDITION: Bubble Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Successively compare adjacent elements.
- 2. Interchange elements if they are in the
wrong order.
- 3. Repeat until there are elements.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Bubble Sort Algorithm
PRECONDITION: Bubble Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Successively compare adjacent elements.
- 2. Interchange elements if they are in the
wrong order.
- 3. Repeat until there are elements.
- 4. Output the list of elements in increasing
order.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Bubble Sort Pseudocode
PROCEDURE bubble Sort
(PRECONDITION: 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
{POSTCONDITION: Output the elements a1,
a2,. . . , an in increasing order.}
Insertion Sort Algorithm
PRECONDITION: Insertion Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Compare second element a2 with the first
element a1.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Insertion Sort Algorithm
PRECONDITION: Insertion Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Compare second element a2 with the first
element a1.
- 2. If a2 is smaller, place it before a1 else place
it after a1.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Insertion Sort Algorithm
PRECONDITION: Insertion Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Compare second element a2 with the first
element a1.
- 2. If a2 is smaller, place it before a1 else place
it after a1.
- 3. Repeat until there are elements.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Insertion Sort Algorithm
PRECONDITION: Insertion Sort (a1, a2,. . . , an : real numbers
with n ≥ 2)
- 1. Compare second element a2 with the first
element a1.
- 2. If a2 is smaller, place it before a1 else place
it after a1.
- 3. Repeat until there are elements.
- 4. Output the list of elements in increasing
order.
POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.
Insertion Sort Pseudocode
PROCEDURE Insertion Sort
PRECONDITION: a1, a2,. . . , an : real numbers with n ≥ 2
for j = 2 to n
{
i = 1
while aj  ai
i = i + 1
m = aj
for k = 0 to j – i – 1
aj–k = aj–k–1
ai = m
}
{POSTCONDITION: Output the elements a1, a2,. . . , an
in increasing order.}
Greedy Algorithm
- selects the best choice at each step, instead of
considering all sequences of steps that may lead
to an optimal. solution.
Greedy Algorithm
- selects the best choice at each step, instead of
considering all sequences of steps that may lead
to an optimal. solution.
- applied in optimization problems where a
solution to the given problem either minimizes
or maximizes the value of some parameter.
Exercise
- Consider the problem of making n cents change
with quarters, dimes, nickels, and pennies, and
using the least total number of coins.
Greedy Change-Making Algorithm
Pseudocode
PROCEDURE change
PRECONDITION: c1, c2,. . . , cn values of denominations of
coins, where c1  c2  . . .  cn; n ∈ Z+
for i = 1 to r
while n ≥ ci
add a coin with value ci to the change
n = n – ci
endwhile
endfor
{POSTCONDITION: Output the minimum number of coins.}
Growth of Functions
- The growth of functions is often described
using Big-O Notation.
The constants C and k in the definition of big-O notation are
called witnesses.
Growth of Functions
- The growth of functions is often described
using Big-O Notation.
- Definition: Let f and g be functions from
R → R; f (x) is O(g(x)) if there are constants
C and k such that
|f (x)| C |g (x)|
whenever x  k.
The constants C and k in the definition of big-O notation are
called witnesses.
Example
- Show that f (x) = x2
+ 2x + 1 is O(x2
).
◦ A useful approach for finding a pair of witnesses
is to first select a value of k for which the size of
|f (x)| can be readily estimated when x  k.
Illustration
Drill
- Show that 7x2
is O(x3
).
Drill
- Show that 7x2
is O(x3
).
- Show that n2
is not O(n).
Drill
- How can big- O notation be used to estimate
the sum of the first n positive integers?
Drill
- How can big- O notation be used to estimate
the sum of the first n positive integers?
- Give big- O estimates for the factorial function
and the logarithm of the factorial function.
Common Big-O Estimates
Big-Omega and Big-Theta Notation
- Big-O notation does not provide a lower bound
for the size of f (x).
Big-Omega and Big-Theta Notation
- Big-O notation does not provide a lower bound
for the size of f (x).
- For the lower bound, we use big-Omega
(big-Ω) notation.
Big-Omega and Big-Theta Notation
- Big-O notation does not provide a lower bound
for the size of f (x).
- For the lower bound, we use big-Omega
(big-Ω) notation.
- For the both lower and upper bound, we use
big-Theta (big-Θ) notation.
Algorithm Time Complexity
- can be expressed in terms of the number of
operations used by the algorithm when the
input has a particular size.
Algorithm Time Complexity
- can be expressed in terms of the number of
operations used by the algorithm when the
input has a particular size.
- the number of comparisons will be used as the
measure of the time complexity of the
algorithm, because comparisons are the basic
operations used.
Complexity of Algorithms
Example
- Describe the time complexity of algorithm for
finding the maximum element in a set.
PROCEDURE max(a1, a2, . . ., an : integers)
max = a1
for i = 2 to n
if max  aj then max = aj
{Output: max is the largest element}
Drill
- What is the worst-case complexity of the
bubble sort in terms of the number of
comparisons made?
PROCEDURE bubble Sort
(PRECONDITION: 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
{POSTCONDITION: Output the elements a1,
a2,. . . , an in increasing order.}
Division and Modulo Operator
- Let a be an integer and d a positive integer.
Then there are unique integers q and r, with
0 ≤ r  d, such that a = dq + r.
Division and Modulo Operator
- Let a be an integer and d a positive integer.
Then there are unique integers q and r, with
0 ≤ r  d, such that a = dq + r.
q = a div d r = a mod d
Division and Modulo Operator
- Let a be an integer and d a positive integer.
Then there are unique integers q and r, with
0 ≤ r  d, such that a = dq + r.
q = a div d r = a mod d
- In the equality given, d is called the divisor, a
is called the dividend, q is called the quotient,
and r is called the remainder.
Example
- What are the quotient and remainder when 101
is divided by 11 ?
Example
- What are the quotient and remainder when 101
is divided by 11 ?
- What are the quotient and remainder when –11
is divided by 3?
Modulo Equivalence
- 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.
Modulo Equivalence
- 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.
a ≡ b mod m iff. m|(a – b)
Example
- Determine whether 17 is congruent to 5
modulo 6 and whether 24 and 14 are congruent
modulo 6.
Applications
- Cryptology: the study of secret messages.
- Ex. Caesar Cipher: Messages are made secret
by shifting each letter three letters forward in
the alphabet.
Applications
- Cryptology: the study of secret messages.
- Ex. Caesar Cipher: Messages are made secret
by shifting each letter three letters forward in
the alphabet.
- Caesar’s encryption method can be represented
by the function f that assigns to the
nonnegative integer p, p ≤ 25, the integer f(p)
in the set {0, 1, 2, ..., 25} with
f (p) = (p + 3) mod 26
Example
- What is the secret message produced from the
message MEET YOU IN DLSU using the
Caesar cipher?
Example
- What is the secret message produced from the
message MEET YOU IN DLSU using the
Caesar cipher?
- PHHW BRX LQ GOVY
Example
- What is the secret message produced from the
message MEET YOU IN DLSU using the
Caesar cipher?
- PHHW BRX LQ GOVY
f –1
(p) = (p – 3) mod 26
Representation 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
Representation 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 nonnegative integer, a0, a1, . . . , ak
are nonnegative integers less than b, and
ak = 0.
Example
- What is the decimal expansion of the integer
that has (101011111)2 as its binary expansion?
Example
- What is the decimal expansion of the integer
that has (101011111)2 as its binary expansion?
- What is the decimal expansion of the
hexadecimal expansion of (2AEOB)16?
Base Conversion Algorithm
1. Divide n by b to obtain a quotient and
remainder, n = bq0 + a0, 0 ≤ a0  b
Base Conversion Algorithm
1. Divide n by b to obtain a quotient and
remainder, n = bq0 + a0, 0 ≤ a0  b
2. The remainder, a0, is the rightmost digit in
the base b expansion of n.
Base Conversion Algorithm
1. Divide n by b to obtain a quotient and
remainder, n = bq0 + a0, 0 ≤ a0  b
2. The remainder, a0, is the rightmost digit in
the base b expansion of n.
3. Divide q0 by b to obtain q0 = bq1 + a1,
0 ≤ a1  b
Base Conversion Algorithm
1. Divide n by b to obtain a quotient and
remainder, n = bq0 + a0, 0 ≤ a0  b
2. The remainder, a0, is the rightmost digit in
the base b expansion of n.
3. Divide q0 by b to obtain q0 = bq1 + a1,
0 ≤ a1  b
4. The remainder, a1, is the second digit from
the right in the base b expansion of n.
Base Conversion Algorithm
1. Divide n by b to obtain a quotient and
remainder, n = bq0 + a0, 0 ≤ a0  b
2. The remainder, a0, is the rightmost digit in
the base b expansion of n.
3. Divide q0 by b to obtain q0 = bq1 + a1,
0 ≤ a1  b
4. The remainder, a1, is the second digit from
the right in the base b expansion of n.
5. Continue process until we obtain a quotient
equal to zero.
Example
- Find the base 8, or octal, expansion of
(12345)10.
Example
- Find the base 8, or octal, expansion of
(12345)10.
- Find the hexadecimal expansion of (177130)10.
Example
- Find the base 8, or octal, expansion of
(12345)10.
- Find the hexadecimal expansion of (177130)10.
- Find the binary expansion of (241)10.
Base b Expansions Algorithm
Pseudocode
PROCEDURE base b expansion
PRECONDITION: (n: positive integer);
q = n
k = 0
while q = 0
{
ak = qmodb
q = q/b
k = k + 1
}
{POSTCONDITION: the base b expansion of n is
(ak–1, . . . , a1, a0)b}
Euclidean Algorithm
A method for computing the greatest common
divisor of two integers.
- Let a = bq + r, where a, b, q, and r are
integers. Then gcd(a, b) = gcd(b, r).
Euclidean Algorithm
A method for computing the greatest common
divisor of two integers.
- Let a = bq + r, where a, b, q, and r are
integers. Then gcd(a, b) = gcd(b, r).
gcd(a, b) = gcd(ro, r1) = gcd(r1, r2) = . . .
= gcd(rn–2, rn–d = gcd(rn1
, rn) = gcd(rn, 0) = rn.
Euclidean Algorithm
A method for computing the greatest common
divisor of two integers.
- Let a = bq + r, where a, b, q, and r are
integers. Then gcd(a, b) = gcd(b, r).
gcd(a, b) = gcd(ro, r1) = gcd(r1, r2) = . . .
= gcd(rn–2, rn–d = gcd(rn1
, rn) = gcd(rn, 0) = rn.
- The greatest common divisor is the last
nonzero remainder in the sequence of divisions.
Example
- Find the greatest common divisor of 414 and
662 using the Euclidean algorithm.
Euclidean Algorithm Pseudocode
PROCEDURE GCD
PRECONDITION: (a, b: positive integers; ab);
x = a
y = b
while y = 0
{
r = x mod y
x = y
y = r
}
{POSTCONDITION: GCD(a, b) is x }
Reference:
Rosen, K.H. Discrete Mathematics and Its
Applications (7 ed.), New York, McGraw-Hill
Thank you for your attention!

DISMATH_Part2

  • 1.
    DISMATH Discrete Mathematics Methods ofProof, Algorithms, and Number Theory De La Salle University September 2014
  • 2.
  • 3.
    Overview - Methods ofProof - Algorithms
  • 4.
    Overview - Methods ofProof - Algorithms - The Growth of Functions
  • 5.
    Overview - Methods ofProof - Algorithms - The Growth of Functions - Complexity of Algorithms
  • 6.
    Overview - Methods ofProof - Algorithms - The Growth of Functions - Complexity of Algorithms - Integers and Division
  • 7.
    Overview - Methods ofProof - Algorithms - The Growth of Functions - Complexity of Algorithms - Integers and Division - Number Theory and Applications
  • 8.
    Why Study Proofs? Computingsystems are doing so much: How can we guarantee they work?
  • 9.
    Why Study Proofs? Whynot just testing? - Integrates well with programming - No new languages, tools required - Conclusive evidence for bugs
  • 10.
    Why Study Proofs? Whynot just testing? - Integrates well with programming - No new languages, tools required - Conclusive evidence for bugs Because. . . - Difficult to assess coverage - Cannot demonstrate absence of bugs - No guarantees for safety-critical systems
  • 11.
    Why Study Proofs? FormalVerification 1. SOFTWARE: If you want to debug a program beyond a doubt, prove that it’s bug-free! Deduction and proof provide universal guarantees. 2. HARDWARE: Proof-theory has recently also been shown to be useful in discovering bugs in pre-production hardware.
  • 12.
    Methods of Proof -Direct Proof
  • 13.
    Methods of Proof -Direct Proof - Proof by Contraposition (Indirect)
  • 14.
    Methods of Proof -Direct Proof - Proof by Contraposition (Indirect) - Vacuous and Trivial Proof
  • 15.
    Methods of Proof -Direct Proof - Proof by Contraposition (Indirect) - Vacuous and Trivial Proof - Proof by Contradiction (Indirect)
  • 16.
    Methods of Proof -Direct Proof - Proof by Contraposition (Indirect) - Vacuous and Trivial Proof - Proof by Contradiction (Indirect) - Proof by Equivalence
  • 17.
    Direct Proof - Ina conditional statement p → q, assume that p is true and use definitions and previously proven theorems, to show that q must also be true.
  • 18.
    Direct Proof - Ina conditional statement p → q, assume that p is true and use definitions and previously proven theorems, to show that q must also be true. - Ex. Give a direct proof of the theorem: "If n is an odd integer, then n2 is odd."
  • 19.
    Proof by Contraposition -We take ¬q as a hypothesis, and using definitions, and previously proven theorems, we show that ¬p must follow.
  • 20.
    Proof by Contraposition -We take ¬q as a hypothesis, and using definitions, and previously proven theorems, we show that ¬p must follow. - Ex. Prove that if n is an integer and 3n + 2 is odd, then n is odd.
  • 21.
    Exercise - Prove thatif n = ab, where a and b are positive integers, then a ≤ √ n or b ≤ √ n.
  • 22.
    Vacuous and TrivialProofs - Vacuous proof Show that p is false, because p → q must be true when p is false. - ¬p → (p → q)
  • 23.
    Vacuous and TrivialProofs - Vacuous proof Show that p is false, because p → q must be true when p is false. - ¬p → (p → q) - Trivial proof Show that q is true, it follows that p → q must also be true. - q → (p → q)
  • 24.
    Exercise - Prove thestatement: If there are 30 students enrolled in this course this semester, then 62 = 36.
  • 25.
    Exercise - Prove thestatement: If there are 30 students enrolled in this course this semester, then 62 = 36. - Prove the statement. If 6 is a prime number, then 62 = 30.
  • 26.
    Exercise - Prove thatif n is an integer and n2 is odd, then n is odd.
  • 27.
    Proof by Contradiction -Show that assuming ¬p is true leads to a contradiction.
  • 28.
    Proof by Contradiction -Show that assuming ¬p is true leads to a contradiction. - Ex. Prove that √ 2 is irrational by giving a proof by contradiction.
  • 29.
    Exercise - Give aproof by contradiction of the theorem If 3n + 2 is odd, then n is odd.
  • 30.
    Proof by Equivalence -To prove a theorem that is a biconditional statement, p ↔ q, we show that p → q and q → p are both true.
  • 31.
    Proof by Equivalence -To prove a theorem that is a biconditional statement, p ↔ q, we show that p → q and q → p are both true. - (p ↔ q) ↔ [(p → q) ∧ (q → p)]
  • 32.
    Exercise - Prove thetheorem If n is a positive integer, then n is odd if and only if n2 is odd.
  • 33.
    Exercise - Show thatthese statements about the integer n are equivalent: P1 : n is even. P2 : n – 1 is odd. P3 : n2 is even.
  • 34.
    Exercise - T/F: Everypositive integer is the sum of the squares of two integers.
  • 35.
  • 36.
    Algorithm - Algorithm a finiteset of precise instructions for performing a computation or for solving a problem.
  • 37.
    Algorithm - Algorithm a finiteset of precise instructions for performing a computation or for solving a problem. - Ex. Describe an algorithm for finding the maximum (largest) value in a finite sequence of integers.
  • 38.
    Finding the MaximumElement Algorithm 1. Set the temporary maximum equal to the first integer in the sequence.
  • 39.
    Finding the MaximumElement Algorithm 1. Set the temporary maximum equal to the first integer in the sequence. 2. Compare the next integer in the sequence to the temporary maximum, if larger, set the temporary maximum equal to this integer.
  • 40.
    Finding the MaximumElement Algorithm 1. Set the temporary maximum equal to the first integer in the sequence. 2. Compare the next integer in the sequence to the temporary maximum, if larger, set the temporary maximum equal to this integer. 3. Repeat the previous step if there are more integers in the sequence.
  • 41.
    Finding the MaximumElement Algorithm 1. Set the temporary maximum equal to the first integer in the sequence. 2. Compare the next integer in the sequence to the temporary maximum, if larger, set the temporary maximum equal to this integer. 3. Repeat the previous step if there are more integers in the sequence. 4. Stop when there are no integers left in the sequence.
  • 42.
    Pseudocode - high-level descriptionof an algorithm that uses the structural conventions of a programming language, but is intended for human reading.
  • 43.
    Pseudocode - high-level descriptionof an algorithm that uses the structural conventions of a programming language, but is intended for human reading. - computer programs can be produced in any computer language using the pseudocode description as a starting point.
  • 44.
    Finding the MaximumElement Pseudocode PROCEDURE max(a1, a2, . . ., an : integers) max = a1 for i = 2 to n if max aj then max = aj {Output: max is the largest element}
  • 45.
    Preconditions and Postconditions -Preconditions statements that describe valid input
  • 46.
    Preconditions and Postconditions -Preconditions statements that describe valid input - Ex. (a1, a2, . . ., an) ∈ Z
  • 47.
    Preconditions and Postconditions -Preconditions statements that describe valid input - Ex. (a1, a2, . . ., an) ∈ Z - Postconditions conditions that the ouput should satisfy when the program has run
  • 48.
    Preconditions and Postconditions -Preconditions statements that describe valid input - Ex. (a1, a2, . . ., an) ∈ Z - Postconditions conditions that the ouput should satisfy when the program has run - Ex. Output: max is the largest element
  • 49.
    Properties of Algorithm -Input An algorithm has input values from a specified set.
  • 50.
    Properties of Algorithm -Input An algorithm has input values from a specified set. - Output From each set of input values an algorithm produces output values from a specified set.
  • 51.
    Properties of Algorithm -Input An algorithm has input values from a specified set. - Output From each set of input values an algorithm produces output values from a specified set. - Definiteness The steps of an algorithm must be defined precisely.
  • 52.
    Properties of Algorithm -Correctness An algorithm should produce the correct output values for each set of input values.
  • 53.
    Properties of Algorithm -Correctness An algorithm should produce the correct output values for each set of input values. - Finiteness An algorithm should produce the desired output after a finite number of steps.
  • 54.
    Properties of Algorithm -Correctness An algorithm should produce the correct output values for each set of input values. - Finiteness An algorithm should produce the desired output after a finite number of steps. - Generality The procedure should be applicable for all problems of the desired form, not just for a particular set of input.
  • 55.
    Finding the MaximumElement Algorithm Sample Program
  • 56.
    Searching Algorithms - Theproblem of locating an element in an ordered list.
  • 57.
    Searching Algorithms - Theproblem of locating an element in an ordered list. - Locate an element x in a list of distinct elements a1, a2, . . ., an, or determine that it is not in the list.
  • 58.
    Searching Algorithms - Theproblem of locating an element in an ordered list. - Locate an element x in a list of distinct elements a1, a2, . . ., an, or determine that it is not in the list. - Solution: the location of the term in the list that equals x (that is, i is the solution if x = ai ) and is 0 if x is not in the list.
  • 59.
    Linear Search Algorithm PRECONDITION:Linear search (x : integer, a0, a1,. . . , an : distinct integers) - 1. Compare x and a0. If x = a0, location = 0, else proceed to next element. POSTCONDITION: Output the location.
  • 60.
    Linear Search Algorithm PRECONDITION:Linear search (x : integer, a0, a1,. . . , an : distinct integers) - 1. Compare x and a0. If x = a0, location = 0, else proceed to next element. - 2. Repeat step 1 while a match has not been found and there are still elements. POSTCONDITION: Output the location.
  • 61.
    Linear Search Algorithm PRECONDITION:Linear search (x : integer, a0, a1,. . . , an : distinct integers) - 1. Compare x and a0. If x = a0, location = 0, else proceed to next element. - 2. Repeat step 1 while a match has not been found and there are still elements. - 3. Output the location if a match is found, else location = -1 signifying not found. POSTCONDITION: Output the location.
  • 62.
    Linear Search Pseudocode PROCEDURElinear search (PRECONDITION: x : integer, a0, a1, . . ., an–1 : distinct integers) i = 0 while (i n and x = ai ) i = i + 1 if i n then location = i else location = –1 {POSTCONDITION: location is the subscript of the term that equals x , or is –1 if x is not found}
  • 63.
  • 64.
    Binary Search Algorithm PRECONDITION:Binary search (x : integer, a0, a1,. . . , an–1 : sorted integers) - 1. Compare x to the middle term of the list. If x is larger, choose the upper half, else choose the lower half. POSTCONDITION: Output the location.
  • 65.
    Binary Search Algorithm PRECONDITION:Binary search (x : integer, a0, a1,. . . , an–1 : sorted integers) - 1. Compare x to the middle term of the list. If x is larger, choose the upper half, else choose the lower half. - 2. Repeat step 1 while a match has not been found and there are still elements. POSTCONDITION: Output the location.
  • 66.
    Binary Search Algorithm PRECONDITION:Binary search (x : integer, a0, a1,. . . , an–1 : sorted integers) - 1. Compare x to the middle term of the list. If x is larger, choose the upper half, else choose the lower half. - 2. Repeat step 1 while a match has not been found and there are still elements. - 3. Output the location if a match is found, else location = -1 signifying not found. POSTCONDITION: Output the location.
  • 67.
    Example - Search for19 in the list 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22
  • 68.
    Binary Search Pseudocode PRECONDITION:Binary search (x : integer, a0, a1,. . . , an–1 : sorted integers) i = 0 {i is left endpoint of search interval} j = n – 1 {j is right endpoint of search interval} while i j { m = (i + j)/2 if x am then i = m + 1 else j = m } if x = ai then location = i else location := –1 POSTCONDITION: Output the location.
  • 69.
  • 70.
    Sorting Algorithms - Theproblem of putting elements in increasing order.
  • 71.
    Sorting Algorithms - Theproblem of putting elements in increasing order. - Given a list of elements of a set, sort in increasing order.
  • 72.
    Sorting Algorithms - Theproblem of putting elements in increasing order. - Given a list of elements of a set, sort in increasing order. - Solution: bubble sort, insertion sort, etc.
  • 73.
    Bubble Sort Algorithm PRECONDITION:Bubble Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Successively compare adjacent elements. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 74.
    Bubble Sort Algorithm PRECONDITION:Bubble Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Successively compare adjacent elements. - 2. Interchange elements if they are in the wrong order. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 75.
    Bubble Sort Algorithm PRECONDITION:Bubble Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Successively compare adjacent elements. - 2. Interchange elements if they are in the wrong order. - 3. Repeat until there are elements. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 76.
    Bubble Sort Algorithm PRECONDITION:Bubble Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Successively compare adjacent elements. - 2. Interchange elements if they are in the wrong order. - 3. Repeat until there are elements. - 4. Output the list of elements in increasing order. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 77.
    Bubble Sort Pseudocode PROCEDUREbubble Sort (PRECONDITION: 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 {POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.}
  • 78.
    Insertion Sort Algorithm PRECONDITION:Insertion Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Compare second element a2 with the first element a1. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 79.
    Insertion Sort Algorithm PRECONDITION:Insertion Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Compare second element a2 with the first element a1. - 2. If a2 is smaller, place it before a1 else place it after a1. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 80.
    Insertion Sort Algorithm PRECONDITION:Insertion Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Compare second element a2 with the first element a1. - 2. If a2 is smaller, place it before a1 else place it after a1. - 3. Repeat until there are elements. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 81.
    Insertion Sort Algorithm PRECONDITION:Insertion Sort (a1, a2,. . . , an : real numbers with n ≥ 2) - 1. Compare second element a2 with the first element a1. - 2. If a2 is smaller, place it before a1 else place it after a1. - 3. Repeat until there are elements. - 4. Output the list of elements in increasing order. POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.
  • 82.
    Insertion Sort Pseudocode PROCEDUREInsertion Sort PRECONDITION: a1, a2,. . . , an : real numbers with n ≥ 2 for j = 2 to n { i = 1 while aj ai i = i + 1 m = aj for k = 0 to j – i – 1 aj–k = aj–k–1 ai = m } {POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.}
  • 83.
    Greedy Algorithm - selectsthe best choice at each step, instead of considering all sequences of steps that may lead to an optimal. solution.
  • 84.
    Greedy Algorithm - selectsthe best choice at each step, instead of considering all sequences of steps that may lead to an optimal. solution. - applied in optimization problems where a solution to the given problem either minimizes or maximizes the value of some parameter.
  • 85.
    Exercise - Consider theproblem of making n cents change with quarters, dimes, nickels, and pennies, and using the least total number of coins.
  • 86.
    Greedy Change-Making Algorithm Pseudocode PROCEDUREchange PRECONDITION: c1, c2,. . . , cn values of denominations of coins, where c1 c2 . . . cn; n ∈ Z+ for i = 1 to r while n ≥ ci add a coin with value ci to the change n = n – ci endwhile endfor {POSTCONDITION: Output the minimum number of coins.}
  • 87.
    Growth of Functions -The growth of functions is often described using Big-O Notation. The constants C and k in the definition of big-O notation are called witnesses.
  • 88.
    Growth of Functions -The growth of functions is often described using Big-O Notation. - Definition: Let f and g be functions from R → R; f (x) is O(g(x)) if there are constants C and k such that |f (x)| C |g (x)| whenever x k. The constants C and k in the definition of big-O notation are called witnesses.
  • 89.
    Example - Show thatf (x) = x2 + 2x + 1 is O(x2 ). ◦ A useful approach for finding a pair of witnesses is to first select a value of k for which the size of |f (x)| can be readily estimated when x k.
  • 90.
  • 91.
    Drill - Show that7x2 is O(x3 ).
  • 92.
    Drill - Show that7x2 is O(x3 ). - Show that n2 is not O(n).
  • 93.
    Drill - How canbig- O notation be used to estimate the sum of the first n positive integers?
  • 94.
    Drill - How canbig- O notation be used to estimate the sum of the first n positive integers? - Give big- O estimates for the factorial function and the logarithm of the factorial function.
  • 95.
  • 96.
    Big-Omega and Big-ThetaNotation - Big-O notation does not provide a lower bound for the size of f (x).
  • 97.
    Big-Omega and Big-ThetaNotation - Big-O notation does not provide a lower bound for the size of f (x). - For the lower bound, we use big-Omega (big-Ω) notation.
  • 98.
    Big-Omega and Big-ThetaNotation - Big-O notation does not provide a lower bound for the size of f (x). - For the lower bound, we use big-Omega (big-Ω) notation. - For the both lower and upper bound, we use big-Theta (big-Θ) notation.
  • 99.
    Algorithm Time Complexity -can be expressed in terms of the number of operations used by the algorithm when the input has a particular size.
  • 100.
    Algorithm Time Complexity -can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. - the number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used.
  • 101.
  • 102.
    Example - Describe thetime complexity of algorithm for finding the maximum element in a set. PROCEDURE max(a1, a2, . . ., an : integers) max = a1 for i = 2 to n if max aj then max = aj {Output: max is the largest element}
  • 103.
    Drill - What isthe worst-case complexity of the bubble sort in terms of the number of comparisons made? PROCEDURE bubble Sort (PRECONDITION: 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 {POSTCONDITION: Output the elements a1, a2,. . . , an in increasing order.}
  • 104.
    Division and ModuloOperator - Let a be an integer and d a positive integer. Then there are unique integers q and r, with 0 ≤ r d, such that a = dq + r.
  • 105.
    Division and ModuloOperator - Let a be an integer and d a positive integer. Then there are unique integers q and r, with 0 ≤ r d, such that a = dq + r. q = a div d r = a mod d
  • 106.
    Division and ModuloOperator - Let a be an integer and d a positive integer. Then there are unique integers q and r, with 0 ≤ r d, such that a = dq + r. q = a div d r = a mod d - In the equality given, d is called the divisor, a is called the dividend, q is called the quotient, and r is called the remainder.
  • 107.
    Example - What arethe quotient and remainder when 101 is divided by 11 ?
  • 108.
    Example - What arethe quotient and remainder when 101 is divided by 11 ? - What are the quotient and remainder when –11 is divided by 3?
  • 109.
    Modulo Equivalence - Ifa and b are integers and m is a positive integer, then a is congruent to b modulo m if m divides a – b.
  • 110.
    Modulo Equivalence - Ifa 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. a ≡ b mod m iff. m|(a – b)
  • 111.
    Example - Determine whether17 is congruent to 5 modulo 6 and whether 24 and 14 are congruent modulo 6.
  • 112.
    Applications - Cryptology: thestudy of secret messages. - Ex. Caesar Cipher: Messages are made secret by shifting each letter three letters forward in the alphabet.
  • 113.
    Applications - Cryptology: thestudy of secret messages. - Ex. Caesar Cipher: Messages are made secret by shifting each letter three letters forward in the alphabet. - Caesar’s encryption method can be represented by the function f that assigns to the nonnegative integer p, p ≤ 25, the integer f(p) in the set {0, 1, 2, ..., 25} with f (p) = (p + 3) mod 26
  • 114.
    Example - What isthe secret message produced from the message MEET YOU IN DLSU using the Caesar cipher?
  • 115.
    Example - What isthe secret message produced from the message MEET YOU IN DLSU using the Caesar cipher? - PHHW BRX LQ GOVY
  • 116.
    Example - What isthe secret message produced from the message MEET YOU IN DLSU using the Caesar cipher? - PHHW BRX LQ GOVY f –1 (p) = (p – 3) mod 26
  • 117.
    Representation 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
  • 118.
    Representation 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 nonnegative integer, a0, a1, . . . , ak are nonnegative integers less than b, and ak = 0.
  • 119.
    Example - What isthe decimal expansion of the integer that has (101011111)2 as its binary expansion?
  • 120.
    Example - What isthe decimal expansion of the integer that has (101011111)2 as its binary expansion? - What is the decimal expansion of the hexadecimal expansion of (2AEOB)16?
  • 121.
    Base Conversion Algorithm 1.Divide n by b to obtain a quotient and remainder, n = bq0 + a0, 0 ≤ a0 b
  • 122.
    Base Conversion Algorithm 1.Divide n by b to obtain a quotient and remainder, n = bq0 + a0, 0 ≤ a0 b 2. The remainder, a0, is the rightmost digit in the base b expansion of n.
  • 123.
    Base Conversion Algorithm 1.Divide n by b to obtain a quotient and remainder, n = bq0 + a0, 0 ≤ a0 b 2. The remainder, a0, is the rightmost digit in the base b expansion of n. 3. Divide q0 by b to obtain q0 = bq1 + a1, 0 ≤ a1 b
  • 124.
    Base Conversion Algorithm 1.Divide n by b to obtain a quotient and remainder, n = bq0 + a0, 0 ≤ a0 b 2. The remainder, a0, is the rightmost digit in the base b expansion of n. 3. Divide q0 by b to obtain q0 = bq1 + a1, 0 ≤ a1 b 4. The remainder, a1, is the second digit from the right in the base b expansion of n.
  • 125.
    Base Conversion Algorithm 1.Divide n by b to obtain a quotient and remainder, n = bq0 + a0, 0 ≤ a0 b 2. The remainder, a0, is the rightmost digit in the base b expansion of n. 3. Divide q0 by b to obtain q0 = bq1 + a1, 0 ≤ a1 b 4. The remainder, a1, is the second digit from the right in the base b expansion of n. 5. Continue process until we obtain a quotient equal to zero.
  • 126.
    Example - Find thebase 8, or octal, expansion of (12345)10.
  • 127.
    Example - Find thebase 8, or octal, expansion of (12345)10. - Find the hexadecimal expansion of (177130)10.
  • 128.
    Example - Find thebase 8, or octal, expansion of (12345)10. - Find the hexadecimal expansion of (177130)10. - Find the binary expansion of (241)10.
  • 129.
    Base b ExpansionsAlgorithm Pseudocode PROCEDURE base b expansion PRECONDITION: (n: positive integer); q = n k = 0 while q = 0 { ak = qmodb q = q/b k = k + 1 } {POSTCONDITION: the base b expansion of n is (ak–1, . . . , a1, a0)b}
  • 130.
    Euclidean Algorithm A methodfor computing the greatest common divisor of two integers. - Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) = gcd(b, r).
  • 131.
    Euclidean Algorithm A methodfor computing the greatest common divisor of two integers. - Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) = gcd(b, r). gcd(a, b) = gcd(ro, r1) = gcd(r1, r2) = . . . = gcd(rn–2, rn–d = gcd(rn1 , rn) = gcd(rn, 0) = rn.
  • 132.
    Euclidean Algorithm A methodfor computing the greatest common divisor of two integers. - Let a = bq + r, where a, b, q, and r are integers. Then gcd(a, b) = gcd(b, r). gcd(a, b) = gcd(ro, r1) = gcd(r1, r2) = . . . = gcd(rn–2, rn–d = gcd(rn1 , rn) = gcd(rn, 0) = rn. - The greatest common divisor is the last nonzero remainder in the sequence of divisions.
  • 133.
    Example - Find thegreatest common divisor of 414 and 662 using the Euclidean algorithm.
  • 134.
    Euclidean Algorithm Pseudocode PROCEDUREGCD PRECONDITION: (a, b: positive integers; ab); x = a y = b while y = 0 { r = x mod y x = y y = r } {POSTCONDITION: GCD(a, b) is x }
  • 135.
    Reference: Rosen, K.H. DiscreteMathematics and Its Applications (7 ed.), New York, McGraw-Hill
  • 136.
    Thank you foryour attention!