climbing an infinite ladder 1 suppose we have an infinite ladder: 1. we can reach the first rung of...

75
Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of the ladder, then we can reach the next rung. From (1), we can reach the first rung. Then by applying (2), we can reach the second rung. Applying (2) again, the third rung. And so on. We can apply (2) any number of times to reach any particular rung, no matter how high up. This example motivates proof by mathematical induction.

Upload: mae-wheeler

Post on 23-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Climbing an Infinite Ladder

1

Suppose we have an infinite ladder:1. We can reach the first rung of the ladder.2. If we can reach a particular rung of the ladder, then we can reach the next rung.

From (1), we can reach the first rung. Then by applying (2), we can reach the second rung. Applying (2) again, the third rung. And so on. We can apply (2) any number of times to reach any particular rung, no matter how high up.

This example motivates proof by mathematical induction.

Page 2: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Principle of Mathematical Induction

Principle of Mathematical Induction: To prove that P(n) is true for all positive integers n, we complete these steps:– Basis Step: Show that P(1) is true.– Inductive Step: Show that P(k) → P(k + 1) is true for all positive integers k.

To complete the inductive step, assuming the inductive hypothesis that P(k) holds for an arbitrary integer k, show that must P(k + 1) be true.

Climbing an Infinite Ladder Example:

– BASIS STEP: By (1), we can reach rung 1.– INDUCTIVE STEP: Assume the inductive hypothesis that we can reach rung

k. Then by (2), we can reach rung k + 1. Hence, P(k) → P(k + 1) is true for all positive integers k. We can reach

every rung on the ladder.

2

Page 3: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Important Points About Using Mathematical Induction

• Mathematical induction can be expressed as the rule of inference

where the domain is the set of positive integers.• In a proof by mathematical induction, we don’t assume

that P(k) is true for all positive integers! We show that if we assume that P(k) is true, then P(k + 1) must also be true.

• Proofs by mathematical induction do not always start at the integer 1. In such a case, the basis step begins at a starting point b where b is an integer.

3

(P(1) ∧ ∀k (P(k) → P(k + 1))) → ∀n P(n),

Page 4: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Proving a Summation Formula by Mathematical Induction

Example: Show that: Solution:– BASIS STEP: P(1) is true since 1(1 + 1)/2 = 1.– INDUCTIVE STEP: Assume true for P(k).

The inductive hypothesis is Under this assumption,

4

Note: Once we have this conjecture, mathematical induction can be used to prove it correct.

Page 5: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Proving Inequalities

Example: Use mathematical induction to prove that n < 2n for all positive integers n.

Solution: Let P(n) be the proposition that n < 2n.

– BASIS STEP: P(1) is true since 1 < 21 = 2.– INDUCTIVE STEP: Assume P(k) holds, i.e., k < 2k, for an

arbitrary positive integer k.– Must show that P(k + 1) holds. Since by the inductive

hypothesis, k < 2k, it follows that: k + 1 < 2(k + 1) ≤ 2k * 21 = 2 ∙ 2k = 2k+1

Therefore n < 2n holds for all positive integers n.

Page 6: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

6

Page 7: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Strong Induction and Well-Ordering

Section 5.2

7

Page 8: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Strong Induction

• Strong Induction: To prove that P(n) is true for all positive integers n, where P(n) is a propositional function, complete two steps:– Basis Step: Verify that the proposition P(1) is true.– Inductive Step: Show the conditional statement

[P(1) ∧ P(2) ∧∙∙∙ ∧ P(k)] → P(k + 1) holds for all positive integers k.

8

Strong Induction is sometimes called the second principle of mathematical induction or complete induction.

Page 9: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Strong Induction and the Infinite Ladder

9

Strong induction tells us that we can reach all rungs if:1. We can reach the first rung of the ladder.2. For every integer k, if we can reach the first k rungs, then we can reach the (k + 1)st rung. To conclude that we can reach every rung by strong induction:• BASIS STEP: P(1) holds• INDUCTIVE STEP: Assume P(1) ∧ P(2) ∧∙∙∙ ∧ P(k) holds for an arbitrary integer k, and show that P(k + 1) must also hold.We will have then shown by strong induction that for every positive integer n, P(n) holds, i.e., we can reach the nth rung of the ladder.

Page 10: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Which Form of Induction Should Be Used?

• We can always use strong induction instead of mathematical induction. But there is no reason to use it if it is simpler to use mathematical induction.

• In fact, the principles of mathematical induction, strong

induction, and the well-ordering property are all equivalent.

• Sometimes it is clear how to proceed using one of the three methods, but not the other two.

10

Page 11: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Proof using Strong Induction

Example: Prove that every amount of postage of 12 cents or more can be formed using just 4-cent and 5-cent stamps.

Page 12: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Proof using Strong InductionSolution:

Let P(n) be the proposition that postage of n cents can be formed using 4-cent and 5-cent stamps.

BASIS STEP: P(12), P(13), P(14), and P(15) hold.• P(12) uses three 4-cent stamps.• P(13) uses two 4-cent stamps and one 5-cent stamp.• P(14) uses one 4-cent stamp and two 5-cent stamps.• P(15) uses three 5-cent stamps.

INDUCTIVE STEP: • The inductive hypothesis states that P(j) holds for 12 ≤ j ≤ k,

where k ≥ 15. Assuming the inductive hypothesis, it can be shown that P(k + 1) holds.

• Using the inductive hypothesis, P(k − 3) holds since k − 3 ≥ 12. To form postage of k + 1 cents, add a 4-cent stamp to the postage for k − 3 cents.

Hence, P(n) holds for all n ≥ 12.

12

Page 13: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Well-Ordering Property

• Well-ordering property: Every nonempty set of nonnegative integers has a least element.

The well-ordering property can be used directly in proofs, as the next example illustrates.

– Definition: A set is well ordered if every subset has a least

element.• N is well ordered under ≤.• The set of finite strings over an alphabet using lexicographic ordering

is well ordered.– We will see a generalization of induction to sets other than the

integers in the next section.

13

Page 14: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Definitions

The principle of mathematical induction: the statement ∀n P(n), is true if P(1) is true and ∀k (P(k) → P(k + 1)) is true.

Basis step: the proof of P(1) in a proof by mathematical induction of ∀n P(n). Inductive step: the proof of P(k) → P(k + 1) for all positive integers k

in a proof by mathematical induction of ∀n P(n).Strong induction: the statement ∀n P(n) is true if P(1) is true and ∀k [(P(1) ∧ P(2) ∧∙∙∙ ∧ P(k)) → P(k + 1) ].Well-ordering property: every nonempty set of nonnegative

integers has a least element.

14

Page 15: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursively Defined Functions

Definition: A recursive or inductive definition of a function consists of two steps.– BASIS STEP: Specify the value of the function at

zero.– RECURSIVE STEP: Give a rule for finding its value

at an integer from its values at smaller integers.

• A function f(n) is the same as a sequence a0, a1, … , where ai, where f(i) = ai.

15

Page 16: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursively Defined Functions

Example: Suppose f is defined by:

f(0) = 3, f(n + 1) = 2f(n) + 3

Find f(1), f(2), f(3), f(4)

Solution:f(1) = 2f(0) + 3 = 2∙3 + 3 = 9f(2) = 2f(1) + 3 = 2∙9 + 3 = 21f(3) = 2f(2) + 3 = 2∙21 + 3 = 45f(4) = 2f(3) + 3 = 2∙45 + 3 = 93

16

Page 17: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursively Defined Functions

Example: Give a recursive definition of the factorial function n!:

Solution:f(0) = 1f(n + 1) = (n + 1) * f(n)

17

Page 18: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursively Defined Functions

Example: Give a recursive definition of:

Solution: The first part of the definition is The second part is

18

Page 19: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursively Defined Sets and Structures

Recursive definitions of sets have two parts:– The basis step specifies an initial collection of elements.– The recursive step gives the rules for forming new elements in

the set from those already known to be in the set.• Sometimes the recursive definition has an exclusion rule,

which specifies that the set contains nothing other than those elements specified in the basis step and generated by applications of the rules in the recursive step.

• We will always assume that the exclusion rule holds, even if it is not explicitly mentioned.

• We will later develop a form of induction, called structural induction, to prove results about recursively defined sets.

19

Page 20: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Structural Induction and Binary Trees Theorem:

If T is a full binary tree, then n(T) ≤ 2h(T)+1 – 1. Proof:

Use structural induction.BASIS STEP: The result holds for a full binary tree consisting only of a

root, n(T) = 1 and h(T) = 0. Hence, n(T) = 1 ≤ 20+1 – 1 = 1.RECURSIVE STEP: Assume n(T1) ≤ 2h(T1)+1 – 1 and also n(T2) ≤ 2h(T2)+1 – 1

whenever T1 and T2 are full binary trees.

20

n(T) = 1 + n(T1) + n(T2) (by recursive formula of n(T))

≤ 1 + (2h(T1)+1 – 1) + (2h(T2)+1 – 1) (by inductive hypothesis)

≤ 2∙max(2h(T1)+1, 2h(T2)+1 ) – 1 = 2∙2max(h(T1),h(T2))+1 – 1 (max(2x, 2y)= 2max(x,y) ) = 2∙2h(t) – 1 (by recursive

definition of h(T)) = 2h(t)+1 – 1

− 2 .

Page 21: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Generalized Induction

• Generalized induction is used to prove results about sets other than the integers that have the well-ordering property.

• For example, consider an ordering on N ⨉ N, ordered pairs of nonnegative integers.

Specify that (x1, y1) is less than or equal to (x2, y2) if either x1 < x2, or x1 = x2 and y1 <y2 .

This is called the lexicographic ordering.• Strings are also commonly ordered by a lexicographic

ordering.• The next example uses generalized induction to prove a result

about ordered pairs from N ⨉ N.

21

Page 22: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Generalized Induction Example:

Suppose that am,n is defined for (m,n)∊N × N by a0,0 = 0 and

Show that am,n = m + n(n + 1)/2 is defined for all (m,n)∊N × N. Solution:

Use generalized induction.BASIS STEP: a0,0 = 0 = 0 + (0∙1)/2INDUCTIVE STEP: Assume that am ̍,n ̍ = m ̍+ n ̍(n ̍ + 1)/2 whenever(m ̍,n ̍) is

less than (m,n) in the lexicographic ordering of N × N . • If n = 0, by the inductive hypothesis we can conclude am,n = am−1,n + 1 = m − 1+ n(n + 1)/2 + 1 = m + n(n + 1)/2 .• If n > 0, by the inductive hypothesis we can conclude am,n = am−1,n + 1 = m + n(n − 1)/2 +n = m + n(n + 1)/2 .

22

− 2 .

Page 23: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursive Algorithms

Definition: An algorithm is called recursive if it solves a

problem by reducing it to an instance of the same problem with smaller input.

For the algorithm to terminate, the instance of the problem must eventually be reduced to some initial case for which the solution is known.

23

Page 24: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursive Factorial Algorithm

Example: Give a recursive algorithm for computing n!,

where n is a nonnegative integer. Solution:

Use the recursive definition of the factorial function.

24

procedure factorial(n: nonnegative integer)

if n = 0 then return 1else return n∙factorial(n − 1){output is n!}

Page 25: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursive Binary Search Algorithm

Example: Construct a recursive version of a binary search algorithm.

Solution: Assume we have a1,a2,…, an, an increasing sequence of integers. Initially i is 1 and j is n. We are searching for x.

25

procedure binary search(i, j, x : integers, 1≤ i ≤ j ≤n)m := ⌊(i + j)/2⌋if x = am then return melse if (x < am and i < m) then return binary search(i,m−1,x)else if (x > am and j >m) then return binary search(m+1,j,x)else return 0{output is location of x in a1, a2,…,an if it appears, otherwise 0}

Page 26: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Proving Recursive Algorithms Correct

Both mathematical and strong induction are useful techniques to show that recursive algorithms always produce the correct output.

Example: Prove that the algorithm for computing the powers of real numbers is correct.

Solution: Use mathematical induction on the exponent n. BASIS STEP: a0 =1 for every nonzero real number a, and power(a,0) = 1. INDUCTIVE STEP: The inductive hypothesis is that power(a,k) = ak, for all a ≠0. Assuming the

inductive hypothesis, the algorithm correctly computes ak+1, Since power(a,k + 1) = a∙ power (a, k) = a∙ ak = ak+1 .

26

procedure power(a: nonzero real number, n: nonnegative integer)

if n = 0 then return 1else return a∙ power (a, n − 1){output is an}

− 2 .

Page 27: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

DefinitionsRecursive definition of a function: a definition of a function that

specifies an initial set of values and a rule for obtaining values of this function at integers from its values at smaller integers.

Recursive definition of a set: a definition of a set that specifies an initial set of elements and a rule for obtaining other elements from those in the set.

Structural induction: a technique for proving results about recursively defined sets.

Recursive algorithm: an algorithm that proceeds by reducing a problem to the same problem with smaller input.

Merge sort: a sorting algorithm that sorts a list by splitting it in two, sorting each of the two resulting lists, and merging the results into a sorted list.

Iteration: a procedure based on the repeated use of operations in a loop.

27

Page 28: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursively Defined Functions

Example: Give a recursive definition of the factorial function n!:

Solution:f(0) = 1f(n + 1) = (n + 1) * f(n)

28

Page 29: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursive Algorithms

Definition: An algorithm is called recursive if it solves a

problem by reducing it to an instance of the same problem with smaller input.

For the algorithm to terminate, the instance of the problem must eventually be reduced to some initial case for which the solution is known.

29

Page 30: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursive Factorial Algorithm

Example: Give a recursive algorithm for computing n!,

where n is a nonnegative integer. Solution:

Use the recursive definition of the factorial function.

30

procedure factorial(n: nonnegative integer)

if n = 0 then return 1else return n∙factorial(n − 1){output is n!}

Page 31: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Recursive Binary Search Algorithm

Example: Construct a recursive version of a binary search algorithm.

Solution: Assume we have a1,a2,…, an, an increasing sequence of integers. Initially i is 1 and j is n. We are searching for x.

31

procedure binary search(i, j, x : integers, 1≤ i ≤ j ≤n)m := ⌊(i + j)/2⌋if x = am then return melse if (x < am and i < m) then return binary search(i,m−1,x)else if (x > am and j >m) then return binary search(m+1,j,x)else return 0{output is location of x in a1, a2,…,an if it appears,

otherwise 0}

Page 32: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Proving Recursive Algorithms Correct

Both mathematical and strong induction are useful techniques to show that recursive algorithms always produce the correct output.

Example: Prove that the algorithm for computing the powers of real numbers is correct.

Solution: Use mathematical induction on the exponent n. BASIS STEP: a0 = 1 for every nonzero real number a, and power(a, 0) = 1. INDUCTIVE STEP: The inductive hypothesis is that power(a,k) = ak, for all a ≠ 0. Assuming the

inductive hypothesis, the algorithm correctly computes ak+1, Since power(a, k + 1) = a ∙ power (a, k) = a ∙ ak = ak+1 .

32

procedure power(a: nonzero real number, n: nonnegative integer)

if n = 0 then return 1else return a∙ power (a, n − 1){output is an}

− 2 .

Page 33: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

The Relationship between (mod m) and mod m Notations

• The use of “mod” in a ≡ b (mod m) and a mod m = b are different.– a ≡ b (mod m) is a relation on the set of integers.– In a mod m = b, the notation mod denotes a

function.• The relationship between these notations is made

clear in this theorem.• Theorem 3:

Let a and b be integers, and let m be a positive integer. Then a ≡ b (mod m) if and only if a mod m = b mod m.

Page 34: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Base b Representations

We can use positive integer b greater than 1 as a base, because of this theorem:

Theorem 1: 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. The aj, j = 0,…,k are called the base-b digits of the representation.

The representation of n given in Theorem 1 is called the base b expansion of n and is denoted by (akak-1….a1a0)b.

We usually omit the subscript 10 for base 10 expansions.

Page 35: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Binary Expansions Most computers represent integers and do

arithmetic with binary (base 2) expansions of integers. In these expansions, the only digits used are 0 and 1.

Example: What is the decimal expansion of the integer that has (101011111)2 as its binary expansion?

Solution: (1 0101 1111)2 = 1∙28 + 0∙27 + 1∙26 + 0∙25 + 1∙24 + 1∙23 + 1∙22 + 1∙21 + 1∙20 =351.

Page 36: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Binary ExpansionsExample:

What is the decimal expansion of the integer that has (11011)2 as its binary expansion?

Solution: (11011)2 = 1 ∙24 + 1∙23 + 0∙22 + 1∙21 + 1∙20 =27.

Page 37: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Octal Expansions The octal expansion (base 8) uses the digits

{0,1, 2, 3, 4, 5, 6, 7}. Example:

What is the decimal expansion of the number with octal expansion (7016)8 ?

Solution: 7∙83 + 0∙82 + 1∙81 + 6∙80 = 3598

Page 38: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Octal ExpansionsExample:

What is the decimal expansion of the number with octal expansion (111)8 ?

Solution: 1∙82 + 1∙81 + 1∙80 = 64 + 8 + 1 = 73

Page 39: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Hexadecimal Expansions

The hexadecimal expansion needs 16 digits, but our decimal system provides only 10.

So letters are used for the additional symbols. The hexadecimal system uses the digits {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}.

The letters A through F represent the decimal numbers 10 through 15.

Example: What is the decimal expansion of the number with hexadecimal expansion (2AE0B)16 ?

Solution: 2∙164 + 10∙163 + 14∙162 + 0∙161 + 11∙160 =175627

Page 40: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Hexadecimal Expansions

Example: What is the decimal expansion of the number with

hexadecimal expansion (1E5)16 ?

Solution: 1∙162 + 14∙161 + 5∙160 = 256 + 224 + 5 = 485

Page 41: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Base ConversionTo construct the base b expansion of an integer n:

Divide n by b to obtain a quotient and remainder.n = bq0 + a0 0 ≤ a0 ≤ b

The remainder, a0 , is the rightmost digit in the base b expansion of n. Next, divide q0 by b.

q0 = bq1 + a1 0 ≤ a1 ≤ bThe remainder, a1, is the second digit from the right in

the base b expansion of n.Continue by successively dividing the quotients by b,

obtaining the additional base b digits as the remainder. The process terminates when the quotient is 0.

continued →

Page 42: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Base Conversion Example:

Find the octal expansion of (12345)10

Solution:

Successively dividing by 8 gives:– 12345 = 8 ∙ 1543 + 1– 1543 = 8 ∙ 192 + 7– 192 = 8 ∙ 24 + 0– 24 = 8 ∙ 3 + 0– 3 = 8 ∙ 0 + 3

The remainders are the digits from right to left yielding (30071)8.

Page 43: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Comparison of Hexadecimal, Octal, and Binary Representations

Each octal digit corresponds to a block of 3 binary digits.Each hexadecimal digit corresponds to a block of 4 binary digits. So, conversion between binary, octal, and hexadecimal is easy.

Initial 0s are not shown

Page 44: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Conversion Between Binary, Octal, and Hexadecimal Expansions

Example: Find the octal and hexadecimal expansions of (11 1110 1011 1100)2.

Solution: To convert to octal, we group the digits into blocks of three

(011 111 010 111 100)2, adding initial 0s as needed. The blocks from left to right correspond to the digits 3, 7, 2, 7, and 4. Hence, the solution is (37274)8.

To convert to hexadecimal, we group the digits into blocks of four (0011 1110 1011 1100)2, adding initial 0s as needed. The blocks from left to right correspond to the digits 3, E, B, and C. Hence, the solution is (3EBC)16.

Page 45: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Primes

Definition: A positive integer p greater than 1 is called prime if the only positive factors of p are 1 and p. A positive integer that is greater than 1 and is not prime is called composite.

Example: The integer 7 is prime because its only positive factors are 1 and 7, but 9 is composite because it is divisible by 3.

Page 46: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

The Fundamental Theorem of Arithmetic

Theorem: Every positive integer greater than 1 can be written uniquely as a prime or as the product of two or more primes where the prime factors are written in order of nondecreasing size.

Examples:– 100 = 2 ∙ 2 ∙ 5 ∙ 5 = 22 ∙ 52 – 641 = 641– 999 = 3 ∙ 3 ∙ 3 ∙ 37 = 33 ∙ 37 – 1024 = 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 ∙ 2 = 210

Page 47: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Greatest Common Divisor

Definition: Let a and b be integers, not both zero. The largest integer d such that d | a and also d | b is called the greatest common divisor of a and b. The greatest common divisor of a and b is denoted by gcd(a, b).

One can find greatest common divisors of small numbers by inspection.

Example:What is the greatest common divisor of 24 and 36?

Solution: gcd(24, 26) = 12

Example:What is the greatest common divisor of 17 and 22?

Solution: gcd(17, 22) = 1

Page 48: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Greatest Common Divisor

Definition: The integers a and b are relatively prime if

their greatest common divisor is 1. Example: 17 and 22

Page 49: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Greatest Common Divisor

Definition: The integers a1, a2, …, an are pairwise relatively prime if

gcd(ai, aj)= 1 whenever 1 ≤ i<j ≤n.

Example: Determine whether the integers 10, 17, and 21 are pairwise relatively prime.

Solution: Because gcd(10, 17) = 1, gcd(10, 21) = 1, and gcd(17,21) = 1, 10, 17, and 21 are pairwise relatively prime.

Page 50: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Greatest Common Divisor

Example: Determine whether the integers 10, 19, and 24 are pairwise relatively prime.

Solution: Because gcd(10, 24) = 2, 10, 19, and 24 are not pairwise relatively prime.

Page 51: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Least Common Multiple 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.

It is denoted by lcm(a,b).The least common multiple can also be computed from the prime factorizations.

This number is divided by both a and b and no smaller number is divided by a and b. Example:

lcm(233572, 2433) = 2max(3,4) 3max(5,3) 7max(2,0) = 24 35 72The greatest common divisor and the least common multiple of two integers are

related by: Theorem 5:

Let a and b be positive integers. Then ab = gcd(a,b) ∙ lcm(a,b) (proof is Exercise 31)

Page 52: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Euclidean Algorithm

The Euclidian algorithm is an efficient method for computing the greatest common divisor of two integers. It is based on the idea that gcd(a,b) is equal to gcd(a,c) when a > b and c is the remainder when a is divided by b.

Example: Find gcd(91, 287):• 287 = 91 ∙ 3 + 14• 91 = 14 ∙ 6 + 7• 14 = 7 ∙ 2 + 0

gcd(287, 91) = gcd(91, 14) = gcd(14, 7) = 7

Euclid (325 B.C.E. – 265 B.C.E.)

Stopping condition

Divide 287 by 91Divide 91 by 14Divide 14 by 7

continued →

Page 53: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Euclidean Algorithm

• The Euclidean algorithm expressed in pseudocode is:

• The time complexity of the algorithm is O(log b), where a > b.

procedure gcd(a, b: positive integers)x := ay := bwhile y ≠ 0 r := x mod y x := y y := rreturn x {gcd(a,b) is x}

Page 54: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Cryptographic Protocols: Key Exchange

Cryptographic protocols are exchanges of messages carried out by two or more parties to achieve a particular security goal.

Key exchange is a protocol by which two parties can exchange a secret key over an insecure channel without having any past shared secret information.

Page 55: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Product Rule

The Product Rule: A procedure can be broken down into a sequence of i

tasks. There are n1 ways to do the first task

and n2 ways to do the second task

and n… ways to do the …th task

and ni ways to do the ith task

Then there are n1 n∙ 2 …∙ n∙ i ways to do the procedure.

Page 56: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

The Product Rule

Example: How many different license plates can be made if each plate contains a

sequence of three uppercase English letters followed by three digits?

Solution: By the product rule, there are 26 ∙ 26 ∙ 26 ∙ 10 ∙ 10 ∙ 10 n1 ∙ n2 ∙ n3 ∙ n4 ∙ n5 ∙ n6

= 17,576,000 different possible license plates.

Page 57: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Basic Counting Principles: The Sum Rule

The Sum Rule:

If a task can be done either in one of n1 ways or in one of n2 ways to do the task, where none of the set of n1 ways is the same as any of the n2 ways, then there are n1 + n2 ways to do the task.

Example: The mathematics department must choose either a student or a faculty member as a representative for a university committee. How many choices are there for this representative if there are 37 members of the mathematics faculty and 83 mathematics majors and no one is both a faculty member and a student.

Solution: By the sum rule it follows that there are 37 + 83 = 120 possible ways to pick a representative.

Page 58: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Counting Functions

Counting Functions: How many functions are there from a set with m elements

to a set with n elements?

Questions to ask oneself:What is a function? One item from domain to one in codomain.How many elements in domain; in codomain?For each item in the domain, how many choices can you make in the

codomain?

Page 59: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Combining the Sum and Product Rule

Example: Suppose statement labels in a programming language can be either a single letter or a letter followed by a digit. Find the number of possible labels.

Solution: Use the product rule.

26 + 26 ∙ 10 = 286

Page 60: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Counting Functions

Solution: Since a function represents a choice of one

of the n elements of the codomain for each of the m elements in the domain, the product rule tells us that there are n ∙ n ∙ ∙ ∙ n = nm such functions.

nm = codomaindomain (function)

Page 61: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Each function is a unique choice from an element in m to an element in n.

But – each choice in m may select the any element in n.

Page 62: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Choices

Page 63: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Other questions

Other questions to ask:Is it a combination?

what is a combination?2 types

combination of product and sum rulescombination of r permutations

Is it a permutation?Is repetition allowed?What is repetition?Are the objects distinguishable?

Example: the S in word SUCCESS. (combination of 4 permutations)

C(7,3)C(4,2)C(2,1)C(1,1) = 420

Page 64: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Is it a combination?

Example: Suppose statement labels in a programming language can be either a single letter or a letter followed by a digit. Find the number of possible labels.

Solution: Use the product rule.

26 + 26 ∙ 10 = 286

Page 65: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Is it a permutation?

A permutation of a set of distinct objects is an ordered arrangement of these objects.

Example: Let S = {1,2,3}. – The ordered arrangement 3,1,2 is a permutation of S.

Example: How many ways are there to select a first-prize winner, a second prize winner, and a third-prize winner from 100 different people who have entered a contest?

P(100,3) = 100 ∙ 99 ∙ 98 = 970,200

Page 66: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

r-permuation

An ordered arrangement of r elements of a set is called an r-permuation.

Example: Let S = {1,2,3}. – The ordered arrangement 3, 2 is a 2-permutation of S.

Page 67: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

A Formula for the Number of Permutations

The number of r-permuatations of a set with n elements is denoted by P(n,r).

What is n? The number of elements in a set.

What is r? The number of elements selected.

Page 68: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Combinations

An r-combination of elements of a set is an unordered selection of r elements from the set.

Thus, an r-combination is simply a subset of the set with r elements.

The number of r-combinations of a set with n distinct elements is denoted by C(n, r). (Also (n!/(n-r)!)

The notation is also used and is called a binomial coefficient.

Page 69: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Combinations

Example: Let S be the set {a, b, c, d}. Then {a, c, d} is a 3-combination from S. It is the same as {d, c, a} since the order listed does not matter.C(4,2) = 6 because the 2-combinations of {a, b, c, d} are the six subsets {a, b}, {a, c}, {a, d}, {b, c}, {b, d}, and {c, d}.

Page 70: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Combinations

Example: How many poker hands of five cards can be dealt from

a standard deck of 52 cards? Also, how many ways are there to select 47 cards

from a deck of 52 cards? Solution: Since the order in which the cards are dealt does not

matter, the number of five card hands is:

(n = 52, r = 5)

Page 71: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Combinations

C(n, r) = C(n, n − r)

Page 72: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Permutations with Repetition

The number of r-permutations of a set of n objects with repetition allowed is nr.

Example: How many strings of length r can be formed from the uppercase letters of the English alphabet?

Solution: The number of such strings is 26r, which is the number of r-permutations of a set with 26 elements.

Page 73: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Combinations with Repetition

Choice = Example: How many ways are there to select

five bills from a box containing at least five of each of the following denominations: $1, $2, $5, $10, $20, $50, and $100? (7 ways)

n choices = 7 (the demominations) are indistinguishable!r ways = 5 (how many bills).

(must have enough bills to allow choice of any 5)

Page 74: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Indistinguishable Object Combinations

C(n + r – 1,r) = C(n + r – 1, n –1)C(ways + choices - 1, ways)C(7 + 5 – 1, 5) = C(7 + 5 – 1, 7 –1)C(11, 5) = C(11, 6)= 11!/(11-5)!*5!= 11!/6!*5!

Page 75: Climbing an Infinite Ladder 1 Suppose we have an infinite ladder: 1. We can reach the first rung of the ladder. 2. If we can reach a particular rung of

Indistinguishable Object Combinations

Example: Suppose that a cookie shop has four different kinds of cookies. How many different ways can six cookies be chosen?

Solution: The number of ways to choose six cookies is the number of 6-combinations of a set with four elements.

C(n + r – 1, r) = C(6 + 4 -1, 4) = C(9, 4)= 9!/4!*5! is the number of ways to choose six cookies from the four kinds.

Incorrect =