<<

CSE 1400/MTH 2051 Discrete Math Notes William David Shoaff February 3, 2018

Preliminaries

• Read and understand the syllabus • Use Canvas to communicate and review grades • Use my page (cs.fit.edu/~wds/classes/adm) for supplemental material • Follow the calendar: Listen for announced calendar changes. • Recitation sessions: Lead by Zubin Kadva, is scheduled on TBA from TBA to TBA in TBA • Edward Tufte recommends this lecture structure: – Provide audience 1 to 2 pages of notes – Allow audience 10 to 20 minutes to read notes – Glare at those who are not reading (his wry sense of humor?) – While fleshing-out notes ask and answer questions • I’m going to change Tufte’s formula a . – I’ve provided the daily notes here – Students shall read a few pages ahead before class – Students shall develop a list of questions – Students shall bring notes and questions to class

Contents Induction 29 Recursion 35 Preliminaries 1 2 Functions 39 Conversions Among Systems 4 The Pigeonhole Principle 48 Negative Numbers 6 Relations 50 Rational Numbers 12 Equivalences 54 Truth 17 Orders 57 Sets 21 Modular 59 Rules of Inference 23 Mathematical Proof 66 First-Order Logic 23 Paradoxes 71 Sequences 25 Questions and Problems 73 cse 1400/mth 2051 discrete math notes 2

Numbers

Numbers are very useful. Numbers can be written in many different languages. The language, base 10, is the lingua franca for common conversations. The decimal alphabet is

D = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} called digits

These digits provide coefficients for powers of 10, where the power p is determined by the position of the 10. An example should help:

314.15 = 3 · 102 + 1 · 101 + 4 · 100 + 1 · 10−1 + 5 · 10−2

The syntax (position) of the digit determines the semantics (meaning) of the expression. A breakthrough occurred in the late first-half of the twentieth century when electronic com- puters were built. The design of these machines made the binary language, base 2, important. The binary alphabet is B = {0, 1} called These bits provide coefficients for powers of 2. The power p is positional. (Do you find it interesting that 2 is

written as (10)2 in base 2?, I do, there is a pattern. 16 written in base 16 is (10)16.) When written in binary, the value 314.15 is

=  314.15 1 0011 1010.001 0011 0011 2 where 0011 repeats without bound One goal of the class is to master techniques for conversions between these two languages. First, agree that by arithmetic, 8 5 4 3 1 314 = (1 0011 1010)2 = 2 + 2 + 2 + 2 + 2 Negative numbers and of will be studied later. Numbers written in binary are long, about lg 10 ≈ 3.3219 ··· longer than decimal. To write shorter strings, use larger alphabets. The early 70’s Intel 4004 CPU was a 4-bit chip. Four bits are sometimes called a nybble. Four bits define digits (hexits). The hexadecimal language, base 16 uses the alphabet

H = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, , , E, F} called hexits

The letters A to F stand for the numbers 10 to 15. Now the value equivalence is

2 314 = (13A)16 = 16 + 3 · 16 + 10

Numbers written in hexadecimal are about 4 = lg 16 times shorter than numbers written in binary and about 1.204 · · · ≈ log10 16 times shorter than numbers written in decimal. The generic name for elements in numerical alphabets such as B, D, H is numerals.

Exponential – Logarithmic Map between Value and String Length

There is an exponential – logarithmic relationship between the value of a number n and the length of its representation in a given base. Approximately logb n numerals are needed to write (n)b. The exact formula is   logb n + 1 = numerals required to write n in base b. cse 1400/mth 2051 discrete math notes 3

Examples provide a start to understanding:   log10 314 + 1 = b≈ 2.4 ···c + 1 = 3 the number of digits to write 314.

blog2 314c + 1 = b≈ 8.2946 ···c + 1 = 9 the number of digits to write 314.

Nothing is important! Starting from nothing, zero, you can count up to 10k − 1 using k digits. For

instance, you can count from 0 to 99 using at most 2 digits. Similarly, you can count from 0 to (111)2 using at most 3 bits. As a general statement: You can count from 0 to bk − 1 using at most k base b numerals.

Historical number systems are interesting. The system, base 60, remains in use today. Babylonians made sexagesimal famous. They learned it from the Sumerians. (This was about 4000 to 5000 years ago) There are 60 minutes in an hour; 60 seconds in a minute. There are 360 degrees in a circle, 60 minutes of arc in a . The number 60 = 223151 is highly composite: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60 all divide 60. Properties of and composite numbers are studied in number theory. A topic with inter- esting uses. Given the prime of n, being able to count n’s is a useful skill too. I find it interesting that the Romans are known for their engineering, but their number system is infa- mous for calculations. The Roman alphabet is {I, V, X, L, C, D, M}. Arcane rules define the value interpretation (semantics) from the format (syntax) of a number written using . Finally, as a teacher, I like to ask questions about numbers in arbitrary bases, say base 3 or 11, to test if students understand the ideas presented here.

Questions and Problems

1. What is the alphabet for base 3, ternary numbers? 2. What is the alphabet for base 8, numbers? 3. If you were to use digits and lower case English alphabet to represent numbers, what would the base be? 4. Are Roman numerals a positional number system?

5.L ogarithms are important! The expression lg n stands for the log base 2 of n. What are the values of lg 1, lg 2, lg 16, lg 256? 6. What does it mean to say a number p is prime? 7. What does it mean to say a number c is composite? 8. What does it mean to say a is a of n? 9. Describe the prime factorization of a number.

10.L anguages are important! How would you define language? Syntax and semantics are words about languages. How would you define syntax and how would you define semantics? cse 1400/mth 2051 discrete math notes 4

Conversions Among Number Systems

Language translation is an interesting topic. Natural language translation remains difficult, but significant progress is being made. Translations between number systems is algorithmic. There is one efficient , and its inverse. The algorithm is known as Horner’s rule, but everyone knows her as synthetic division. To be sure, there are other conversion and efficiencies in special cases.

Horner’s rule

Horner’s rule converts numbers written in base b into numbers written base 10 decimal . You are proficient in base 10 decimal arithmetic. By hand, Horner’s rule is used to change base b numbers into base 10 (decimal) numbers. Here are three examples:

Show that the (0010 1010)2 = 42.

Horner’s Rule @ x = 2 0 0 1 0 1 0 1 0 0 0 2 4 10 20 42 0 0 1 2 5 10 21 42

Show that the binary number (1100 1100)2 = 204.

Horner’s Rule @ x = 2 1 1 0 0 1 1 0 0 2 6 12 24 50 102 204 1 3 6 12 25 51 102 204

Show that the octal number (237)8 = 159.

Horner’s Rule @ x = 8 2 3 7 16 152 2 19 159

Horner’s schema can be expressed by the code below. The code evaluates the

n n−1 p(x) = an x + an−1 x + ··· + a1 x + a0 at the base x = b. That is, the code computes the value

p(b) = (an an−1 ··· a1 a0 )b horner : : (Num a ) => a −> [ a ] −> a horner b [ ] = 0 horner b [a] = a horner b [a:a1 ] = a *b + a1 horner b [a:a1 :as] = horner b [a *b+a1 : as ] cse 1400/mth 2051 discrete math notes 5

If you pretend numerals are stored in little-endian order, that is reversed of the usual order, so the less sig-

nificant numeral is first: (1234)le = 4321, then replace the line horner x [b:c:ds] = horner x [b*x+c:ds] with horner x (b:bs) = b + x * (horner x bs) What’s clear from the code is: It takes n multiplies and n adds to convert a number (a0a1 ··· an − 1an)b to decimal.

Inverting Horner’s rule

Horner’s rule uses ; it should not be surprising that its inverse uses divi- sion. By hand, the inverse of Horner’s rule converts a decimal number to some other base. Here are three examples: • Convert the decimal number 161 to binary.

Repeated Remaindering mod 2 Quotients 161 80 40 20 10 5 2 1 Remainders 1 0 0 0 0 1 0 1

Therefore 161 = (1010 0001)2. • Convert the decimal number 237 to binary.

Repeated Remaindering mod 2 Quotients 237 118 59 29 14 7 3 1 Remainders 1 0 1 1 0 1 1 1

Therefore 237 = (1110 1101)2. • Convert the decimal number 73 to octal.

Repeated Remaindering mod 8 Quotients 73 9 1 Remainders 1 1 1

Therefore 73 = (111)8. Inverting Horner’s schema (repeated remaindering) can be expressed by the code below. The code con- verts a decimal number a into its representation as a list of base b numerals. remaindering :: ( I n t e g r a l a ) => a −> a −> [ a ] remaindering b 0 = [ ] remaindering b a = rem a b : remaindering b ( quot a b )

Questions and Problems

1. Choose some small numbers, 2 or 3 digits, and convert them to binary or octal. 2. Choose some small binary or octal numbers, and convert them to decimal cse 1400/mth 2051 discrete math notes 6

Binary – Octal – Hexadecimal Conversion

Powers of 2 form the basis for binary numbers. The basis for hexadecimal numbers are powers of 16, and 16 = 24 is also a power of 2. Because of this, grouping or expanding by 4’s leads to efficient conversions. For instance,

(2ABF)16 = (0010)2 (1010)2 (1011)2 (1111)2 = (0010 1010 1011 1111)2

Questions and Problems

1. Test your ability: Write several 4 bit numbers. Convert them to decimal. Convert them to hexadecimal. 2. What is the meaning of the word basis in the phrase: “The basis for binary numbers are powers of 2.”? 3. Is it right-to-left or left-to-right grouping used when converting binary to hexadecimal? Does it matter? 4. Describe algorithms for convert binary to octal and octal to binary.

Negative Numbers

Not everything is positive. The natural numbers

N = {0, 1, 2, . . .}

are useful for addition (and multiplication): Addition (and multiplication) are closed on N, but subtraction is not closed on N. To support subtraction, the naturals are expanded to integers.

Z = {0, ±1, ±2, . . .}

Except for 0, every has a + or − , and its sign must be encoded somehow. The usual rule for subtraction is arcane:  a − b if a ≥ b a − b = −(b − a) otherwise

For instance 10 − 5 = 5 and 5 − 10 = −(10 − 5). To avoid these cases an alternative number representation can be used.

Ten’s Complement

Ten’s complement is introduced to help understand two’s complement. Ten’s complement num- bers represent a range of integers. The number of digits used determines the range. • One digit can represent [−5, 4]: The ten’s complement system uses 0 to 4 as normal and uses 5 to 9 to represent −5 to −1. That is,

(5)10c = 5 − 10 = −5 (6)10c = 6 − 10 = −4 = − (4)10c (7)10c = 7 − 10 = −3 − (3)10c

(8)10c = 8 − 10 = −2 − (2)10c (9)10c = 9 − 10 = −1 − (1)10c cse 1400/mth 2051 discrete math notes 7

• Two digits can represent [−50, 49]: The numbers (00)10c to (49)10c have their normal decimal values. And larger numbers are complemented by 100.

(50)10c = 50 − 100 = −50, (51)10c = 51 − 100 = −49, . . . , (98)10c = 98 − 100 = −2, (99)10c = 99 − 100 = −1

For instance (73)10c = 73 − 100 = −27 and (77)10c = 77 − 100 = −13.

• Three digits can represent [−500, 499]: For 3-digit numbers, (000)10c to (499)10c have their normal deci- mal values. And larger numbers are complemented by 1000.

(500)10c = 500 − 1000 = −500, . . . , (999)10c = 999 − 1000 = −1

For instance (073)10c = 73 and (873)10c = 873 − 1000 = −127. And so on. The advantage of this system is there is no need to build circuits for subtraction, addition will do. In- stances are:

(73)10c + (87)10c = (60)10c = −40 (in a 2-digit system)

(073)10c + (873)10c = (946)10c = −54 ((073)10c = 73, (873)10c = −127)

(999)10c + (873)10c = (872)10c = −128 (in a 3-digit system) A disadvantage of every finite number representation is overflow:

(373)10c + (227)10c = (600)10c = −400 (wrong in 3-digits)

(0373)10c + (0227)10c = (0600)10c = 600 (right in 4-digits)

(573)10c + (627)10c = (200)10c = 200 (wrong in 3-digits)

(9573)10c + (9627)10c = (9200)10c = −800 (right in 4-digits) The length of a ten’s complement number can be padded to make it longer.

• For positive numbers, pad with 0’s on the left: (473)10c = (0473)10c = (00473)10c = ···

• For negative numbers, pad with 9’s on the left: (773)10c = (9773)10c = (99773)10c = ···

Questions and Problems

1. What are the decimal values of the ten’s complement numbers:

(79)10c , (39)10c , (790)10c , (390)10c

2. What are the decimal values of sums:

(79)10c + (39)10c , (790)10c + (390)10c

3. What does it mean to say an operation is closed on a set? 4. There are at least two visualization of 10’s complement numbers. (a) On the number line from 0 to 99, pick up and move (shift, translate) the values 50 to 99 before 0. This

aligns (99)10c with −1 and (50)10c with −50. Draw this. (b) Wrap the number line from 0 to 99 around itself into a circle connecting 99 and 0. Draw this. 5. How would you define the 4’s complement number system? cse 1400/mth 2051 discrete math notes 8

Two’s Complement

Two’s complement is the number system use to represent positive and negative whole numbers: The integers Z = {0, ±1, ±2, . . .}, signed whole numbers.

• One bit can represent −1 or 0: (0)2c = 0 (1)2c = 1 − 2 = −1 • Two bits can represent {−2, −1, 0, 1}:

(00)2c = 0 (01)2c = 1

(10)2c = 2 − 4 = −2 (11)2c = 3 − 4 = −1

• Three bits can represent {−4, −3, −2, −1, 0, 1, 2, 3}:

(000)2c = 0 (001)2c = 1 (010)2c = 2 (011)2c = 3

(100)2c = 4 − 8 = −4 (101)2c = 5 − 8 = −3 (110)2c = 6 − 8 = −2 (111)2c = 7 − 8 = −1

And so on. The rules for two’s complement numbers are:

• A number (n)2c that begin with 0 (on the left) has value (n)2.

(0111)2c = 7, (01 0111)2c = 23, (010 0111)2c = 39

d • A number (n)2c that begin with 1 (on the left) has value (n)2 − 2 where d is the number of bits in n.

4 6 7 (1111)2c = 15 − 2 = −1, (11 0111)2c = 55 − 2 = −9, (110 0111)2c = 103 − 2 = −25

Like ten’s complement, the advantage of the two’s complement system is only addition is required: There is no need to build special circuits or code for subtraction. Instances are:

(0101)2c + (0001)2c = (0110)2c = 6 ((0101)2c = 5, (0001)2c = 1)

(1100)2c + (0011)2c = (1111)2c = −1 ((1100)2c = −4, (0011)2c = 3)

(1101)2c + (0111)2c = (0100)2c = 4 ((1101)2c = −3, (0111)2c = 7)

Overflow is still a disadvantage. It can be overcome by building larger registers, more bits per word. Or, eventually in software because any physical finite word length will overflow. Software will sometimes fail too: Somethings cannot be described finitely.

(1001)2c + (1101)2c = (0110)2c = 6 (wrong in a 4-bit system)

(1 1001)2c + (1 1101)2c = (1 0110)2c = −10 (right in a 5-bit system)

(0100)2c + (0101)2c = (1001)10c = −7 (wrong in a 4-bit system)

(0 0100)2c + (0 0101)2c = (0 1001)2c = 9 (right in a 5-bit system)

Overflow occurs when the last carry-in and carry-out bits do not match. cse 1400/mth 2051 discrete math notes 9

Two’s Complement: More Details

Pretend numbers are written using 8-bits:Not too short and not too long. Here are some facts about 8-bit two’s complement numbers.

1. Numbers that start with 0 are positive: For instance, (0111 1010)2c = 122 is positive. 2. Positive integers from 0 to 127 can be written with 8 bits.

3. Numbers that start with 1 are negative: For instance (1000 0110)2c = −122 is negative. 4. Negative integers from −128 = 128 − 256 to −1 = 255 − 256 can be written with 8 bits.

It is easy to negate two’s complement numbers. A number and its negative sum to 0. For 8-bit, 8 two’s complement numbers, the sum a + (−a) = 256 = 2 = (1 0000 0000)2c, which is 0 to 8-bits. For instance, Carries 1 1 1 1 1 1 0 0 0 w = 1 0 0 1 1 1 0 0 −w = 0 1 1 0 0 1 0 0 Sum 1 0 0 0 0 0 0 0 0 To keep this invariant, given a two’s complement number a you can find −a using two steps: 1. Copy a’s bits from right-to-left up to and including the first 1. 2. Flip the remaining bits to the left. For instance, the negative of

flip copy ! (1000 1000)2c is 0111 1000 . 2c

A ninth bit is needed to negate (1000 0000)2c = −128. The negative of

flip copy  −128 = (1 1000 0000)2c is 0 1000 0000 . 2c Some teach one’s complement and use one’s complement plus 1 to describe negation. I don’t do this, I encour- age you to learn about it on your own.

Questions and Problems

1. Write 256 as a power of 2, well really, write the value of all powers of 2 up to 210. 2. How many 4-bit numbers are there? Pick a few and think of them as two’s complement numbers. What are the decimal values of these bit patterns? 3. Write a few 8-bit two’s complement numbers. Write their negatives. Confirm that their 8-bit sums are all 0’s. 4. What is the range of 16-bit two’s complement numbers? cse 1400/mth 2051 discrete math notes 10

Biased Representation of Integers

An alternative to two’s complement is biased notation. It is not so fancy. It simply shifts all nunbers in an interval [a, b] by c to a transformed interval [a + c, b + c]. Shifts are useful geometric opera- tions, along with rotations and scales. Biased numbers are useful as exponents. Biased numbers readily express shifts needed to align numbers written in scientific notation. For instance, to compute the sum below, align the exponents and add.

(3.14158 × 102 + 2.71828 × 10−3 ) = (3.14158 × 102 + 0.0000271828 × 102 ) = 3.1416071828 × 102

A comparison (subtraction) of exponents 2 and −3 determines a shift of 5 places in the decimal point of the smaller number needed to align values.

Here’s all you need to know about biased for this course. If an exponent (e)bias=bis written in biased notation, then its “real” value of the exponent is e − b. For instance,

( ) ( ) 2 5 bias=3 = 22 10 6 bias=5 = 101 cse 1400/mth 2051 discrete math notes 11

Padding Numbers

The length of a two’s complement number can be padded to make it longer. • For positive integers, pad with 0’s on the left:

(0101 0101)2c = (0 0101 0101)10c = (00 0101 0101)10c = ···

• For negative integers, pad with 1’s on the left:

(1010 1010)2c = (1 1010 1010)2c = (11 1010 1010)2c = ···

Signed Decimal To & From Two’s Complement

Integers, signed numbers, always require a sign. This means some minor changes to the previous unsigned algorithms are needed. You know: • Positive integers written in two’s complement start with a 0. • Negative integers written in two’s complement start with a 1.

For positive numbers simply prepend a 0. For instance to convert decimal +173 to two’s complement compute the binary representation for 173 and put a 0 on the left.

Repeated Remaindering mod 2 Quotients 173 86 43 21 10 5 2 1 Remainders 1 0 1 1 0 1 0 1

Therefore, 173 = (1010 1101)2, and +173 = (0 1010 1101)2c. Horner’s rule inverts the above computation to converts (0 1010 1101)2c to +173. Horner’s Rule @ x = 2 0 1 0 1 0 1 1 0 1 0 2 4 10 20 42 86 172 0 1 2 5 10 21 43 86 +173

Conversion between negative integers is not as simple, but not too hard either. There are several algorithms for conversion of negative numbers. Suppose you want to convert a negative two’s complement number to decimal. Here are three algorithms for the task. 1. Negate the number using the bit-flipping rule (copy right-to-left up to and including first 1; flip the remaining bits), convert to decimal using Horner, then put a minus − on the result.

Convert (1010 1100)2c to decimal.

negate Horner negate (1010 1100)2c 7→ (0101 0100)2c 7→ 84 7→ −84

2. Simply use Horner’s rule on the two’s complement number and then subtract 2d from the result, where d is the number of bits in the number.

Horner complement 8 (1010 1100)2c 7→ 172 7→ 172 − 2 = 172 − 256 = −84 cse 1400/mth 2051 discrete math notes 12

3. Change the leading 1 in the two’s complement number to −1 and use Horner’s rule.

negate first bit Horner (1010 1100)2c 7→ (−1010 1100)2c 7→ −84

You are only required to master only one approach for conversion between decimal and two’s complement.

Rational Numbers

Rational numbers start to fill in the between integers. The rationals are ratios of inte- gers Q = {a/b : (a, b ∈ Z) ∧ (b 6= 0)} where a is called the numerator and b is called the denominator. Think: how many of some denomination.

Fixed-Point Numbers

With the advent of calculators, fixed-point numbers have become popular to represent or approximate numbers. Here are some fixed-point numbers: 3.14, 2.718, and 0.006626. They can be written as fractions.

314 2718 6626 3.14 = , 2.718 = , and 0.006626 = . 100 1000 1000000 The values 3, 2, and 0 are the integer part of these numbers. and 0.14, 0.718, and 0.006626 are their frac- tional parts. A decimal fixed-point number has an optional sign, following by one or more digits, a decimal point, and then zero or more digits. This can be expressed as a grammar: fixed−point−number ::= [sign] (digit) + decimal−point (digit) ∗ sign : : = + | − decimal−point : : = . d i g i t : : = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

For binary fixed-point numbers, the only difference is bits replace digits and the decimal-point would be called a binary-point. Fixed-point numbers are written in . For instance, writing 2.718 states 2 ones, 7 tenths, 1 hundredth, and 8 thousandths make 2.718.

2.718 = 2 · 100 + 7 · 10−1 + 1 · 10−2 + 8 · 10−3

And, in binary or hexadecimal,

1 0 −1 −2 −3 2.718 = (10.1011011111001110111 ···)2 = 1 · 2 + 0 · 2 + 1 · 2 + 0 · 2 + 1 · 2 + ···

0 −1 −2 −3 2.718 = (2.B7CED916872B020C49BA ···)16 = 2 · 16 + B · 16 + 7 · 16 + C · 16 + ··· Variants of previously learned conversion algorithms can be used to compute these representations. cse 1400/mth 2051 discrete math notes 13

Conversion to Decimal

A fixed-point binary number, say (1100.0101)2, can be converted to decimal by using Horner’s rule (ignoring the binary-point), and then dividing by 24 (there are 4 places to the right of the point.)

Horner’s Rule @ x = 2 1 1 0 0 0 1 0 1 2 6 12 24 48 98 196 1 3 6 12 24 49 98 197

Therefore, 197 (1100.0101) = (= 12.3125) 2 24 For this type of problem, I’m happiest with an answer expressed as a decimal . There is no need too wirte your answer as a fixed-point number.

As another example, let’s convert a fixed-point octal number, say (23.65)8, to decimal. Horner’s Rule @ x = 8 2 3 6 5 16 152 1264 2 19 158 1269

Therefore, 1269 (23.651) = (= 19.828125) 8 82

Conversion from Decimal

To convert decimal fixed-point numbers to some base b, I recommend separating the integer and fractional calculations. 1. Convert the integer part as before: Invert Horner’s rule: Repeatedly compute quotients and reminders base b. 2. Convert the fractional part by repeated reminding by 1/b (multiplication by b) When a fractional part is multiplied by base b, the integer part of the product is a base b numeral: If 0 ≤ f < 1, then 0 ≤ b f < b. Here are some examples. • Convert 16.8 to binary.

First by inspection, the integer part 16 can be written in binary as (1 0000)2. Next, for the fractional part, 0.8, repeatedly multiply by 2 (divide by 1/2), picking off integer parts (0’s or 1’s) of the quotients (Only the fractional part need to be retained in the quotients: The remainders encode the integer part of the quotient (1 if the integer is odd, 0 if it is even.)

Repeated Remaindering mod 1/2 Quotients 0.8 1.6 3.2 6.4 12.8 25.6 51.2 0.8 1.6 1.2 0.4 0.8 1.6 1.2 Remainders 0. 0.1 0.11 0.110 0.1100 0.11001 0.110011 cse 1400/mth 2051 discrete math notes 14

=  Therefore 16.8 1 0000.11001100 2.

• Convert 16.8 to octal. First, 16 = (20)8 = 2 × 8. Next, for the fractional part, 0.8, repeatedly multiply by 8 (divide by 1/8), picking off integer parts (0’s through 7’s) and

Repeated Remaindering mod 1/8 Quotients 0.8 6.4 3.2 1.6 4.8 6.4 3.2 Remainders 0. 0.6 0.63 0.631 0.6314 0.63146 0.631463

=  Therefore 16.8 20.63146314 8.

Floating Point Numbers

Fixed-point notation falls down for really small and really large numbers. Imagine ex- pressing Planck’s constant, h = 6.62607004 × 10−34m2 kg/s in fixed-point notation. Or, 1 light-year, 9460730472580800 m, which when written in binary takes 54 bits. Computing use floating-point notation, also called scientific notation, instead of fixed-point. Histori- cally, manufacturers built wildly different circuits for floating-point computations. This lead to confounding results when the same code was run across different computer architectures. The set of floating-point numbers is used to approximate the set Q of rational numbers. The rationals are dense in the reals, so floating-point numbers approximate the reals R as well. It is beyond the scope of this course to provide a complete description of floating-point numbers. The IEEE Standard for Floating Point Arithmetic (IEEE 754) describes floating-point numbers in depth. Floating-point notation is built upon scientific notation. Rational numbers can be written in normalized decimal scientific notation: ( ) t = ±d. f × 10 e b

where d is a non-zero digit, f is a string of digits, and (e)b is a biased number. Floating-point numbers can also be written in normalized binary scientific notation:

( ) t = ±1. f × 2 e b

Binary normalized floating-point numbers always start 1. f . Therefore it is not necessary to stored the 1. (There are denormalized floating-point numbers, but these are not covered here.) IEEE Standard for Floating Point Arithmetic (IEEE 754) uses an exponent bias of b = 127 for floats and b = 1023 for doubles. These the most common floating-point types. The upshot is

( ) − 2 a b=127 = 2a 127 for 32-bit single precision numbers with 8 bit exponents

And ( ) − 2 a b=1023 = 2a 1023 for 64-bit double precision numbers with 11 exponent bits Here, we’ll use “pidgin” floating-point numbers that are 8 bits wide. There is 1 sign bit, three exponents bits, and 4 fraction bits. Sign Bit Exponent (bias = 3) Fraction

s e2e1e0 f−1 f−2 f−3 f−4 cse 1400/mth 2051 discrete math notes 15

The sign bit s controls positive or negative. The next three bits e = e2e1e0 encode a base 2 exponent written with a bias b = 3. That means the biased exponents in the range h000, . . . , 111i and represents integers in the range h−3, . . . , 4i. The fraction bits f = f−1 f−2 f−3 f−4 can represent fractions h/16 for h ∈ H, that is, 0/16 to 15/16. At the smallest exponent, the positive values look like this.

0 17 24 31 x 128 128 128

At the largest exponent, the positive values look like this.

0 16 24 31 x

Here are some examples.

(0 000 0000) f p = 0 (The only denormalized number) 1 (0 100 0000) f p = 2 (+(1 + 0) × 2 ) −2 (1 001 1000) f p = −3/8 (−(1 + 1/2) × 2 ) −1 (1 010 1111) f p = −31/32 (−(1 + 15/16) × 2 )

You can go the other way as well. For example to convert 5.3 to pidgin floating point. For instance

=  ≈ ( ) × 2 ( ) 5.3 101.010011001 2 1.0101 2 2 0 101 0101 f p

Questions and Problems

1. Write a few pidgin floating-point numbers and convert them to decimal. 2. Convert 7.3 to pidgin floating point notation.

Summary of Numbers

• Binary, octal, decimal, hexadecimal alphabets • Natural (unsigned whole) numbers – Horner: Some base 7→ decimal; – Remaindering: decimal 7→ some base • Integers (signed whole) numbers – Two’s complement bits in n * For n 6= 0, (n)2c + (−n)2c = 2 * Bit-flipping rule for negating • Fixed-point numbers – To decimal: Forget the point, use Horner, divide by appropriate power of base – To another base: Multiply fractional part by base pulling off numerals cse 1400/mth 2051 discrete math notes 16

• Floating-point numbers – Format Sign Bit Exponent (bias = 3) Fraction

s e2e1e0 f−1 f−2 f−3 f−4 s e e e −3 – Value (−1) 1. f−1 f−2 f−3 f−4 × 2 1 2 3

Questions and Problems

1. Write the number (0110)2c using 8 bits.

2. Write the number (1110)2c using 8 bits. 3. Pick a few positive two-digit decimal integers and write them in two’s complement notation. 4. Pick a few negative two-digit decimal integers and write them in two’s complement notation. 5. How many bits are needed to write integers in the range [−256, 255]? How many integers are in this range? How is this related to a power of 2? 6. How many bits are needed to write integers in the range [−512, 511]? How many integers are in this range? How is this related to a power of 2? 7. How would you define 16’s complement notation? cse 1400/mth 2051 discrete math notes 17

Truth

We all seek it, but truth is elusive. Thinkers seek truth. Our story of the history of truth can begin with Boole. Given statements, formally called propositions, that are either True or False, define some simple algebraic rules: • If proposition p is True, then its negation ¬p is False, and vice versa.

• If propositions p0 and p1 are both True, then their conjunction “p0 and p1” written p0 ∧ p1, is True. Other- wise, if either of p0 or p1 is False, their conjunction p0 ∧ p1 is False.

• If proposition p0 or p1 is True, then their disjunction “p0 or p1” written p0 ∨ p1, is True. Otherwise, when both p0 and p1 False, p0 ∨ p1 is False. The rules can be represented by truth tables. Draw truth tables for these three rules. Boolean logic uses these rules to derive other operations on propositions. The meaning of the proposi- tions is not relevant to Boolean logic, only the formal syntactic manipulation of symbols is important. Of course, when meaning in attached, practical applications of the rules can lead to new insights.

Conditional Statements

Mathematics is all about drawing conclusions from given assumptions. Indeed, mathematics is mostly about proving statements of the form: If these assumptions are True, then these conclusions will be True. Let p stand for assumptions and let q stand for conclusions. The conditional statement can be written as

p ⇒ q read: “if p, then q” or “p implies q.”

A fundamental question is: How to assign True and False to p ⇒ q for given values of p and q? There are four cases: 1. If the assumptions p are True, then the values of p ⇒ q depends on the value of q: (a) (True ⇒ True) = True (True conclusions can be drawn from True assumptions) (b) (True ⇒ False) = False (False conclusions cannot be drawn from True assumptions) 2. Some are confounded by the logic when the assumptions p are False. It goes like this: (a) (False ⇒ True) = True (False assumptions can imply True conclusions) (b) (False ⇒ False) = True (False assumptions can imply False conclusions) Here’s examples that may help explain this reasoning about conditionals. • Let x be a and consider the conditional: If x < 2, then x < 4. Do you agree implication is always True? (If you don’t agree, let’s talk.) Do you agree the implication remains True when x = 3 and when x = 5? • Consider the implication: If a student scores 90 or above on the final, then their grade will be an A. Do you agree the statement is only False when a student scores 90+ and receives a grade of B or below? Do you agree that a score below 90 has no effect on the truth of the implication? cse 1400/mth 2051 discrete math notes 18

Equivalence and Exclusive-Or

Some expressions can look different but have identical values. Here are some useful equiva- lences.

(p ≡ q) ≡ (p ⇒ q) ∧ (q ⇒ p) (Equivalence) (p ⇒ q) ≡ ¬p ∨ q (Material implication) ¬(p ∨ q) ≡ ¬p ∧ ¬q (De Morgan’s law) ¬(¬p ∧ ¬q) ≡ p ∨ q (De Morgan’s dual law) p ⇒ q ≡ (¬q ⇒ ¬p) (Contraposition)

Draw truth tables for the expressions on the left and right of ≡ to show they are equivalent.

Boolean logic can simulate simple addition. The negation of equivalent is exclusive or: One of two propositions is True and the other is False.A half-adder is a 1-bit sum and carry device. It is convenient to write the 1-bit sums and carries as a truth table. term 1 term 2 carry sum 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 The carry bit is the conjunction of the terms, and the sum bit is their exclusive-or ⊕. Another use of Xor is in computer graphics, where an image can be blanked with an Xor with itself and restored with a second Xor p ⊕ p = 0 0 ⊕ p = p Five functions (operations) on two Boolean propositions have been presented:

∧, ∨, ⇒ , ≡, ⊕

Are there other two- Boolean functions? If so, what are they? How many two-variable Boolean functions are there?

Counting Truth

There are four assignments of truth on two propositions. The values in a pair (p, q) can take one of four combinations from the set B2 = {(0, 0), (0, 1), (1, 0), (1, 1)}. A triple (p, q, r) can take on one of eight values. There are 21 = 2 truth assignments for one variable (proposition) and only one 20 = 1 truth assignment on no variables (the empty value).

Generalize: An n-tuple of propositions has 2n different truth assignments. This set of truth assignments can be denoted Bn. The operations and, or, implies,equivalent, exclusive-or, etc., are a few of the functions on two propositions, the domain B2. Let’s count functions on Boolean tuples. cse 1400/mth 2051 discrete math notes 19

0 • There are 2 = 22 functions on no input: Boolean constants B() = True or B() = False. 1 • There are 4 = 22 Boolean functions on one variable p: Always False, Identity, Negation, and Always True: B(p) = False B(p) = p B(p) = ¬p B(p) = True

2 • There are 16 = 22 Boolean functions of 2 variables. Some can be interpreted as comparison relations.

Input Outputs p q = 6= < ≤ > ≥ 0 0 1 0 0 1 0 1 0 1 0 1 1 1 0 0 1 0 0 1 0 0 1 1 1 1 1 0 0 1 0 1 Decimal 9 6 4 13 2 11

An additional ten functions can be defined on ordered pairs of Boolean variables (p, q).

Input Outputs

p q and or not p True False p nor not q q nand 0 0 0 0 1 1 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 1 1 1 0 0 1 0 1 0 1 0 1 0 1 1 1 1 1 0 1 0 1 0 0 1 0 Decimal 1 7 12 15 0 3 8 10 5 14

n • In general, there are 22 Boolean functions on n propositions.

Generalize again: There are |Y||X| functions f : X 7→ Y from X to Y.

Truth for Arithmetic

The truth table below describes a binary full-adder. Input bits a, b, and cin are summed to produce a sum bit s and a carry-out bit cout. Input Output

Row a b cin cout s 1 0 0 0 0 0 2 0 0 1 0 1 3 0 1 0 0 1 4 0 1 1 1 0 5 1 0 0 0 1 6 1 0 1 1 0 7 1 1 0 1 0 8 1 1 1 1 1 cse 1400/mth 2051 discrete math notes 20

There are two canonical ways to represent a Boolean function: disjunctive and conjunctive normal forms. The input/output behavior of a function can be used to construct these forms. the function. Disjunctive normal form is also called sum of products. Given a truth table: For each row with output 1, form an AND-clause of the input variables using p when p = 1 and ¬p when p = 0. Next, form the disjunction of each of these AND-clauses. For instance, referring to full-adder truth table the sum bit s is 1 in row 2, 3, 5 and 8.

Row a b cin And-clause

2 0 0 1 c2 = ¬a ∧ ¬b ∧ cin 3 0 1 0 c3 = ¬a ∧ b ∧ ¬cin 5 0 1 1 c5 = ¬a ∧ b ∧ cin 8 1 1 1 c8 = a ∧ b ∧ cin

The disjunctive normal form for the sum is:

s = c2 ∨ c3 ∨ c5 ∨ c8 = (¬a ∧ ¬b ∧ cin) ∨ (¬a ∧ b ∧ ¬cin) ∨ (a ∧ ¬b ∧ ¬cin) ∨ (a ∧ b ∧ cin)

You can test that this formula evaluates to 1 in rows 2, 3, 5 and 8, and it is 0 in the other rows. Conjunctive normal form is also called product of sums. Given a truth table: For each row with output 0, form an OR-clause of the input variables using ¬p when p = 1 and p when p = 0. Next, form the conjunction of each of these OR-clauses. For instance, referring to full-adder truth table the sum bit s is 0 in row 1, 4, 6, and 7.

Row a b cin And-clause

1 0 0 0 d1 = a ∨ b ∨ cin 4 0 1 1 d4 = a ∨ ¬b ∨ ¬cin 6 1 0 1 d6 = ¬a ∨ b ∨ ¬cin 7 1 1 0 d7 = ¬a ∨ ¬b ∨ cin

The conjunctive normal form for the sum is:

s = d1 ∧ d4 ∧ d6 ∧ d7 = (a ∨ b ∨ cin) ∧ (a ∨ ¬b ∨ ¬cin) ∧ (¬a ∨ b ∨ ¬cin) ∧ (¬a ∨ ¬b ∨ cin)

You can test that this formula for s evaluates to 0 in rows 1, 4, 6 and 7, and it is 1 in the other rows.

Questions and Problems

1. A back-of-the envelope calculation estimates there are 1080 hydrogen atoms in the universe. Calculate the number of Boolean inputs needed to create more functions than atoms. 2. Construct truth tables for the following Boolean expressions. (a) Modus ponens: (p ∧ (p ⇒ q)) ⇒ q (b) Contradiction: (¬p ⇒ q) ∧ (¬p ⇒ ¬q) ≡ p (c) Currying: ((p ∧ q) ⇒ r) ≡ (p ⇒ (q ⇒ r)) (d) Modus tollens: (¬q ∧ (p ⇒ q)) ⇒ ¬p cse 1400/mth 2051 discrete math notes 21

Sets

Sets are a complicated things. Naïve concepts about sets are simple, but details eventually get in the way. For example, consider Russell’s paradox: Is the set S of all sets that are not members of themselves in itself?

S = {A : A 6∈ A}

If S 6∈ S, then S ∈ S. And, if S ∈ S, then S 6∈ S. Types try to tame paradoxes and are essential to compuing, but this is outside this scope of this class.

Finite Sets

A finite set can be described by listing its members in a separated list enclosed in curly braces {}. For instance, • The bits B = {0, 1} • The hexits H = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}

• The modular integers Zm = {0, 1, 2, . . . , (m − 1)} Finite sets are important because they define alphabets for languages. for natural and abstract language symbols is a large finite set. And, the number of states in computing machines is finite. However, infinite collections are interesting and some can be described finitely.

Set Comprehension

Some sets can be described by comprehension. Simple infinite sets can be described by a clear recur- pattern. For instance, • The integers Z = {0, ±1, ±2, . . .} • Even and odd integers: EVEN = {2n : n ∈ Z} and ODD = {2n + 1 : n ∈ Z} • The rational numbers Q = {a/b : a ∈ Z, b ∈ Z, b 6= 0} A set X can be comprehended by a predicate:

X = {x : p(x) is True} where p(x) is a statement whose truth depends on the value of x. More generally, X can be compre- hended by a function where a predicate is satisfied.

X = { f (x) : p(x) is True}

There is an isomorphism between sets and Boolean expressions. There are two special sets: 1. The empty set ∅, some write {}: The set with no elements. Identify ∅ with logical False. cse 1400/mth 2051 discrete math notes 22

2.A universal set U: The set of all possible things. Identify U with logical True. The universal set depends on context (semantics). It is good to have an algorithm that decides if a given thing x is or is not in a set: (x ∈ {X}) or (x 6∈ {X}). There are three basic operations: 1. The set complement ¬X = {a : a 6∈ X} Identify set complement ¬ with not. 2. The intersection X ∩ Y = {a : (a ∈ X) ∧ (a ∈ Y)} Identify intersection ( ∩ ) with and (∧). 3. The union X ∪ Y = {a : (a ∈ X) ∨ (a ∈ Y)} Identify union (round ∪ ) with or (pointy ∨). Other Boolean logic operations can be interpreted in terms of sets. • Conditional becomes subset: p ⇒ q 7→ P ⊂ Q where P is the set where p is True and Q is the set where q is True. • Equivalence becomes set : (p ≡ q) ≡ (P ⊆ Q) ∧ (Q ⊆ P) • Exclusive Or becomes symmetric difference: P ⊕ Q = {x : (x ∈ P) ⊕ (x ∈ Q)}

Venn Diagrams

Set expressions can be visualized by Venn and Euler diagrams. Two sets X and Y can be drawn in several relationships: Euler diagrams illustrate these.

U U U Y Y Y X X X

X ∩ Y = ∅ X ⊆ Y

Let’s count shadings of Venn diagrams as a function of the number of intersecting subsets. 0 • There are 2 = 22 shadings of the universe on no subsets: One for ∅ and one for U. 1 • There are 4 = 22 of the universe shadings on one set: A single subset X partitions the universe into two regions: X and ¬X and each can be shaded or not. 2 • There are 16 = 22 shadings on two intersecting sets: Two intersecting subsets X and Y partition the universe into 4 regions: X ∩ Y, X ∩ ¬Y, ¬X ∩ Y, and ¬X ∩ ¬Y. 3 • There are 256 = 22 shadings on three intersecting sets: Three intersecting subsets X, Y, and V partition the universe into 8 regions. n • In general, n intersecting sets partition the universe into 2n regions so there are 22 ways to shade the universe given n intersecting sets. cse 1400/mth 2051 discrete math notes 23

Rules of Inference

When a proposition is always True, it can be used whenever needed. These valid expressions are called tautologies and rules of inference. 1. Modus ponens: (p ∧ (p ⇒ q)) ⇒ q “If p is True and p ⇒ q is True, then q must be True.” The rule behind direct proofs. 2. Modus tollens: (¬q ∧ (p ⇒ q)) ⇒ ¬p “If ¬q is True and p ⇒ q is True, then p must be False.” The rule behind indirect proofs. 3. Contradiction: (¬p ⇒ q) ∧ (¬p ⇒ ¬q) ≡ p “If ¬p can imply both q and ¬q, then p must be True.” The rule behind proofs by contradiction. 4. Currying: ((p ∧ q) ⇒ r) ≡ (p ⇒ (q ⇒ r)) “An implication that depends on two assumptions can be reduced to two implications each depending on one assumption.”

First-Order Logic

The value of a statement can depend on variables values within it. Predicate logic adds two quantifiers to propositional logic. The quantifiers are something and everything. Predicate statements are written using the templates: • Some thing satisfies a property (the predicate) • Every thing satisfies a property (the predicate) For instance, you know there is some real number x that satisfies the equation x2 − x − 1 = 0 On the other hand, you know every real number x satisfies the equation x + 0 = x And, there are no real numbers x that satisfy the equation x2 + x + 1 = 0 Mathematicians use standard symbols for these and similar phrases. ∃ is read: For some, there exists, there is a least one, or like phrases. For some is an existential quantifier: It can be used to state a predicate is sometimes True or sometimes False. ∀ is read: For all, for every, for each, or like phrases. For all is a universal quantifier: It can be used to state a predicate is always True or never True. Statements in predicate logic have forms such as All S are T (S ⊆ T) No S are T (S ∩ T = ∅) Some S are T (S ∩ T 6= ∅) Some S are not T (S 6⊆ T)

• (∀ x)(∀ y) commute: If predicate p(x, y) is True for every x and for every y, then it is True for every y and for every x. (∀ x)(∀ y) ≡ (∀ y)(∀ x) For instance,

(∀ X ⊆ U)(∀ Y ⊆ U)(X ∪ Y = Y ∪ X) ≡ (∀ Y ⊆ U)(∀ X ⊆ U)(X ∪ Y = Y ∪ X) cse 1400/mth 2051 discrete math notes 24

• (∃ x)(∃ y) commute: If predicate p(x, y) is True for some x and for some y, then it is True for some y and for some x. (∃ x)(∃ y) ≡ (∃ y)(∃ x)

• (∀ x)(∃ y) do not commute in general: If a predicate p(x, y) is True for all x’s and for some y, then it may not be True for some y and all x’s.

(∀ x)(∃ y) 6≡ (∃ y)(∀ x)

For instance, for every x ∈ N there exists a y ∈ N such that y = x2 − x − 1. But, there does not exists a y ∈ N such that y = x2 − x − 1 for every x ∈ X. • (∀ y)(∃ x) do not commute: If a predicate p(x, y) is True for all y’s and for some x, then it may not be True for some x and all y’s. (∀ y)(∃ x) 6≡ (∃ x)(∀ y)

De Morgan-like Laws for Quantification

The not operator can be distributed across quantified expressions. • The statement: “It is not the case that p(x) is True for all x” is equivalent to “There is an x such that p(x) is False. ¬(∀x)(p(x)) ≡ (∃x)(¬p(x))

• The statement: “It is not the case that p(x) is True for some x” is equivalent to “For all x p(x) is False.

¬(∃x)(p(x)) ≡ (∀x)(¬p(x))

Examples Predicate Statements

• big O: Let f , g : N 7→ R be functions from the natural to the reals.

(∃c > 0)(∃m > 0)(∀n ≥ m)( f (n) ≤ cg(n))

• Fermat’s last theorem: (∀n ≥ 3)(∀x, y, z ∈ N+)(xn + yn 6= zn)

Questions and Problems

1. Let EVEN be the set of even integers Write the English sentence “If n is even, then there exists an inte- ger k such that n = 2k” as a quantified mathematical expression. 2. What simple English sentence describes the statement “(∀n ∈ N)(∃m ∈ N)(n < m)”? cse 1400/mth 2051 discrete math notes 25

Sequences

Sequences are the discrete analog of continuous functions. A sequence ~S is a discrete func- tion from the naturals (domain) to the reals (co-domain): ~S : N 7→ R. Most often, the co-domain is the naturals too. For sequences, I recommend the Online Encyclopedia of Integer Sequences to you. On May 4, 2014 it contained 241, 515 edited sequences that were found interesting to somebody, I’m certain there are more today. There are classic functions studied in continuous mathematics (, , exponentials, trigonometric, etc). There are classic sequences studied in discrete mathematics. • Alice: A~ = h1, 1, 1, 1, 1, 1, 1, 1, 1, 1, . . .i • Gauss: G~ = h0, 1, 2, 3, 4, 5, 6, 7, 8, 9 . . .i • Triangular: ~T = h0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, . . .i • Doubling (or Power of 2): D~ = h1, 2, 4, 8, 16, 32, 64, 128, 256, 512, . . .i • Mersenne: M~ = h0, 1, 3, 7, 15, 31, 63, 127, 255, 511, . . .i The largest discovered (12/26/2017) prime is 277,232,9171. • : ~F = h0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .i D E ~ 3 11 50 274 • Harmonic: H = 0, 1, 2 , 6 , 24 , 120 ,... • Prime: ~P = h2, 3, 5, 7, 11, 13, 17, 19, 23, 29, . . .i The values in a sequence can sometimes be described by some function. The function sometimes satisfies a recurrence equation that can be used to compute future values from current and past values. (Differential equations are the continuous analog of this concept.) • Alice: A~ = h1, 1, 1, 1, 1, 1, 1, 1, 1, 1, . . .i

a(n) = 1 (functional form)

an = an−1, a0 = 1 (recursive-initial value form)

• Gauss: G~ = h0, 1, 2, 3, 4, 5, 6, 7, 8, 9 . . .i

g(n) = n (functional form)

gn = gn−1 + 1, g0 = 0 (recursive-initial value form)

• Triangular: ~T = h0, 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, . . .i

t(n) = n(n − 1)/2 (functional form)

tn = tn−1 + (n − 1), t0 = 0 (recursive-initial value form)

• Doubling (or Power of 2): D~ = h1, 2, 4, 8, 16, 32, 64, 128, 256, 512, . . .i

d(n) = 2n (functional form)

dn = 2dn−1, d0 = 1 (recursive-initial value form) cse 1400/mth 2051 discrete math notes 26

• Mersenne: M~ = h0, 1, 3, 7, 15, 31, 63, 127, 255, 511, . . .i

m(n) = 2n − 1 (functional form)

mn = 2mn−1 + 1, m0 = 0 (recursive-initial value form)

• Fibonacci: ~F = h0, 1, 1, 2, 3, 5, 8, 13, 21, 34, . . .i

ϕn − ϕ¯n f (n) = √ (functional form) 5

fn = fn−1 + fn−2, f0 = 0, f1 = 1 (recursive-initial value form) D E ~ 3 11 50 274 • Harmonic: H = 0, 1, 2 , 6 , 24 , 120 ,...

1 1 1 −6 Hn = ln(n) + γ + − + − O(n ) (asymptotic formula) 2n 12n2 120n4 (γ ≈ 0.5772 ··· is the Euler-Mascheroni constant) 1 H = H − + , H = 0 (recursive-initial value form) n n 1 n 0

• Prime: ~P = h2, 3, 5, 7, 11, 13, 17, 19, 23, 29, . . .i No known function, asymptotic formula, or recurrence for prime numbers.

The Difference Sequence

Derivatives are studied in continuous mathematics. Differences are the discrete analog. Let

~ S = hs0, s1, s2, s3,..., sn,...i be a sequence. The (forward) difference of ~S is the sequence

~ 4S = h(s1 − s0), (s2 − s1), (s3 − s2), (s4 − s3),..., (sn − sn−1),...i

Here are some results for the sequences mentioned above. • The difference of the Alice sequence is the Zero sequence. 4A~ = h0, 0, 0, 0, . . .i = ~Z • The difference of the Gauss sequence is the Alice sequence. 4G~ = h1, 1, 1, 1, . . .i = A~ • The difference of the Triangular sequence is the Gauss sequence. 4~T = h0, 1, 2, 3, . . .i = G~ • The difference of the Mersenne sequence is the Doubling sequence. 4M~ = h1, 2, 4, 8, . . .i = D~ • The difference of the Fibonacci sequence is 1 : ~F 4~F = h1, 0, 1, 1, 2, 3, 5, . . .i = 1 : ~F D E ~ 1 1 1 1 1 1 • The difference of the Harmonic sequence is the harmonic progression 4H = 1, 2 , 3 , 4 , 5 , 6 , 7 ,... • The difference of the Prime sequence has no known formula cse 1400/mth 2051 discrete math notes 27

The Partial Sum Sequence

Integrals are studied in continuous mathematics. Summations are the discrete analog. Let

~ S = hs0, s1, s2, s3,..., sn−1,...i be a sequence (of things that can be added). The partial sums of ~S is the sequence * + ~ ∑ S = 0, s0, (s0 + s1), (s0 + s1 + s2),..., ∑ sk,... 0≤k

I (almost always) choose to begin a sequence of partial sums with the empty sum ∑0≤k<−1 (the sum of no terms) which is equal to zero. In the sum ∑ sk = s0 + s1 + ··· + sn−1 0≤k

k = 0 is the lower limit, k = (n − 1) is the upper limit, and terms summed are sk for k = 0, 1, . . . , (n − 1). As a random example, let ~S = h30, 17, 87, 8, 22, . . .i The sequence of partial sums is * + ~ ∑ S = 0, 30, 47, 134, 142, . . . , ∑ sk,... (What is the next partial sum?) 0≤k

Here are some examples for classic sequences mentioned in previous notes. • The partial sums of the Alice sequence is the Gauss sequence.

∑ A~ = ∑ 1 = h0, 1, 2, 3, . . .i = G~ 0≤k

• The partial sums of the Gauss sequence is the Triangular sequence.

∑ G~ = ∑ k = h0, 0, 1, 3, 6, 10, 15, . . .i = ~T 0≤k

• The partial sums of the Triangular sequence is the Pyramidal sequence.

n ∑ ~T = ∑ = h0, 0, 0, 1, 4, 10, 20, . . .i = ~P 0≤k

• The partial sums of the Mersenne sequence is the Doubling minus the tail of the Gauss sequence.

∑ M~ = ∑ (2k − 1) = h0, 0, 1, 4, 11, 26, . . .i = D~ − tail(G~ ) 0≤k

• The partial sum of the Fibonacci sequence is the tail of itself minus the Alice sequence.

~ ~ ~ ∑ F = ∑ Fk = h0, 0, 1, 2, 4, 7, 12, . . .i = tail(F) − A 0≤k

The Fundamental Theorem of the Sum and Difference Calculus

Derivatives and integrals are related by the Fundamental Theorem of Calculus. The fun- damental theorem for sums and differences is more simple. The sum of differences ∑ 4 f (k) = f (n) − f (0) 0≤k

∑ 4 f (k) = ∑ [ f (k + 1) − f (k)] 0≤k

The difference of sums is ! 4 ∑ f (k) = f (n) 0≤k

Mathematical Induction

The ability to construct proofs by mathematical induction is a valuable skill. Induction proofs are based on one of Peano’s axioms for arithmetic.

Principle of Induction: Let S be a subset of naturals (S ⊂ N) such that •0 ∈ S, and • if n ∈ S, then n + 1 ∈ S. Then all naturals are in S (S = N). Some call this the domino axiom: If one domino falls, then so will the next and the first domino falls.

Questions and Problems

1. Consider the sequence of even naturals: ~E = h0, 2, 4, . . .i. Find a formula for its sequence of partial sums. 2. Consider the sequence of odd naturals: O~ = h1, 3, 5, . . .i. Find a formula for its sequence of partial sums. cse 1400/mth 2051 discrete math notes 29

Induction

If p(0) is True and if p(n) ⇒ p(n + 1) is True, then p(n) is True for all natural numbers n ∈ N. There are so many applications of this axiom you could write a book1 1. Here are a few examples.

Arithmetic Sums

An arithmetic sequence has terms that fall along a straight line. Values in the Gauss se- quence G~ = h0, 1, 2, 3, . . .i lie on the line y = x. In general, the sequence ~S = hb, m + b, 2m + b, 3m + b,...i is arithmetic and its terms lie on the line y = mx + b. An arithmetic sum is a sum of terms from an arith- metic sequence. Here’s an example. Partial sums of the Gauss sequence produce the Triangular sequence.

n(n − 1) n 0 + 1 + 2 + 3 + ··· + (n − 1) = ∑ k = = (n choose 2) 0≤k

And, here’s the mathematical induction proof. 1. Basis: Show the result is True for n = 0.

0(0−1) ∑ k = 0 (The sum is empty and equals 0. The function 2 = 0 too.) 0≤k<0

2. Inductive Step: Show p(n) ⇒ p(n + 1) is True. (a) Assume:  n(n − 1)  (∃n ∈ N) 0 + 1 + 2 + ··· + (n − 1) = 2 That this assumption is True follows from the basis. (b) Prove: (n + 1)n n + 1 0 + 1 + 2 + 3 + ··· + (n − 1) + n = k = = ∑ 2 2 0≤k<(n+1) Follow the steps:

 n(n − 1)  [0 + 1 + 2 + 3 + ··· + (n − 1)] + n = + n (by the assumption) 2 n(n − 1) 2n = + 2 2 n2 + n = 2 (n + 1)n n + 1 = = (functional form preservered) 2 2

 n(n − 1) n (∀n ∈ N) 0 + 1 + 2 + ··· + (n − 1) = = ∴ 2 2 There is a simple rule for arithmetic sums: The value of an arithmetic sum is the number of terms times the average of the first and last term. cse 1400/mth 2051 discrete math notes 30

Use mathematical induction to prove

(n − 1)m + 2b b + (m + b) + (2m + b) + ··· + ((n − 1)m + b) = ∑ (mk + b) = n 0≤k

Geometric Sums

A geometric sequence has terms that fall along an exponential curve. Values in the Dou- bling sequence D~ = h1, 2, 4, 8, . . .i lie on the exponential y = 2x. A general geometric sequence is ~R = 1, r, r2, r3,... . A geometric sum is a sum of terms from a geometric sequence. The partial sums of the Doubling sequence produce the Mersenne sequence. 1 + 2 + 22 + 23 + ··· + 2n−1 = ∑ 2k = 2n − 1 0≤k

2. Inductive Step: Show p(n) ⇒ p(n + 1) is True. (a) Assume:   (∃n ∈ N) 1 + 2 + ··· + 2n−1 = 2n − 1

This the assumption is True because the basis has been established. (b) Prove h i 1 + 2 + 22 + ··· + 2n−1 + 2n = ∑ 2k = 2n+1 − 1 0≤k<(n+1) Follow the steps:

[1 + 2 + 22 + ··· + 2n−1] + 2n = [2n − 1] + 2n (by the assumption) = 2 · 2n − 1 = 2n+1 − 1

 n−1 n  ∴ (∀n ∈ N) 1 + 2 + ··· + 2 = 2 − 1 Use mathematical induction to prove rn − 1 1 + r + r2 + ··· + rn−1 = rk = (for r 6= 1) ∑ − 0≤k

Questions and Problems

1. Use mathematical induction to prove the sum of the odd naturals from 1 to 2n − 1 is n2. cse 1400/mth 2051 discrete math notes 31

Induction, Binomial Coefficients

Pascal’s triangle furnishes many induction examples.

0 1 2 3 4 5 Row Sum 0 1 2 3 4 5 0 0 1 1 0 (0) 1 1 1 1 1 2 1 (0)(1) 2 2 2 2 1 2 1 4 2 (0)(1)(2) 3 3 3 3 3 1 3 3 1 8 3 (0)(1)(2)(3) 4 4 4 4 4 4 1 4 6 4 1 16 4 (0)(1)(2)(3)(4) 5 5 5 5 5 5 5 1 5 10 10 5 1 32 5 (0)(1)(2)(3)(4)(5) The values in the triangle are called “binomial coefficients.” Empty cells have value 0. Pascal’s identity is a famous recurrence that can be used to compute binomial coefficients.

n n − 1 n − 1 = + k k k − 1

with boundary conditions n n  (∀n ∈ N) = 1 ∧ = 1 0 n Binomial coefficients can also have a functional form. n n! = k k!(n − k)!

Here are some summation formulas found in Pascal’s triangle. n • A sum on column m up to row n − 1 is (m+1), the value in the next row and next column.  k   n  = ∑ + 0≤k

The proof goes like this. Let the column m ≥ 0 be fixed. The induction is over rows n. 1. Basis: For n = 0.   k 0 ∑ = 0 (The sum is empty and equals 0. The function (m+1) = 0 also.) 0≤k<0 m

2. Inductive Step: Show p(n) ⇒ p(n + 1) is True. (a) Assume:  0   1  n − 1  n  (∃n ∈ N) + + ··· + = m m m m + 1 This the assumption is True because the basis has been established. (b) Prove  0   1  n − 1  n   n + 1 + + ··· + + = m m m m m + 1 cse 1400/mth 2051 discrete math notes 32

Follow the steps:  0   1  n − 1  n   n   n  + + ··· + + = + (by the assumption) m m m m m + 1 m  n + 1 = (By Pascal’s identity) m + 1 • Another important example is: The sum of all values in row n is 2n. The basis is for row 0: The sum is 0 0 (0) = 1 and 2 = 1. The conditional to prove True is: n n n n + 1 n + 1 n + 1 + + ··· + = 2n ⇒ + + ··· + = 2n+1 0 1 n 0 1 n + 1 n n+1 n Pretend the assumption is True. Then, using Pascal’s identity and the facts that (0) = ( 0 ) = (n) = n+1 (n+1) = 1. n + 1 n + 1 n + 1 n + 1 n + 1 n n n n + + ··· + + = + + + + + 0 1 n n + 1 0 1 0 2 1 n  n  n + 1 ··· + + + n n − 1 n + 1 n n n n = 2 + + + ··· + 0 1 2 n = 2 · 2n = 2n+1

Questions and Problems

1. Use mathematical induction to prove the sum of the even naturals from 0 to 2n is (n + 1)n. 2. Use mathematical induction to prove the sum of 0 + 1 + 4 + 9 + ··· + (n − 1)2 is n(n − 1)(2n + 1)/6. n 3. The binomial coefficient (k) is equal to the form n n! = k k!(n − k)! where 0! = 1 and n! = n(n − 1) ··· 2 · 1 for n > 0. Use this to proof Pascal’s identity: n n − 1 n − 1 = + k k k − 1 4. Pretend you save $100 at the start of each month for 40 years. How much money will you have invested? 5. Pretend your monthly savings go into an annuity which grows at 1% per month. The value of your annuity is geometric sum that at the start of month 480 has value

$100(1 + (1.01) + (1.01)2 + ··· + (1.01)480)

Show that the worth of your annuity is (1.01)481 − 1 100 × = $1, 188, 342.02 0.01 Was it worth it? Getting 1% a month may be unreasonable. Do the calculation again at a 0.5% monthly rate. cse 1400/mth 2051 discrete math notes 33

Induction, Part 2

There are 2n subsets of an n-element set. This follows from the row sum formula for binomial coefficients presented in the previous notes. Here’s another inductive proof of this fact. The basis is when n = 0: There is 20 = 1 subset of ∅, the empty set itself: ∅ ⊆ ∅. The expression (∀ sets X)(∅ ⊆ X) is always True because x ∈ ∅ ⇒ x ∈ X is always True because x ∈ ∅ is always False. The hypothesis is: For some n ≥ 0, there are 2n subsets of any n-element set. The conclusion is: Let S = {0, 1, . . . , (n − 1), n} be an (n + 1) element set. By hypothesis, there are 2n subsets of the n-element subset S0 = {0, 1, . . . , (n − 1)}. Each one of these, say X, is a subset of S. And, there are 2n subsets of S that have the form X ∪ {n}. Therefore there are 2n + 2n = 2 · 2n = 2n+1 subsets of any n + 1-element set. Recall, there is a mapping between bit strings and subsets of a finite sets. Use this to prove there are 2n subsets of an n-element set.

The sum of the first n Fibonacci numbers is Fn+1 − 1. The Fibonacci sequence is ~ F = h0, 1, 1, 2, 3, 5, 8, 13, 21,...i = hF0, F1, F2, F3, F4, F5,...i

A basis for the statement is: The sum of no terms, n = 0 is empty and equals 0. The value of F1 − 1 is also 0. So, if F0 + F1 + ··· + Fn−1 = Fn+1 − 1, then

[F0 + F1 + ··· + Fn−1 ] + Fn = [Fn+1 − 1] + Fn (Assumption)

= Fn+2 − 1 (Fibonacci recurrence)

A True basis is necessary! Let f (n) = 2n + 1 and g(n) = 2n − 1. What’s wrong with this proof? Assume f (n) = g(n) is True for some n. Then, f (n + 1) = 2(n + 1) + 1 = (2n + 1) + 2 = f (n) + 2 and g(n + 1) = 2(n + 1) − 1 = (2n − 1) + 2 = g(n) + 2. Therefore, f (n + 1) = g(n + 1). Conclusion: 2n + 1 = 2n − 1 for all n at and beyond the value in the assumption.

Sums involving fractions need not be hard. Consider the equation: 1 n = ∑ ( + ) + 1≤k≤n k k 1 n 1 Notice that partial fraction decomposition 1 1 1 = − k(k + 1) k k + 1 to see the sum telescopes

1  1 1  1 1 n = − = − = ∑ ( + ) ∑ + + + 1≤k≤n k k 1 1≤k≤n k k 1 1 n 1 n 1

Some induction proofs start with a basis other than 0. Prove that n! > 2n for all n ≥ 4. cse 1400/mth 2051 discrete math notes 34

The inequality is True for n = 4, 4! = 24 > 24 = 16. But it is False at n = 3: 3! = 6 < 23 = 8. So, if n! > 2n is True for some n ≥ 4, then

(n + 1)! = (n + 1) · n! > (n + 1)2n ≥ 5 · 2n > 2n+1

Here’s some more examples.

There are n! permutations of on an n-element set.A permutation is an arrangement of members in the set. A permutation is a one-to-one and onto function from a set to itself. There is one permutation on ∅: the empty permutation that arranges nothing. There is one permutation on Set0: the arrangement h0i. There is two permutation on Set0, 1: h0, 1i and h1, 0i. For {0, 1, 2}, insert 2 in h0, 1i last, second, and first. Do the same with h1, 0i. Therefore there are 3 · 2 = 3! permutations on 3 elements. Now follow the same argument starting with n! permutations on {0, 1, . . . , (n − 1)}. Then include another value n. Why will there be (n + 1)! permutations on an n + 1-element set.

Prove 6 divides 7n − 1 for all natural numbers n. For n = 0, 6 divides 70 − 1 = 0. If 6 divides 7n − 1 for some n ≥ 0, then

7n+1 − 1 = (6 + 1)7n − 1 = 6 · 7n + (7n − 1) (6 divides 6 · 7n and 7n − 1 )

The sum of binomial coefficients along an upward diagonal is a Fibonacci number. n n − 1 n − 2 0 + + + ··· + = F + 0 1 2 n n 1

Write the first few rows of Pascal’s triangle and verify this equation. The Fibonacci recurrence depends on two previous values. So sometimes two consecutive basis proofs and two hypotheses must be made. 0 1 0 For n = 0, the sum is one term (0) = 1 and F0+1 = 1 also. For n = 1, the sum is one term (0) + (1) = 1 and F1+1 = 1 also. If, for n = 0 and n = 1 n − k (n − 1) − k ∑ = Fn+1 and ∑ = Fn 0≤k≤n k 0≤k≤n−1 k

n −1 then, using (−1) = (n+1) = 0 n + 1 − k n − k n − k = + ∑ ∑ − 0≤k≤n+1 k 0≤k≤n+1 k k 1 " # " # n − k n − k = + ∑ ∑ − 0≤k≤n k 1≤k≤n k 1 " # " # n − k (n − 1) − k = ∑ + ∑ 0≤k≤n k 0≤k≤n−1 k

= Fn+1 + Fn

= Fn+2 cse 1400/mth 2051 discrete math notes 35

Recursion

Recursive: adj.See:Recursive (Kelly-Bootle, 1981). Differential equations are studied in contin- uous mathematics. Recurrence equations are the discrete analog. Recurrence equations define a value in terms of previous values. A initial conditions are also given to terminate the recursion. Several recurrences have been listed in previous notes.

an = an−1, a0 =1 (Alice)

gn = gn−1 + 1, g0 =0 (Gauss)

tn = tn−1 + (n − 1), t0 =0 (Triangular)

dn = 2dn−1, d0 =1 (Doubling)

mn = 2mn−1 + 1, m0 =0 (Mersenne)

fn = fn−1 + fn−2, f0 =0, f1 = 1 (Fibonacci) 1 H = H − + , H =0 (Harmonic) n n 1 n 0 Partial differential equations are also important, and they have discrete analogs too. They have boundary conditions. n n − 1 n − 1 n n = + , for all n = = 1 (Binomial coefficients) k k k − 1 0 n n n − 1 n − 1 n n = (n − 1) + , for n > 0 = 0, and for all n = 1 (First Stirling numbers) k k k − 1 0 n n n − 1 n − 1 n n = k + , for n > 0 = 0, and for all n = 1 (Second Stirling numbers) k k k − 1 0 n

Methods to solve equations are major topics in mathematics. The oracle method may be the most simple:√ Given a proposed solution, show that it satisfies the equation. For instance, the ϕ = (1 + 5)/2 solves the Fibonacci quadratic equation x2 − x − 1 = 0. Here’s the proof:

√ !2 √ √ √ 1 + 5 1 + 5 1 + 2 5 + 5 1 + 5 − − 1 = − − 1 2 2 4 2 √ √ 3 + 5 1 + 5 2 = − − = 0 2 2 2 To apply the oracle method to recurrences, a function, instead of a number, is substituted into the equation. For instance, the function m(n) = 2n − 1 satisfies the Mersenne recurrence

mn = 2mn−1 + 1 for n > 0, and initial condition m0 = 0.

n 0 To see this, notice the function m(n) = 2 − 1 satisfies the initial condition m(0) = 2 − 1 = 0 = m0, the basis. Next, if m(n) = 2n − 1, then m(n − 1) = 2n−1 − 1 and

2m(n − 1) + 1 =2(2n−1 − 1) + 1 = 2n − 1 = m(n) cse 1400/mth 2051 discrete math notes 36

Some problems are just fanciful, such as the Tower of Hanoi: Somewhere in southeast Asia, near Hanoi, there is a sect of monks who are moving 64 golden disks from one diamond needle onto another diamond needle subject to God’s laws: 1. Move only one disk at a time. 2. Never place a large disk on a smaller one. God decreed that world will end when the monks complete their task. Let’s assume the monks are speedy and can move 1 disk every second, and let’s compute when the world will end. God also instructed us to name things: Let n be the number of disks, and let mn be the number of moves for n disks. For small values of n, the move count can be computed by examination, and can be seen to be Mersenne numbers.

Number of disks n 0 1 2 3 4 5 6 Number of moves m(n) 0 1 3 7 15 31 63

The recurrence is: To move n disks, move the top n − 1 disks, then move the bottom disk, and finally move the top n − 1 disks onto the bottom one. The Mersenne equation describes this general relationship.

mn = mn−1 + 1 + mn−1 = 2mn−1 + 1

with initial condition m0 = 0. Oh yes, when will the world end? To move 64 disks from one diamond needle to another requires m(64) = 264 − 1 total moves. As you know, 1 century ≈ π billion seconds. Next, use the approximation 210 = 1, 024 ≈ 1, 000 = 103 to make a back-of-the-envelope calculation.

 6.4 264 − 1 moves ≈ 103 seconds

≈ 1019 seconds ≈ 3π · 109 · 109 seconds

Therefore, the monks will finish in about

3π billion billion seconds or 3 billion centuries

Scientists estimate the big bang occurred about 13.75 billion years ago. That’s about 0.1375 billion cen- turies. Therefore, the monks will finish their task when the known universe is about 22 times its current age!

References

Kelly-Bootle, S. (1981). The Devil’s DP Dictionary. McGraw-Hill paperbacks. McGraw-Hill. [page 35] cse 1400/mth 2051 discrete math notes 37

Recursion, Part 2

n(n−1) It is often easy to see if a given function f (n) solves an recurrence. Show that t(n) = 2 solves the recurrence tn = tn−1 + (n − 1) with initial condition t0 = 0.

n(n−1) (n−1)(n−2) Proof: If t(n) = 2 , then renaming the variable, t(n − 1) = 2 . And, (n − 1)(n − 2) t − + (n − 1) = + (n − 1) n 1 2 (n − 1)(n − 2) 2(n − 1) = + 2 2 n − 1 = ((n − 2) + 2) 2 n(n − 1) = = t 2 n And, yes t(0) = 0(0 − 1)/2 = 0 matches the initial condition too.

Binary search: To search for x in a sorted list of n > 0 values, compare x to the middle value m. Three cases: (1) x = m, x was found; (2) x < m, search the first half; (3) x > m, search the second half. Counting the number of comparisons, in the worst case, computes the time complexity of binary search. If x is not 1 found, the size of the problem is reduced by 2 after each comparison. With little lost of generality, let n = 2p be a power of 2. Then, the time complexity of binary search can be described by the recurrence  n  b(n) = b + 1, b(0) = 1 2

The base 2, lg n = log2 n satisfies this recurrence. Recall, some basic facts:

k a • lg n = k if and only if 2 = n • lg b = lg a − lg b • lg a · b = lg a + lg b • lg ab = b lg a

Pretend b(n) = lg n. Then b(n/2) = lg n/2 = lg n − lg 2 = lg n − 1. That is, b(n/2) + 1 = b(n), and b(1) = lg 1 = 0.

Mergesort: To sort a list of n > 0 values, split it into two halves. Sort each half, then merge the two back into one list. The time complexity of Mergesort can be modeled by the recurrence

m(n) = 2m(n/2) + n, m(1) = 0

The function m(n) = n lg n solves this recurrence The initial condition m(1) = 1 lg 1 = 0 is satisfied. And, n  n  2m(n/2) + n = 2 · lg + n 2 2 = n(lg n − 1) + n = n lg n

n n ASecond Order Example. Show that the function s(n) = sn = 2 + 3 solves the recurrence

sn = 5sn−1 − 6sn−2 with initial conditions s0 = 2, s1 = 5. cse 1400/mth 2051 discrete math notes 38

Substitute s(n − 1) = 2n−1 + 3n−1 and s(n − 2) = 2n−2 + 3n−2 into the recurrence

sn = 5sn−1 − 6sn−2 = 5(2n−1 + 3n−1) − 6(2n−2 + 3n−2) = [5(2n−1) − 6(2n−2)] + [5(3n−1) − 6(3n−2)] = [5(2n−1) − 3(2n−1)] + [5(3n−1) − 2(3n−1)] = [2(2n−1)] + [3(3n−1)] = 2n + 3n

And, the initial conditions are met s(0) = 20 + 30 = 2 and s(1) = 21 + 31 = 5.

Many things have recursive definitions. Shoes and ships and sealing-wax and cabbages and kings.

Strings:

Let A be an alphabet, think of the lower case English letters if you like. A string can be defined by this recursive definition. • The empty string λ is a string. • Each letter α ∈ A is a string. • If s and t are strings, then their concatenation st is a string. A length of a string has this recursive definition. Think of a string s as the first letter, called the head fol- lowed by the remaining letters called the tail. • The length of the empty string is 0. • The length of s = head : tail is 1 plus the length of the tail.

Binary Trees:

A (non-empty) binary tree is a collection of nodes. A node holds something, maybe a nut. Here’s a recur- sive definition of binary trees. • The empty tree τ is a binary tree. • A single node containing a nut is a binary tree. • If node is a node, and left and right are binary trees, then node left right is a binary tree. Here is an example binary tree.

4867

4 8

6 7 cse 1400/mth 2051 discrete math notes 39

Functions

Function:A deterministic mapping of input to output. Deterministic means for a given input, there is one and only one output. The output is uniquely determined by the input. Functions are often visualized as graphs in Cartesian spaces: 2-dimensions (x, y), 3-dimensions (x, y, z), or higher dimensional spaces.

Basic Notation and Definitions

A function is a relation that maps values in one set X uniquely into values of another set Y. The notation commonly used is f : X 7→ Y A function is a set of ordered pairs

{(x, y) : for each x ∈ X there is one and only one y ∈ Y such that f (x) = y.}

The set X is called the domain of the function f and the set Y is called its co-domain. The range of f is the set {y ∈ Y : (∃x ∈ X)( f (x) = y)}. The range is the subset of Y that is the image of values in the domain.

Functions have properties. • Onto: A function f : X 7→ Y is onto when its range is its co-domain.

(∀y ∈ Y)(∃x ∈ X)( f (x) = y)

(What do you think? Is this the same or different from (∃x ∈ X)(∀y ∈ Y)( f (x) = y)? Exercise: Draw graphs of functions from R to R that are or are not onto. I like to draw functions as arrows from one blob to a second. A function is onto if every value in the second blob is hit by some arrow. • One-to-one: A function is one-to-one if every value in the co-domain is the image of at most one value in the domain. If f (x1) = f (x2), then x1 = x2. Or, if two (or more) arrows hit the same spot, they started from the same location. (∀x1, x2 ∈ X)(( f (x1) = f (x2)) ⇒ (x1 = x2)) or (∀x1, x2 ∈ X)((x1 6= x2) ⇒ ( f (x1) 6= f (x2))) Exercise: Draw graphs of functions from R to R that are or are not one-to-one

The inverse of a function is a useful idea. A function f : X 7→ Y has an inverse function f −1 : Y 7→ X if and only if f is one-to-one and onto. For instance, f (x) = lg x and f −1(x) = 2x are inverses

lg 2x = 2lg x = x

Composition of functions is another useful idea given short shrift here. Functions can be chained together so that if f : X 7→ Y and g : Y 7→ U, then the composition g ◦ f : X 7→ U by the computation (g ◦ f )(x) = g( f (x)) cse 1400/mth 2051 discrete math notes 40

Cardinality is a concept given long shrift. Two sets X and Y have the same number of elements, the same cardinality, if there is a one-to-one and onto function from X to Y. The sets {0, . . . , 25} and {a,..., z} The sets N, Z and Q have the same cardinality, called ℵ0. The reals R are a larger set with cardinality ℵ1 > ℵ0.

Functions can be classified by type. A few types are (1) Polynomials; (2) Logarithmic and Exponen- tial functions; (3) Integer functions (floor, ceiling, mod, gcd, min, max, etc.); and Permutations.

Polynomials

There is plenty to know about polynomials. Polynomial evaluation via Horner’s rule is the only idea tested here. Here are some examples. Evaluate p(x) = x3 − x2 − x − 1 at x = 4.

Horner’s Rule @ x = 4 1 −1 −1 −1 4 12 44 1 3 11 43

Evaluate p(x) = 7x5 − 5x3 + 3x2 at x = −2.

Horner’s Rule @ x = −2 7 0 −5 3 0 0 −14 28 −46 86 −172 7 −14 23 −43 86 −172

The set of power functions is the default basis for polynomials. n o {xn : n ∈ N} = 1, x, x2, x3,...

Falling form another useful basis for polynomials.

{xn : n ∈ N} = {x(x − 1) ··· (x − n + 1) : n ∈ N} = {1, x, x(x − 1), x(x − 1)(x − 2),...}

There is just too much to know about polynomials, so let’s stop here and you can explore polynomial space on your own. cse 1400/mth 2051 discrete math notes 41

Logarithms and Exponentials

Exponentials produce a wide range of values, logarithms narrow the scope. Draw the graphs of y = 2x and y = lg x. 1. The common logarithm, the logarithm base 10, written as a function y = log x which means 10y = x. 2. The , the logarithm base e ≈ 2.718, written as a function y = ln x which means ey = x. 3. The binary logarithm, the logarithm base 2, written as a function y = lg x which means 2y = x. Logarithms simplify calculations: products become sums, quotients become differences, become products. • The log of a product is the sum of logs; the exp of a sum is the product of exps.

log (ab) = log a + log b; (xa+b = xa xb )

• The log of a quotient is the difference of logs; the exp of a difference is the quotient of exps.

 a  xa log = log a − log b; xa−b = b xb

• The log of a power is the power times the log; the exp of an product is the exp of an exp.   log ab = b log a; xab = (xa )b

Names and Numbers

Their is an exponential-logarithmic relationship between alphabet cardinality, string length, and the count of strings. Consider the decimal alphabet D = {0, . . . , 9} and strings of length n. • There is 1 = 100 decimal string of length 0, the empty string λ. • There are 10 = 101 decimal strings of length 1, the strings 0 to 9. • There are 10 = 102 decimal strings of length 2, the strings 00 to 99. In general, there are 10n decimal strings of length n. Think: You can name 10n things, people, plants, whatever with decimal strings of length n. On the other hand: • You can count nothing using no digits. • You can count from 0 to 9 = 101 − 1 using 1 digit. • You can count from 0 to 99 = 102 − 1 using 2 digits. In general, you can count from 0 to 10n − 1 using n digits. Think: Counting starts from an initial condi- tion. Zero is the default. But, you can count from x to x + (10n − 1) using n digits. There is nothing special about decimal, base 10. It is just more familiar. There are 2n binary strings of length n. And, you can count from 0 to 2n − 1 using n bits. There are 16n hexadecimal strings of length n. cse 1400/mth 2051 discrete math notes 42

And, you can count from 0 to 16n − 1 using n hexits. These problems are of the type: How many different strings can be written using an alphabet of b symbols and strings of length n.

The inverse problems are interesting too. 1. How many digits, bits, whatever, are needed to name k things? 2. How many digits, bits, whatever, are needed to write the number k? Let’s answer these questions for binary and generalize to other bases. Any natural k can be bound by powers of 2. One way to bound is 2n−1 < k ≤ 2n. If 2n−1 < k ≤ 2n, then at least n bits are needed to name k things. Test this: If 16 < k ≤ 32, then 5 bits are required to name k things. Given k, the number of bits n needed to name k things can be determined by these computations: (The ceiling function dxe will be described soon, look up.)

2n−1 < k ≤ 2n ⇒ n − 1 < lg k ≤ n ⇒ n = dlg ke

Another way to bound k is 2n−1 ≤ k < 2n. If 2n−1 ≤ k < 2n, then k can be written using n bits. Test this: If 8 ≤ k < 16, k can be written using 4 bits. Given k, the number of bits n to write k can be determined by these computations:

2n−1 ≤ k < 2n ⇒ n − 1 ≤ lg k < n ⇒ n − 1 = blg kc ⇒ n = blg kc + 1

(The floor function bxc will be described soon, look down.)

Questions and Problems

1. Show the log of a root is the log divided by the root

√ log a log b a = b

2. Show that logc a logb a = or logb a logc b = logc a logc b 3. Show the root of an exp is the exp of a quotient √ b a xa = x b

4. Exponential–Logarithmic symmetry ( ) ( ) alogb c = clogb a cse 1400/mth 2051 discrete math notes 43

Integer Functions

Integer function act on or produce integer results.

The Floor Function

The floor bxc or greatest integer function is the largest integer less than or equal to x. For instance,

 24   −24  bπc = 3 = 4 b17c = 17 b−πc = −4 = −5 b−17c = −17 5 5

Here’s a Cartesian plot of the floor for small values. Such graphs describe step functions.

y = bxc 3 2 1 x −2−1 0 1 2 3 −1 −2

Consider x ∈ R − N, bounded between by two consecutive integers n < x < n + 1. The floor of x is bxc = n. The floor of −x is b−xc = −n − 1.

−x x

−n − 1 −n 0 n n + 1

The Ceiling Function

The ceiling dxe or least integer function is the smallest integer greater than or equal to x. For instance,

 24   −24  dπe = 4 = 5 d17e = 17 d−πe = −3 = −4 d−17e = −17 5 5

Here’s a Cartesian plot of the ceiling for small values. cse 1400/mth 2051 discrete math notes 44

y = dxe 3 2 1 x −2−1 0 1 2 3 −1 −2

Consider x ∈ R − N, bounded between by two consecutive integers n < x < n + 1. The ceiling of x is dxe = n + 1. The ceiling of −x is d−xe = −n.

The Quotient and Remainder Functions

Given two integer a and m, return the quotient and remainder when a is divided by m. The quotient q = a ÷ m is the floor of a/m. Here, the input values a and m are integers with m 6= 0. j a k q = a ÷ m = is the quotient of a divided by m. m For instance,

5 ÷ 3 = 1 24 ÷ 5 = 4 17 ÷ 1 = 17 −5 ÷ 3 = −2 −24 ÷ 5 = −5 −17 ÷ 1 = −17

The integer quotient remains constant at value q for a = mq, mq + 1, . . . , mq + (m − 1), where it jumps to (q + 1) when a reaches (m + 1)q. Here’s a Cartesian plot for m = 3 and small values of a.

q = a ÷ 3 1

a −6 −5 −4 −3 −2 −1 0 1 2 3 4 5

−1

−2

The remainder r = a mod m is the remainder when a is divide by m. j a k r = a − mq = a − m(a ÷ m) = a − m is the remainder of a divided by m. m For instance,

5 mod 3 = 2 24 mod 5 = 4 17 mod 1 = 0 −5 mod 3 = 1 −24 mod 5 = 1 −17 mod 1 = 0

The remainder plot starts at r = 0 when a is a multiple of m. The remainder steps by 1 as a increments until it reaches the next multiple of m, where it returns to 0. Here’s a Cartesian plot for r = a mod 3 and small values of a. cse 1400/mth 2051 discrete math notes 45

r = a mod 3 2 1

a −6 −5 −4 −3 −2 −1 0 1 2 3 4 5

−1 cse 1400/mth 2051 discrete math notes 46

The Min and Max Functions

Return the smallest or largest value in a set. The minimum function of a set X is

min X = a ∈ X such that (∀x ∈ X)(a ≤ x) (e.g. min {−2, 3, 4, −5} = −5)

The maximum function of a set X is

max X = a ∈ X such that (∀x ∈ X)(a ≥ x) (e.g. max {−2, 3, 4, −5} = 4)

The Greatest Common Divisor

Given two naturals a and b, return largest natural that divides them both. For instance,

gcd(3, 5) = 1 gcd(5, 24) = 1 gcd(73, 37) = 1 gcd(6, 15) = 3 gcd(25, 35) = 5 gcd(30, 42) = 6

If it were easy to compute the prime factor decomposition of numbers, it would be easy to compute the great- est common divisor (gcd). For instance,

2 28 = 2 · 7 and 42 = 2 · 3 · 7 ∴ gcd(28, 42) = 2 · 7 = 14

Consider

3 2 2 4 2 1800 = 2 · 3 · 5 and 2 · 3 · 5 · 7 = 5670, ∴ gcd(1800, 5670) = 2 · 3 · 5 = 90

The gcd can be read off by choosing factors common to numbers using the minimum each occurs. Unfor- tunately, even though your third grade teacher taught it, factoring into primes is not easy. Fortunately, Euclid wrote down an easy computation of the gcd(a, b) about 2300 years ago. Euclid’s idea is to repeatedly compute quotients and remainders. This is the conversion between units idea: Keep breaking a unit (say yards) into another unit (say centimeters) until there is a common measure or the approximation is good enough. Here’s a typical Euclidean algorithm computation.

Dividend = Divisor · Quotient + Remainder a = m · q + r 5670 = 1800 · 3 + 270 1800 = 270 · 6 + 180 270 = 180 · 1 + 90 180 = 90 · 2 + 0

More later. The importance of this algorithm in fundamental applications may become clear. It is interest- ing that the Fibonacci sequence produces the worst case time complexity of the Euclidean algorithm.

Permutations

A permutation is a one-to-one function from a set X onto itself. There are n! = n(n − 1)(n − 2) ··· 2 · 1 permutations on an n-element set. Exercise: Write all permutations on a 2-element set. Do the cse 1400/mth 2051 discrete math notes 47

same for a 3-element set. How many are 2-element and 3-element permutation are there? How many 4- element permutations are there?

It is common to pick a subset of things and then place them in order. Give a set with n- n elements, there are ( k ) (n choose k) ways to select k of the n elements. There are k! ways to arrange k things. Therefore, there are

n n! n! · k! = · k! = = n(n − 1)(n − 2) ··· (n − k + 1) k k!(n − k)! (n − k)! permutations of n things taken k at a time. 35 35·34·33 Given 35 people there are ( 3 ) = 3! ways to choose three of them. There are 6 = 3! ways to assign three people to roles: (first, second, third). Therefore, there are

35 · 3! = 35 · 34 · 33 ways to fill 3 positions given 35 people. 3

Permutation can be represented by cyclic notation. The permutation 0 7→ 1, 1 7→ 2, 2 7→ 0, can be represents by the notation [0 1, 2] The 6 permutations on three (0, 1, 2) values can be represented by

[0][1][2] maps (0, 1, 2) 7→ (0, 1, 2)[0, 1][2] maps (0, 1, 2) 7→ (1, 0, 2) [0, 2][1] maps (0, 1, 2) 7→ (2, 1, 0)[1, 2][0] maps (0, 1, 2) 7→ (0, 2, 1) [0, 1, 2] maps (0, 1, 2) 7→ (2, 0, 1)[0, 2, 1] maps (0, 1, 2) 7→ (1, 2, 0)

n The number of k cycle permutations of n things is denoted by Stirling’s number of the first kind [k], read “n cycle k”. n Stirling Numbers of the First Kind [k] Cycle k 0 1 2 3 4 5 6 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 n 2 0 1 1 0 0 0 0 3 0 2 3 1 0 0 0 4 0 6 11 6 1 0 0 5 0 24 50 35 10 1 0 cse 1400/mth 2051 discrete math notes 48

The Pigeonhole Principle

When many things are placed in a few boxes, some box may contain several things. Place n pigeons in a coop with m pigeonholes. The pigeonhole principle states: If there are more pigeons than pigeonholes (n > m) some hole contains two or more pigeons, a bigger coop is needed. The 50¢ term for the pigeonhole principle is Dirichlet’s box principle. Expressing the pigeonhole principle as its contrapositive is useful. If no pigeonhole contains more than one pigeon, there are fewer pigeons than holes, that is, n ≤ m Here are some examples of the pigeonhole principle. • If 5 naturals are divided by 3, at least two will have the same remainder: There are 5 numbers (pigeons) to place in 3 remainders (holes) {0, 1, 2}. Mapping 5 values to 3 values cannot be one-to-one. • Pretend there is a container of red, green, and blue widgets. How many must be drawn to have two of same color? The answer is 4. Do you see why? One draw cannot give a color pair, two draws could, but there is no guarantee, but with three draws, there must be at least a pair of the same color. pair. How many must be drawn if there are widgets of n colors? • In a set of 27 words, at least 2 will start with the same letter. In a set of 53 words, at least 3 will start with the same letter. • If n people shake other people’s hand why must some pair shake the same number of hands? • In a room 366 or more people at least two will share a birthday. In a room of 733 people, at least 3 will have the same birthday. In a room of 13 or more people at least two will share the same birth month. In a room of 25 or more people at least three will share the same birth month. The pigeonhole principle can be stated in terms of onto and one-to-one functions. • If |X| > |Y|, then there does not exist any one-to-one functions f : X → Y. A large set must over-cover some element in a smaller set. If there are more pigeons than holes, some hole will hold two or more pigeons.

(|X| > |Y|) ⇒ (∀ f : X → Y)(∃ x0, x1 ∈ X, x0 6= x1)( f (x0) = f (x1)) The conclusion (right-hand side) of the above implication can be stated as: For every function mapping X to Y there are two different values in X that map to the same value in Y. • If |X| < |Y|, then there does not exist any onto function f : X → Y. A small set cannot functionally cover a larger set. If there are fewer pigeons than holes, some hole will be empty. (|X| < |Y|) ⇒ (∀ f : X → Y)(∃ y ∈ Y)(∀ x ∈ X)( f (x) 6= y) The conclusion (right-hand side) of the above implication can be stated as: For every function mapping X to Y there is a y ∈ Y and no value of x ∈ X maps to it. cse 1400/mth 2051 discrete math notes 49

Generalize Pigeonhole Principle

Let n be the number of pigeons to be placed in m pigeonholes. Let p(k) be the number of pigeons place in pigeonhole k for k = 0, 1, . . . , (m − 1). The sum of pigeons in holes equals the count of pigeons.

∑ p(k) = n 0≤k

Here’s what can be decided. 1. Some pigeonhole contains at least dn/me pigeons.  l n m (∃ k ∈ Z ) p(k) ≥ m m

Let there be n = 35 pigeons and m = 9 pigeonholes. Some hole contains at least d35/9e = 4 pigeons. If each hole contained 3 or fewer pigeons, there would only be 9 · 3 = 27 of fewer pigeons, fewer than 35. 2. Some pigeonhole contains bn/mc or fewer pigeons.  j n k (∃ k ∈ Z ) p(k) ≤ m m

Let there be n = 35 pigeons and m = 9 pigeonholes. Some hole contains b35/9c = 3 or fewer pigeons. If each hole contained 4 or more pigeons, there would 9 · 4 = 36 or more pigeons, more than 35. Here are direct proofs of the generalized pigeonhole principle. 1. If every pigeonhole contains fewer than dn/me pigeons, then, p(k) ≤ dn/me − 1 for all k = 0, 1, . . . , (m − 1). Summing p(k) over each hole gives, l n m  l n m n = ∑ p(k) ≤ ∑ − 1 = m − m 0≤k

Notice that l n m n < + 1 m m Therefore, l n m  n  n ≤ m − m < m + 1 − m = n m m A contradiction. 2. If every pigeonhole contains more than bn/mc pigeons, then, p(k) ≥ bn/mc + 1 for all k = 0, 1, . . . , (m − 1). Summing p(k) over each hole gives, j n k  j n k n = ∑ p[k] ≥ ∑ + 1 = m + m holes holes m m

Notice that j n k n > − 1 m m Therefore, j n k  n  n ≥ m + m > m − 1 + m) = n m m A contradiction. cse 1400/mth 2051 discrete math notes 50

Relations

Relations are non-deterministic. Functions are deterministic. If asked: What integer is larger than 7? There are many correct answers: 8, 9, . . . . It cannot be determined how someone would answer. If asked: What is the value of the function f (n) = n2 − n − 1 at n = 7? There is only one correct answer: f (7) = 41. You are familiar with several relations:

x ≤ y (less than or equal) X ⊆ Y (subset) m\a (evenly divides)

Here are a few more. • Equality: x = y: • Homogeneous coordinates (a, b) ∝ (c, d): The ratio (fractions) of both pairs are equal a/b = c/d. • Congruence mod m (a ≡ b mod m): Both a and b have identical remainders when divided by m.

• Parallel lines (`0 k `1): The slope of both lines are equal.

• Perpendicular lines (`0 ⊥ `1): The product of slopes is −1 (there is a better definition using inner prod- ucts). • Approximately equal (x ≈ y): The absolute difference is small |x − y| ≤ e. • Relatively prime (a ⊥ b): The only common divisor of a and b is 1. • Element (x ∈ X): the value x is a member of set X. Some things to note: • The value of a relational statement is either True or False. • Infix notation x ∼ y is used to denote a relation. • Relation are, in general, one-to-many.

Relations have properties. A relation is: • Reflexive if every element is related to itself:

(∀a)(a ∼ a)

Exercise: List relations that are and are not reflexive. • Symmetric if elements commute about the relational symbol:

(∀a, b)(a ∼ b ⇒ b ∼ a)

Exercise: List relations that are and are not symmetric. • Antisymmetric if only equal values commute about the relational symbol:

(∀a, b)((a ∼ b ∧ b ∼ a) ⇒ (a = b))

or, in the contrapositive (∀a, b)(a 6= b ⇒ ((a 6 ∼b) ∨ (b 6 ∼a))

Exercise: List relations that are and are not antisymmetric. cse 1400/mth 2051 discrete math notes 51

• Transitive if the relation hop-scotches, from a to b and from b to c, then there is a direct path from a to c.

(∀a, b, c)((a ∼ b ∧ b ∼ c) ⇒ (a ∼ c))

Exercise: List relations that are and are not transitive.

Representations of relations

A relation can be visualized as an adjacency matrix. Reflexive, symmetric, antisymmetric and func- tion can be easily detected by a relation’s adjacency matrix. Consider the set D.A reflexive relation where (a ∼ a) for all values a.A reflexive relation where there is a 1 at each diagonal entry of its adjacency matrix. The less than or equal relation a ≤ b is reflexive.

0 1 2 3 ··· 9 0 1 x x x ··· x 1 x 1 x x ··· x 2 x x 1 x ··· x 3 x x x 1 ··· x ...... ··· . 9 x x x x ··· 1

The values in off-diagonal entries do not effect reflexivity. A symmetric relation where (x ∼ y) ⇒ (y ∼ x) for all values of x and y. When represented as an adja- cency matrix, symmetry means that if there is a 1 in row x, column y, then there is a 1 in row y, column x. Congruence mod 3 is a symmetric relation. On domain D, its adjacency matrix on D is:

0 1 2 3 ··· 9 0 1001 ··· 1 1 0100 ··· 0 2 0 010 ··· 0 3 1001 ··· 1 ...... ··· . 9 1001 ··· 1

In this case the lower triangle completely determines the upper triangle. Values along the diagonal are unconstrained. An easy way to say this is: The matrix is equal to its transpose, which interchanges rows and columns. An antisymmetric relation is one where ((x ∼ y) ∧ (y ∼ x)) ⇒ (x = y) for all values of x and y. When represented as an adjacency matrix, antisymmetry means that if there is a 1 in row x, column y, then there is cse 1400/mth 2051 discrete math notes 52

a 0 in row y, column x. The less than relation a < b is antisymmetric. its adjacency matrix on D is:

0 1 2 3 ··· 9 0 0111 ··· 1 1 0 011 ··· 1 2 0 0 01 ··· 1 3 0 0 0 0 ··· 1 ...... ··· . 9 0 0 0 0 ··· 0

Transitivity is not so easy to see from a relation’s adjacency matrix. The idea would be to trace: If there is a 1 in row x and column y, is there also a 1 in row y and column z, and if so, is there a 1 in row x and column z. This may be too hard for trace.

A function is a special type of relation. A function is a deterministic relation. The adjacency for a function has one 1 in each row. Every value in the domain maps to one and only one value in the co- domain. The adjacency for an onto function has at at least one 1 in each column. Every value in the co-domain is the image of at least one value in the domain. The adjacency for a one-to-one function has at at most one 1 in each column. No value in the co-domain is the image of more than one value in the domain. A permutation is a one-to-one and onto function from a set to itself. One particular permutation is [0, 1, 2, 3] which has adjacency matrix: 0 1 2 3 0 0100 1 0 010 2 0 0 01 3 1 0 0 0 A permutation has one 1 in each row (it is one-to-one) and one 1 in each column (it is onto).

Counting Functions

n 3 Previously, you learned there are 22 functions f : Bn 7→ B. For instance, there are 22 = 256 functions from 3 Boolean values p, q, r to True or False. The general rule for counting functions is:

Theorem 1. There are |Y||X| functions from domain X to co-domain Y.

For the Boolean case, |Y| = |B| = 2 and |X| = |Bn | = 2n. As an example of the general rule, there are 32 functions from {a, b} to {0, 1, 2}: Map a to one of 0, 1, 2 and map b to one of 0, 1, 2. Each of these 9 functions can be represented by one of the 9 sets below.

f0 = {(a, 0), (b, 0)} f1 = {(a, 0), (b, 1)} f2 = {(a, 0), (b, 2)}

f3 = {(a, 1), (b, 0)} f4 = {(a, 1), (b, 1)} f5 = {(a, 1), (b, 2)}

f6 = {(a, 2), (b, 0)} f7 = {(a, 2), (b, 1)} f8 = {(a, 2), (b, 2)} cse 1400/mth 2051 discrete math notes 53

Adjacency matrices provide a simple proof of the theorem.

Proof. For every x in the domain, choose one (and only one) y in the co-domain and include the pair (x, y) in function, that is f (x) = y. That is, for every row (x) in an adjacency matrix, choose one column (y) for the mapping. There are |X| rows and |Y| choices for each row. Therefore, there are |Y||X| possible functions

As another example, if |X| = 6 and |Y| = 5, then there are 56 functions from X to Y.

Counting one-to-one functions is easy. Let |X| = m and |Y| = n where m ≤ n. Why is there a restriction m ≤ n for counting one-to-one functions? To be one-to-one, once there is a 1 in some column of an adjacency matrix there can be no other 1’s in that column. So, first there are n columns to choose from, then second there are (n − 1) column choices, then (n − 2), and so on down to (n − m + 1) choices for the last value in X. In total then, there are n! n(n − 1)(n − 2) ··· (n − m + 1) = (n − m)!

one-to-one functions from X to Y when |X| = m and |Y| = n.

Counting Relations

Counting relations is easy too. The adjacency matrix for a relation between X and Y has dimen- sions m × n when |X| = m and |Y| = n. That is, there is a total of mn entries in the adjacency matrix and each can be either a 0 or a 1.

Theorem 2. There are 2mn relations between X and Y when |X| = m and |Y| = n.

A relation between X and Y is a subset of the Cartesian product X × Y = {(x, y) : (x ∈ X) ∧ (y ∈ Y)}. The cardinality of X × Y is mn. Therefore X × Y has 2mn subsets and each is a relation. Recall row mn of Pascal’s triangle sums to 2mn, and each value in a row counts subsets of a given size.

Counting reflexive relations on X is easy. The values on the main diagonal of the adjacency ma- trix for a reflexive relation are all 1’s. There are m2 − m off-diagonal entries. Each of these values can be 2 either 0 or 1, so there are 2m −m = 2m(m−1) reflexive relations on an m-element set. This is a use for the sum 1 + 2 + ··· + (m − 1) = m(m − 1)/2 result too. There is one entry in the north-east corner of the matrix, two entries in next diagonal, then three entries, all the way to (m − 1) entries in the diagonal above the main diagonal.

Counting symmetric relations on X is easy too. Values on the main diagonal can be 0 or 1. And, values in the upper triangle completely determine values in the lower triangle. There√ are m(m + 1)/2 values along the main diagonal and upper triangle. Therefore, there are There are = 2m(m+1) symmetric relations on an m element set. cse 1400/mth 2051 discrete math notes 54

Equivalences

Grouping things together that are similar is a human trait. Library books are grouped by subject. fastidious people organize their closets in various ways. Relational properties make grouping by some equivalence precise. Recall the context: There is a relation ∼ on a set U, written u ∼ v on pairs (u, v) ∈ U × U. Here are two equivalences. 1. Congruence mod m: a ≡ b (mod m) on integers a and b, for fixed modulus m. The relation a ≡ b (mod m) means a and b have identical remainders when divided by m, for instance 5 ≡ 14 (mod 3) and 5 6≡ 14 (mod 4). 2. Homogeneous coordinates: Given non-origin points (a, b) and (c, d), Say (a, b) ∝ (c, d) (proportional b to) if the ratio (fractions) of both points are equal a/b = c/d. Related pairs lie along rays y = a x from the origin.

Three properties define equivalence. • An object is equivalent to itself. What every property “it” has, “it” has it too. This is the reflexive prop- erty: (∀u ∈ U)(u ∼ u) Instances: 17 divided by 5 has the same remainder as 17 divided by 5. The point (3, 7) has the same ratio as (3, 7). Both have ratios 3/7. • If a and b share a property, then b and a do too. This is the symmetric property: (∀u, v ∈ U)((u ∼ v) ⇒ (v ∼ u) Instances: If 17 and 32 have the same remainder when divided by 5 (they do), then 32 and 17 have the same remainder too. The contrapositive is True too; The points (3, 6) and (5, 10) have the same ratio (1/2). Therefore, (5, 10) and (3, 6) have the same ratio. • If a and b share a property and b and c share the same property, then a and c have that property. This is the transitive property: (∀a, b, c ∈ U)((a ∼ b) ∧ (b ∼ c) ⇒ (a ∼ c) Instances: If 17 and 32 have the same remainder when divided by 5 and 32 and 47 have the same re- mainder when divided by 5, then 17 and 47 have the same remainder too. The points (3, 6) and (6, 12) have the same ratios 1/2. The points (6, 12) and (2, 4) have the same ratios 1/2. Therefore, (3, 6) and (2, 4) have the same ratio. To show a relation is an equivalence, demonstrate that it is reflexive, symmetric, and transitive. Here’s an example. Let f : N 7→ N be a function from the naturals to the naturals. Define the relation a ∼ b to mean f (a) = f (b). • Reflexive: For every number a, a ∼ a because f (a) = f (a). • Symmetric: For every pair a, b: If a ∼ b, then b ∼ a because ( f (a) = f (b)) ⇒ ( f (b) = f (a)). • Transitive: For every triple a, b, c: If a ∼ b and b ∼ c, then a ∼ c because ( f (a) = f (b)) ∧ ( f (b) = f (c)) ⇒ ( f (a) = f (c)). cse 1400/mth 2051 discrete math notes 55

Equivalence classes

An equivalence partitions a set into equivalence classes.A partition of U is a collection of non- empty disjoint subsets of U that cover all of U. For instance, {{0, 2} , {1, 3}} is one of 7 partitions of {0, 1, 2, 3} into 2 subsets: The sets {0, 2} and {1, 3} are non-empty and disjoint ({0, 2} ∩ {1, 3} = ∅). and {0, 2} ∪ {1, 3} = {0, 1, 2, 3}. Congruence mod 2 partitions the integers into the “evens” and “odds.”

[0]2 = {0, ±2, ±4, ±6, ±8, ±10, . . .}

[1]2 = {1, 1 ± 2, 1 ± 4, 1 ± 6, 1 ± 8, 1 ± 10, . . .} Modulo 3 partitions the integers into three equivalence classes.

[0]3 = {0, ±3, ±6, ±9, ±12, ±15, . . . , 3n,...} (A remainder of 0 when divided by 3.)

[1]3 = {1, 1 ± 3, 1 ± 6, 1 ± 9, 1 ± 12, 1 ± 15, . . . , 1 ± 3n,...} (A remainder of 1 when divided by 3.)

[2]3 = {2, 2 ± 3, 2 ± 6, 2 ± 9, 2 ± 12, 2 ± 15, . . . , 2 ± 3n,...} (A remainder of 2 when divided by 3.) In general, congruence mod m partitions the integers into m equivalence classes. Homogeneous coordinates partition the into rays emanating from the origin. Points on a ray are equivalent.

On a finite set, the number of equivalences can be counted. The number of partitions of n n things into k subsets is denoted by Stirling’s number of the second kind { k }, read “n subset k”. n Stirling Numbers of the Second Kind { k } Cycle k 0 1 2 3 4 5 0 1 0 0 0 0 0 1 0 1 0 0 0 0 n 2 0 1 1 0 0 0 3 0 1 3 1 0 0 4 0 1 7 6 1 0 5 0 1 15 25 10 1

Questions and Problems

1. Let X and Y be sets of integers. Say X ∼ Y if the sum of values in X equals the sum of values in Y. ∑ x = ∑ y x∈X y∈Y cse 1400/mth 2051 discrete math notes 56

Show that this relation on sets is an equivalence. 2. Say a ∼ b if |b − a| ≤ 1. Show that this relation is reflexive and symmetric, but not transitive. cse 1400/mth 2051 discrete math notes 57

Orders

Putting things in order is a human trait, although some rage against it. Here are some ways to order things.

Less than or equal on the integers is a total order. Every pair of integers can be ordered: a < b, − + a = b, or a > b. The real line ∞ ∞ visualizes this order. (Of course, ±∞ are not integers. They merely suggest the line is unbounded.)

Subset on the power set of a set is a partial order. The power set X is the set of all of subsets of X. The power set of ∅ is {∅}. The power set of {0} is {∅, {0}}. The power set of {0, 1} is {∅, {0} , {1} , {0, 1}}. The power set of {0, 1, 2} is

{∅, {0} , {1} , {2} , {0, 1} , {0, 2} , {1, 2} , {0, 1, 2}}

Note the logarithmic–exponential relationship between set and power set cardinality: n ↔ 2n. If X has cardinality n, its power set has 2n elements. Subset is a partial order. Some subsets can be ordered, {0} ⊆ {0, 1}, but some subsets cannot {0} 6⊆ {1, 2} and {1, 2} 6⊆ {0}. A point, line segment, , cube, hyper-cube, and so on can be used to visualize the subset order.

{2}

∅ ∅ {0, 2} {0} {1} {1, 2} {0, 1} ∅ {0} {1}

∅ {0} {0, 1} {0, 1, 2}

The subset relation can be imagined as a bit vector. For the 2 element (2-dimensional) example 00 is the empty set and 11 = {0, 1}. Tracing through the vertices in Gray code order 00 7→ 01 7→ 11 7→ 10 is useful and recursive. To generate the 3-bit Gray code order: Prefix each coordinate above with 0, then prefix a 1 on each coordinates in reverse order.

000 7→ 001 7→ 011 7→ 010 7→ 110 7→ 111 7→ 101 7→ 100

Divides on the natural N is a partial order. A natural m divides another when a mod m = 0, also written as a\m. That is a = mq for some quotient q. For instance 7 divides 42 since 42 = 7 · 6. Some pairs can be ordered, 3\15, but some cannot be totally ordered 4 6 \15 and 15 6 \4. Here’s a graph of divides for the first few natural numbers. An edge a → b means a divides b. Transitive and reflexive edges are not shown, they can be inferred by context. Interestingly, 1 is the smallest value: 1 divides every natural. And, 0 is the largest: Every natural divides 0. cse 1400/mth 2051 discrete math notes 58

7

9

3 6

1 2 4 8 0

5 10

11

Three properties define partial orders. Antisymmetry is critical for orders. • Antisymmetry: If u is before v and v is before u, then u and v must be equal. This is the antisymmetric property: (∀u, v ∈ U)((u ∼ v) ∧ (v ∼ u) ⇒ u = v. Instances: (5 ≤ 6 and 6 ≤ 5) imply 5 = 6. Weird, but True because the assumption is False, under the usual interpretation of ≤. • Reflexive: An object is not before or after itself, but is related to itself. This is the reflexive property: (∀u ∈ U)(u ∼ u) A strict order such as less than (<) removes the reflexive restriction. • If a is before b and b is before c then a is before c. This is the transitive property: (∀a, b, c ∈ U)((a ∼ b) ∧ (b ∼ c) ⇒ (a ∼ c) – 12 ≥ 7 and 7 ≥ 5. Therefore 12 ≥ 5. – If {0, 1} ⊆ {0, 1, 2} and {0, 1, 2} ⊆ {0, 1, 2, 3}, then {0, 1} ⊆ {0, 1, 2, 3}. – 7\21 and 21\63. Therefore 7\63. – (12 ≥ 20 and 20 ≥ 25) imply 12 ≥ 25. (When you start from False assumptions, you get weird results.)

Some think antisymmetry means not symmetric. That’s not right! Equality is both symmetric and antisymmetric, so they cannot be negations of each other, that would be inconsistent. • Symmetry means (a ∼ b) ⇒ (b ∼ a) which can be reduced to ¬(a ∼ b) ∨ (b ∼ a). (Recall, p ⇒ q is equivalent to ¬p ∨ q.) Therefore, by De Morgan’s laws not symmetric means (a ∼ b) ∧ ¬(b ∼ a): The value a is related to b, but b is not related to a. Think in terms of a relation’s adjacency matrix. Not symmetric means that for some row i and column j, the entries are not the same: A 0 in one entry and a 1 in the other. • On the other hand antisymmetric means something entirely different. Antisymmetry means (a ∼ b) ∧ (b ∼ a) ⇒ a = b. If a and b are not equal, they cannot commute about the relational symbol. The adjacency matrix on the left below represents a relation that is neither symmetric nor antisymmetric. But to be both symmetric and antisymmetric does not necessarily imply equality, as the matrix on the right shows. 0 1 2 0 1 2 0 1 0 1 0 1 0 0 1 0 0 1 1 0 0 0 2 1 0 1 2 0 0 1 cse 1400/mth 2051 discrete math notes 59

Modular Arithmetic

Military time is modular (cyclic). Midnight starts at 00 : 00 (zero-zero hundred). Each hour incre- ments the count. For example, 11 pm is 23 : 00 (23 hundred) and 24 : 00 is equivalent to 0 : 00. Angles too are modular. Let 0 ≤ θ < 2π, All angles θ + 2πk (or θ + 360o k) k ∈ Z are equivalent: These equivalent angles define the same ray from an origin. The unit vector hcos θ, sin θi in direction θ is useful in some contexts.

The integers mod m, Zm = {0, 1, . . . , (m − 1)} models cyclic relationships. Examples are:

Z3 = {0, 1, 2} , Z5 = {0, 1, 2, 3, 4} , and Z6 = {0, 1, 2, 3, 4, 5}

Think: Remainders when an integer is divided by 3, 5 or 6. Let the modulus m ∈ N+ be fixed (let m = 7 if you want). The function

a mod m : Z 7→ Zm is the remainder when a is divided by m.

For instance,

7 mod 3 = 1 (7 = 3 · 2 + 1) −7 mod 3 = 2 (−7 = 3 · (−3) + 2) 7 mod 5 = 2 (7 = 5 · 1 + 2) −7 mod 5 = 3 (−7 = 5 · (−2) + 3)

 a   7   −7   7   −7  The quotient is q = m . In the examples: 3 = 2, 3 = −3, 5 = 1, and 5 = −2. The remainder r = a mod m can be computed by the formula: j a k a mod m = a − m m

Write a ≡ b (mod m) to state that a and b have the same remainder when divided by m. Read this as: a is congruent to b mod m. Another useful interpretation of a ≡ b (mod m) is: The difference a − b is a multiple of m, or m divides a − b. In most instances congruence mod m is used to reduce a large integer a to a modular integer 0 ≤ b < m. But, the equivalence is True in other instances as well.

7 ≡ 13 (mod 3), 27 ≡ 42 (mod 5), 52 ≡ 104 (mod 13), 37 ≡ 73 (mod 2)

When the context is clear, the syntactic sugar “mod m” can be dropped: mod m is almost always only written on one side of a congruence. Of course, the congruence is not always True.

7 6≡ 12 (mod 3) 27 6≡ 41 (mod 5) 52 6≡ −114 (mod 13) −37 6≡ 72 (mod 2)

Arithmetic works on modular numbers. Well, addition, subtraction, and multiplication work as expected, but division only works sometimes. Consider sums: 7 ≡ 13 (mod 3) and 5 ≡ −7 (mod 3). This implies 7 + 5 ≡ 13 − 7 (mod 3) = 12 ≡ 6 (mod 3). The general statement is: If a ≡ b (mod m) and c ≡ d (mod m), then (a + c) ≡ (b + d)(mod m). cse 1400/mth 2051 discrete math notes 60

Follow the proof: It the differences (a − b) and (c − d) are multiples of m.

If (a − b) = em and (c − d) = f m for some integers e and f .

Then, (a + c) − (b + d) = (a − b) + (c − d) = em + f m = (e + f )m is a multiple of m too. That is, (a + c) = (b + d) mod m Here’s the mod 5 addition table. See how values of sums cycle from row-to-row and column-to-column. Also notice the 0 in the table: These identify additive inverse pairs: A number and its negative: a + (−a) = 0. Mod 5 addition + 0 1 2 3 4 0 0 1 2 3 4 1 1 2 3 4 0 2 2 3 4 0 1 3 3 4 0 1 2 4 4 0 1 2 3 Consider products: 7 ≡ 13 (mod 3) and 5 ≡ −7 (mod 3) and this implies 7 · 5 ≡ 13 · (−7)(mod 3) = 35 ≡ −91 (mod 3). Both 35 and −91 have a positive remainder of 2 when divided by 3:

35 = 3 · 11 + 2 and − 91 = 3 · (−31) + 2

The general statement is: If a ≡ b (mod m) and c ≡ d (mod m), then ac ≡ bd (mod m). Follow the proof: If the differences (a − b) = em and (c − d) = f m are multiples of m, then difference

ac − bd = (ac − bc) + (bc − bd) = c(a − b) + b(c − d) = cem + b f m = (ce + b f )m is a multiple of m.

That is, ac = bd mod m Here’s the mod 5 multiplication table. See how values of product appear in a somewhat random order from row-to-row and column-to-column. Also notice the 1 in the table: These identify multiplicative inverse pairs: A number and its reciprocal: a · a−1 = 1.

Mod 5 multiplication ∗ 0 1 2 3 4 0 0 0 0 0 0 1 0 1 2 3 4 2 0 2 4 1 3 3 0 3 1 4 2 4 0 4 3 2 1

But, when the modulus m is not a prime, some modular numbers will not have reciprocals. cse 1400/mth 2051 discrete math notes 61

Modular Arithmetic, Part 2

When the modulus m is not a prime, some numbers will not have reciprocals. Consider the mod 6 multiplication table. Only 1 has a multiplicative inverse.

Mod 6 multiplication ∗ 0 1 2 3 4 5 0 0 0 0 0 0 0 1 0 1 2 3 4 5 2 0 2 4 0 2 4 3 0 3 0 3 0 3 4 0 4 2 0 4 2

Exercise: Fill out the mod 8 multiplication table to show only 1, 3, 5, and 7 have reciprocals mod 8. In each of these cases 8 and 1, 3, 5, and 7 are relatively prime, that is 1 is their greatest common divisor. Be- cause of this negative result, in practice, the modulus m is almost always a : m ∈ P, where P = {2, 3, 5, 7, 11, . . .} is the set of prime numbers. The modulus m is usually large too, except for class examples.

Inverses are used to solve equations. To solve a linear equation, say 7x + 3 = 0: Add −3 (the additive inverse of 3) to both sides. Then, multiply by 7−1 (the multiplicative inverse of 7). Addition and multiplication are fundamental. Mathematicians enlarge natural numbers N to integers Z, then rationals Q, then reals R and complex C numbers as equations to be solved became more intricate. Next, a tangent is followed, but it will lead back to solving equations.

Randomness can be used to encrypt messages. Modular arithmetic introduces a simple way to generate a sequence of pseudo-random numbers. Numbers generated so simply are not random at all! But, this randomization introduces some basic concepts. Random numbers are used in cryptography, computer simulations, evaluation of integrals, population sampling, and many other applications. Given a seed (initial condition, basis) s0, consider the recurrence:

sn = (asn−1 + b) mod m for n ≥ 1

where the modulus m > 0 and parameters a and b are (usually) non-zero modular integers between 1 and (m − 1). Here’s an example recurrence

sn = (5sn−1 + 1) mod 16 (16 is a power of 2)

Let the seed be s0 = 0. This example recurrence generates the sequence

h0, 1, 6, 15, 12, 13, 2, 11, 8, 9, 14, 7, 4, 5, 10, 3, 0, 1, 6, . . .i

By the pigeonhole principle a pseudo-random sequence of this type will always repeat (have period) after m or fewer iterations. cse 1400/mth 2051 discrete math notes 62

Given a message, encrypt it. Randomness can be useful in encryption. A Caesar cipher is simple to code and easy to break. Caesar ciphers simply shift each letter n cyclically by some value k > 0. Consider the 26 letter English upper-case alphabet and Caesar encryption function

n 7→ (n + k) mod 26 (maybe a single shift n 7→ (n + 1) mod 26)

Can you determine the shift k for this example? Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD To decrypt a letter n compute

n 7→ (n − k) mod 26 (for a two letter forward shift is undone by n 7→ (n − 2) mod 26)

A Caesar cipher is easy to break. A brute-force approach for the 26 letter alphabet searches each of the 25 possible shifts to find plausible interpretations.

Affine Ciphers

An affine cipher encodes a letter x as y = (ax + b) mod m. On the upper-case English alphabet the encod- ing function, for some parameters a and b, is

x 7→ (ax + b) mod 26 = y mod 26 (for instance y = 5x + 1 mod m)

To decode a message y, solve the equation below for x

y = (ax + b) mod 26

From basic algebra: To find x: Add −b to both sides. Then multiply both sides by a−1 the reciprocal of a mod m = 26. To solve the equation

y = (ax + b) mod 26 compute x = a−1(y − b) mod 26

For instance, to solve 3 = (5x + 1) mod 26 for x, compute

x = 5−1(3 − 1) = 5−1 · 2 mod 26

The difficulty is computing 5−1 mod 26. A brute-force approach computes products until a 1 is found. For the example, it takes a while to discover 5 · 21 = 105 = 26 · 4 + 1 = 1 mod 26.

Multiplication by 5 mod 26 ∗ 0 1 2 3 4 5 6 7 8 9 10 11 ··· 21 5 0 5 10 15 20 25 4 9 14 19 24 3 ··· 1

Fortunately, there is a better algorithm, called the extended Euclidean algorithm. First, use the Euclidean algorithm to compute gcd(5, 26).

Dividend = Divisor · Quotient + Remainder a = m · q + r 26 = 5 · 5 + 1 5 = 1 · 5 + 0 cse 1400/mth 2051 discrete math notes 63

Second, fill out a magic table: (To be explained below)

5 5 Quotients 1 0 1 5 0 1 5 26 +1 −1 +1 Determinants

Conclude that 1 · 26 − 5 · 5 = 1 = −5 · 5 mod 26 or −5 = −5 + 26 = 21 mod 26 is the inverse of 5 mod 26. Therefore, the solution to

3 = (5x + 1) mod 26 is x = 5−1 · 2 = 21 · 2 = 42 = 16 mod 26

And, the solution can be checked:

(5 · 16 + 1) = 81 = (26 · 3 + 3) = 3 mod 26

The above needs some explanation. First, the Euclidean algorithm computes the greatest common divisor of two natural numbers. Here is example of the algorithm. The gcd(17, 73) = 1 is the last non-zero remainder, the last divisor.

Dividend = Divisor · Quotient + Remainder a = m · q + r 73 = 17 · 4 + 5 17 = 5 · 3 + 2 5 = 2 · 2 + 1 2 = 1 · 2 + 0

And, here’s another example for the computation of gcd(52, 92) = 4, the last non-zero remainder, the last divisor. Dividend = Divisor · Quotient + Remainder a = m · q + r 92 = 52 · 1 + 40 52 = 40 · 1 + 12 40 = 12 · 3 + 4 12 = 4 · 3 + 0 Exercise: Pick a few pairs of numbers and compute their gcd.

Extended Euclidean algorithm

The goal is to solve linear congruence equations by extending the Euclidean algorithm. Consider the linear congruence.

ax + b = y mod m (For instance, 17x + 3 = 5 mod 73, or 17x = 2 mod 73) cse 1400/mth 2051 discrete math notes 64

Given a and m, the extension of the Euclidean algorithm computes the values of t and s such that

g = gcd(a, m) = at + ms (called Bézout’s identity)

Pretend that g = 1 and write this equation mod m:

1 = at + ms = at mod m (that is, at = 1 so that a and t are multiplicative inverses.)

A little matrix algebra makes the computation of s and t almost fool-proof. The matrix " # x y A = (has determinant xw − zy.) z w

The identity matrix is " # 1 0 I = (its determinant is det I = 1.) 0 1 A magic table uses quotients from the Euclidean algorithm to compute t and s such that

at + ms = gcd(a, m)

The computation gcd(73, 17) = 1 above produced quotients h4, 3, 2, 2i. Here is the magic table for this example. 4 3 2 2 Quotients 1 0 1 3 7 17 0 1 4 13 30 73 +1 −1 +1 −1 +1 Determinants The column under quotient 4 is " # " # " # 1 1 0 = + 4 (first column plus 4 times second column) 4 0 1

The column under quotient 3 is " # " # " # 3 0 1 = + 3 (second column plus 3 times third column) 13 1 4

The column under the last quotients are " # " # " # " # " # " # 1 3 7 3 7 17 + 2 = and + 2 = 4 13 30 13 30 73

The determinate of the last 2 × 2 matrix in the magic table is " # 7 17 det = 1 (that is, 73 · 7 + 17 · (−30) = 1 or 17 · (−30) = 1 mod 26) 30 73

That is, −30 = −30 + 73 = 43 mod 73 is the inverse of 17 mod 73. Therefore, the equation

5 = 17x + 3 mod 73 (has solution x = 17−12 = 43 · 2 = 86 = 13 mod 73) cse 1400/mth 2051 discrete math notes 65

Check: 17 · 13 + 3 = 224 = 73 · 3 + 5 = 5 mod 73. Here is another example: Solve 10x = 5 mod 37. The steps in the Euclidean algorithm are

Dividend = Divisor · Quotient + Remainder a = m · q + r 37 = 10 · 3 + 7 10 = 7 · 1 + 3 7 = 3 · 2 + 1 3 = 1 · 3 + 0

The magic table is 3 1 2 3 Quotients 1 0 1 1 3 10 0 1 3 4 11 37 +1 −1 +1 −1 +1 Determinants The last two columns have determinant

37 · 3 + 10 · (−11) = 111 − 110 = +1

The inverse of 10 mod 37 is −11 = 26 mod 37. And, the solution of

10x = 5 mod 37 (is x = 26 · 5 = 130 = 37 · 3 + 19 = 19 mod 37)

Check: 10 · 19 = 195 = 37 · 5 + 5 = 5 mod 37.

Problems and Questions

1. Solve the equations (a)4 = 3x + 2 mod 7 (b)4 x = 2 mod 7 (c)4 x = 2 mod 17 (d)5 x = 2 mod 17 cse 1400/mth 2051 discrete math notes 66

Mathematical Proof

Starting from self-evident truths (axioms, postulates) derive new truths. There are axioms for geometries (Euclidean and non-Euclidean), arithmetic (Peano), sets (Zermelo–Frankel), and logics (Boolean and Frege–Peirce–Russell).

An axiomatic system must be consistent. Consistency requires that it is not possible to prove a state- ment q and its negation ¬q are both True. At most one of q or ¬q can have a proof. It may be that neither has a proof. Consistency is summarized in the reductio ad absurdum inference rule.

((p ⇒ q) ∧ (p ⇒ ¬q)) ⇒ ¬p (if axioms p imply both q and ¬q, then the axioms p are False)

Inconsistent systems seem to be used consistently in everyday life, but they have no mathematical use. Proving a consistent system is consistent is not always possible.

It’s good for an axiomatic system to be complete too. Completeness requires that every True state- ment have a proof. Completeness is summarized in the inference rule.

(p ⇒ q) ∨ (p ⇒ ¬q) (from axioms p one of q of ¬q can be derived)

There are well known conjectures that appear to be True, but proofs of them have never been discovered. Twin prime conjecture: There are infinitely many prime numbers p such that p + 2 is also prime. Goldbach’s conjecture: Every even integer greater than 2 can be written as the sum of two prime numbers. Collatz’s conjecture: Define the Collatz function by   n if n ≡ 0 (mod 2) c(n) = 2 3n + 1 if n ≡ 1 (mod 2)

Define the Collatz sequence by: c o l l a t z 1 = 1 c o l l a t z n = i f even n hailstone :: Int −> [ Int ] then n ‘ div ‘ 2 h a i l s t o n e = takeWhile (/= 1). i t e r a t e c o l l a t z else 3 * n + 1 Collatz’s conjecture is: For all naturals n > 0, the sequence generated by hailstone n always terminates. Unfortunately, Gödel’s first incompleteness theorem says “Every consistent axiomatic proof system about a sufficiently rich arithmetic is incomplete.”

Proofs are applications of axioms and rules of inference, replacement, and quantification. Rule of inference are statements that are always True (tautologies). Here are a few rules of inference. Truth tables can be constructed to show the following are always True. • Modus Ponens (Bridging method):

p ∧ (p ⇒ q) ⇒ q (q is True if p and p ⇒ q are both True.)

• Modus Tollens (Denying method):

¬q ∧ (p ⇒ q) ⇒ ¬p (if q is False and p ⇒ q is True, the p is False.) cse 1400/mth 2051 discrete math notes 67

• Instantia Contra (Counterexample):

(∃n)(¬p(n)) ⇒ ¬(∀n)(p(n)) (if p(n) is False for at least one n, then p(n) is not always True.)

• Currying: ((p ∧ q) ⇒ r) ⇒ (p ⇒ (q ⇒ r)) A function of two variables can be decomposed into the composition of one variable functions. • Uncurrying: (p ⇒ (q ⇒ r)) ⇒ ((p ∧ q) ⇒ r) The composition of one variable functions can be combined into a function of two variables. Proofs by mathematical induction have been covered in previous notes.

Here are some examples given in the standard theorem–proof style of mathematics. Theorem: If integer n is odd, then n2 is odd too. Proof: If n is odd, then n = 2k + 1 for some integer k (definition of odd) and its square is n2 = (2k + 1)2 = 4k2 + 4k + 1 = 2(2k2 + 2k) + 1 = 2m + 1 is odd too.

Theorem: Divides on the natural numbers is transitive: If a\b and b\c, then a\c. Proof: If a\b, then there exists x ∈ N such that b = ax. If b\c, then there exists y ∈ N such that c = by. Substituting the first equation into the second: c = by = (ax)y = a(xy) = az for z = xy ∈ N. That is a divides c.

Theorem: When modulus m is prime, every modular integer a from 1 to (m − 1) has an multiplicative inverse a−1 such that a · a−1 = 1 mod m. Proof: Apply the pigeonhole principle. Consider the products

a · k (mod m), k = 1, . . . , (m − 1)

If two of these products are equal, that is, if a · i = a · j for some i 6= j, then a(i − j) = 0 (mod m). That is, m = a(i − j), or m is not prime.

Theorem: If a2 is even, then a is even. Proof: By contraposition, if a is odd, then a2 is odd.

Theorem: If n > 0 and 4n − 1 is prime, then n is odd.  2 Proof: By contraposition, if n = 2k > 0 is even, then 4n − 1 = 42k − 1 = 4k − 1 = (4k + 1)(4k − 1) is not prime.

Theorem: If n + m is even, then n and m have the same parity (both even or both odd). Proof: If n = 2k is even and m = 2j + 1 is odd, then n + m = 2(k + j) + 1 is odd. Theorem: If n is a integer such that n mod 4 is 2 or 3, then n is not a perfect square. Proof: This a a proof by cases. If n = m2 is a perfect square, then n equivalent to 0 or 1 mod 4. Every inte- ger m can be written as m = 4q + r where 0 ≤ r < 4. If m = 4q then m2 = 16q2 ≡ 0 mod 4. If m = 4q + 1 then m2 = 16q2 + 8q + 1 ≡ 1 mod 4. If m = 4q + 2 then m2 = 16q2 + 16q + 4 ≡ 0 mod 4. If m = 4q + 3 then m2 = 16q2 + 24q + 9 ≡ 1 mod 4. cse 1400/mth 2051 discrete math notes 68

Mathematical Proofs, Part 2

Proofs by counterexample show a universally quantified statement is not True. To show a statement is not always True, given an example where it is False.

• Give an example to show (a ≡ b mod m) ⇒ (a = b) is False. Know that a ≡ b mod m means a and b have identical remainders when divided by m. Pick a value for m, say m = 3. Pick two different numbers that have identical remainders, say a = 1 and b = 4. Both have the same remainder: 4 ≡ 1 mod 3, but 1 6= 4. • Give an example to show (ab ∈ Q) ⇒ (a, b ∈ Q) is False The contrapositive is√ useful here: Find two numbers that are not rational, but their product is. A simple example is a = b = 2. Neither is rational (see below), but their product is 2, a . • Give an example to show: On the natural numbers divides is symmetric is False. Simply: 1 divides 2, but 2 does not divide 1. By now the meaning of divides does not need to be ex- plained.

Proofs by contradiction (reductio ad absurdum) Proofs by contradiction begin by assuming what is to be proven is False. That is, to prove p is True, assume it is False and derive a contradiction.

Theorem: The set of primes is infinite (unbounded, there is always a larger prime than the one currently known as largest). Proof: Suppose that there are a finite number of primes, say there are n primes named p0 = 2 < p1 = 3 < ... < pn−1, with pn−1 being the largest prime. Let N = p0 p1 ··· pn−1 + 1. Since N > pn−1, the largest prime, N is not prime and must be composite. That is, N is divisible by some prime p. But N divided by any of the primes p0,... pn−1 leaves a remainder of 1. That is N is not divisible by any prime p. That N is divisible by a prime and not divisible by a prime is a contradiction. Therefore, there cannot be a largest prime. √ √ 6∈ Q Theorem: 2 √ , that is 2 cannot√ be represented as the ratio of integers. a Proof: Assume 2 ∈ Q, that is 2 = b where a and b are integers and b 6= 0. Without loss of generality, a (a b) = assume that b has been√ “reduced to lowest terms,” that is gcd , 1. a 2 2 Square both sides of 2 = b and clear the result of fractions to get 2b = a . Therefore a is even. That is, a = 2k for some integer k and a2 = 4k2. This gives 2b2 = 4k2 or b2 = 2k2. That is, b is even. And this contradicts gcd(a, b) = 1.

Theorem: The real numbers in the interval [0, 1] are uncountable. Proof: This is Cantor’s famous diagonalization argument. Diagonalization plays a fundamental role in the development of theoretical computer science. Let T be the set of all infinite sequences of bits. Think of these as numbers in [0, 1], for instance, some sequences in T and their interpretation as real numbers could be:

h0, 0, 0, . . .i 7→ 0.0 h1, 0, 0, . . .i 7→ 0.5 h0, 1, 0, . . .i 7→ 0.25 h1, 1, 0, . . .i 7→ 0.75 cse 1400/mth 2051 discrete math notes 69

Assume that T is countable. That is, there is some one-to-one function f from the natural numbers N onto T. (∃ f : N 7→ T)((onto f ) ∧ (one-to-one f )) If this is the case, then f can be applied to each providing list all infinite binary sequences in ~T. For instance, the function f might map the naturals to the sequences D E 0 7→ 0 , 0, 0, 0, 0, 0, . . . D E 1 7→ 1, 0 , 0, 0, 0, 0, . . .

2 7→ 0, 1, 1 , 0, 0, 0, . . . D E 3 7→ 1, 1, 0, 0 , 0, 0, . . .

4 7→ 1, 1, 1, 0, 1 , 0, . . . 5 7→ 1, 1, 0, 1, 1, 1 ,......

From any such enumeration, construct a sequence of bits using the values along the diagonal. In the exam- ple above bits along the diagonal are: h0, 0, 1, 0, 1, 1, . . .i Next, flip each bit the diagonal sequence.

d~ = h1, 1, 0, 1, 0, 0, . . .i

The sequence d~ cannot be in the enumerated list. ~ (∀k ∈ N)(d 6= ~sk) ~ For each k, the k-th bit in d is the complement of the k-th bit in ~sk: The two sequences cannot be equal. Here is another formulation of Cantor’s diagonalization argument. Theorem: There is no onto function from a set S to its power set 2S. Proof: For a finite set, the statement is somewhat obvious. If |S| = n, then the cardinality of the power set 2S = 2n. Since n < 2n for all n ∈ N, there can be no onto function from the smaller set S to the larger set 2S. Let S be any set and let 2S be the power set of S, the set of all subsets of S. Suppose there is an onto func- onto tion f from S to 2S: f : S 7→ 2S. Let D = {s ∈ S : s 6∈ f (s)}. Since f is onto by assumption, there exists an x ∈ S such that f (x) = D. But, s ∈ D if and only if s 6∈ f (x) = D, a contradiction. The theorem can be stated as:

(∀ f : S 7→ 2S)(∃ D ⊆ S)(∀ s ∈ S)( f (s) 6= D)

When a statement is True, having one or more algorithms that demonstrate the truth of instances of it is useful. But, there are some True statements that have no known algorithm that constructs solutions. Theorem: There exists irrational numbers a, b ∈ R − Q such that ab ∈ Q. cse 1400/mth 2051 discrete math notes 70

√ √ √ 2 √ 2 ∈ Q Proof: If 2 is rational the proof is finished. (No proof that 2 is given.) √ √ √ √ 2 √ 2 √ 2 √ √ 2 √ 2 Otherwise, if 2 is irrational, let a = 2 and b = 2. Then ab = 2 = 2 = 2 is rational. cse 1400/mth 2051 discrete math notes 71

Paradoxes

A paradox a statement that leads, through sound logical reasoning, to contradictory conclusions. Self-reference seems to be crucial in paradoxes. Paradoxes seem to contradict the law of the excluded middle: There seems to be logical statements that are neither True nor False.

The Liar’s Paradox

Some trace the liar’s paradox back to a Cretan philosopher named Epimenides. He lived around 600 BC. Epimenides said “Cretans always lie.” Since Epimenides is a Cretan, his statement must be False. If one Cretan exists who does not always lie can be found, then Epimenides can be proven wrong. Such an exis- tence proof may be difficult to execute. The statement: “This statement is False.” reveals the deeper Liar’s Paradox. • If “This statement is False” is True, then “This statement is False” is, as it says, False. • On the other hand, if “This statement is False” is False, then “This statement is False” must be True.

The Seville Barber Paradox

The barber in Seville shaves all those, and only those, who do not shave themselves. The question is: Who shaves the barber? The statement (∃x)(man(x) ∧ (∀y)(man(y) → (shaves(x, y) ↔ ¬shaves(y, y)))) cannot be satisfied. Consider what it says when y = x.

Curry’s Paradox

I like to pitch Haskell Curry because I like Haskell, the programming language named for him. Consider the sentence: (A) “If this sentence is True, then Earth’s moon is made of green cheese.” Where we’ve called the sentence (A). Agree with me, the conclusion in (A) is False: “Earth’s moon is not made of green cheese.” Consider the cases: • If (A) is True, then the assumption in (A) “this sentence is True” must be False, that is (A) is False. • If (A) is False, then the assumption in (A) “this sentence is True” must be True, that is (A) is True.

Russell’s paradox

Consider the set S of all sets that do not contain themselves. For instance, the set of natural numbers in S because the set of natural numbers is not a natural number: N ∈ S because N 6∈ N. In , S = {X : X 6∈ X} The question is: Is S ∈ S? Consider the cases. cse 1400/mth 2051 discrete math notes 72

• If S ∈ S, then S 6∈ S by definition of S. • If S 6∈ S, then S ∈ S again by the definition of S. Russell’s paradox lead him to introduce types to replace sets. Types are essential in computing. Types have some basic judgment rules: • T is a type.

• T0 and T1 are equal types. • t has type T.

• t0 and t1 are equal values of type T. Functions (which are a type) can be applied to values in a type T to produce values in some type S. A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases to the kinds of values they compute (Pierce, 2002).

Self-Referential Adjectives

This is the Grelling-Nelson Paradox. • An adjective is heterological if it does not describes itself. Instances: Long is not long, monosyllabic is not monosyllabic, yellow is not yellow, . . . • An adjective is homological if it does describes itself. Instances: Polysyllabic is polysyllabic, it has 5 sylla- bles; Simple is simple; unhyphenated is unhyphenated, . . . The excluded middle principle requires that an adjective is either heterological or homological, and it cannot be both. Consider the question: Is heterological heterological? Here’s the problem. • If heterological is heterological, then heterological is homological. Heterological is not heterological. • On the other hand, if heterological is not heterological, then heterological is homological. Heterological is heterological.

References

Pierce, B. C. (2002). Types and Programming Languages. The MIT Press, 1st edition. [page 72] cse 1400/mth 2051 discrete math notes 73

Questions and Problems

Numbers

1. Explain how bits, digits, and hexits can be used to write natural numbers using positional notation. 2. For base 10, with knowledge of logarithms, one can see that a positive natural n can be written using about log n digits. (a) Explain the above statement when 100 ≤ n < 1000 and when 1000 ≤ n < 10000 (b) Compose the logarithm log n and floor bxc functions together with your understanding of the above examples to derive an expression for the number of digits required to write n. (c) Generalize the expression you derived to provide a formula for writing a number n using bits. 3. Choose a few naturals written in decimal and convert them to their binary representations. Do this by repeated division by 2 and collecting remainders. 4. Choose a few 8-bit strings. Assume their type is natural number and convert them to decimal using Horner’s rule. 5. Next assume their type is two’s complement and convert them to integers. 6. Negate your chosen two’s complement bit strings. 7. Choose a few integers, some of them negative, written in decimal notation. Convert them to their two’s complement representation. 8. Write a few fixed-point numbers in binary and convert them to decimal. 9. The notes do not discuss signed fixed-point numbers. How would you represent such numbers?

Logic

1. Hilbert provides a list of axioms for the foundation of mathematics. Here are a few of his axioms. He classifies them into categories. I paraphrase from Hilbert’s paper (Hilber, 1927). Prove that Hilbert’s axioms are valid (always True, tautologies). • Axioms of implication, (a) A ⇒ (B ⇒ A) (introduction of an assumption) (b) (A ⇒ (A ⇒ B)) ⇒ (A ⇒ B) (omission of an assumption) (c) (A ⇒ (B ⇒ C)) ⇒ (B ⇒ (A ⇒ C)) (interchange of assumptions) (d) (B ⇒ C) ⇒ ((A ⇒ B) ⇒ (A ⇒ C)) (elimination of a proposition). • Axioms of ∧ and ∨ (Hilbert used & for ∧) (a) (A ∧ B) ⇒ A (b) A ⇒ (B ⇒ (A ∧ B)) (c) A ⇒ (A ∨ B) (d) ((A ⇒ C) ∧ (B ⇒ C)) ⇒ ((A ∨ B) ⇒ C) • Axioms of negation (Hilbert used ∼ for ¬) (a) (A ⇒ (B ∧ ¬B)) ⇒ ¬A (principle of contradiction); (b) ¬(¬A) ⇒ A (principle of double negation). cse 1400/mth 2051 discrete math notes 74

2. Give the converse, the contrapositive, and the inverse of the statement “If it rains today, then I will bike to work.” 3. Consider the finite set of binary strings.

S ={(000000), (100000), (110000), (111000), (111100), (111110), (111111), (011111), (001111), (000111), (000011), (000001)}

Explain how it is possible to represent such a set as a propositional formula. And, find the most com- pact representation. 4. Consider the statement (∀ m)(∃ n)(P(m, n)) ⇒ (∃ n)(∀ m)(P(m, n)) where both m and n are integers. Determine the truth value of the proposition if (a) P(m, n) is the statement “m < n”. (b) P(m, n) is the statement “m\n”(m divides n) 5. Suppose that the variable x represents students, F(x) means “x is a freshman,” and M(x) means “x is a math major”. Consider each of following symbolic statements in predicate logic concerning M(x) and F(x).

(a) (∀ x)(M(x) ⇒ ¬F(x)) (f) ¬(∀ x)(¬M(x) ∨ ¬F(x)) (b) ¬(∃ x)(M(x) ∧ ¬F(x)) (g) (∀ x)(¬M(x) ∧ ¬F(x)) (c) (∀ x)(F(x) ⇒ ¬M(x)) (h) (∀ x)(¬M(x) ∨ ¬F(x)) (d) (∀ x)(M(x) ⇒ F(x)) (i) ¬(∃ x)(F(x) ∧ M(x)) (e) (∃ x)(F(x) ∧ ¬M(x)) (j) ¬(∀ x)(F(x) ⇒ ¬M(x))

Determine which of the symbolic statements (a)–(j) above describe each of the three statements. (a) Some freshmen are math majors. (b) Every math major is a freshman. (c) No math major is a freshman. Indicate all the equivalent symbolic statements (there many be more than one).

Sequences

Induction

1. Use mathematical induction to prove i  n ∑ = 0≤i

 i  Make a hypothesis about an expression for the sum ∑ and use mathematical induction to ver- 0≤i

(x + 1)(x − 1) = x2 − 1 (x2 + x + 1)(x − 1) = x3 − 1 (x3 + x2 + x + 1)(x − 1) = x4 − 1

Make a hypothesis about an expression for the sum ∑ xi and use mathematical induction to verify it. 0≤i

FV = p ∑ (1 + r)k 0≤k

Use your knowledge of geometric sums to find a simple formula for FV.

Recursion

1. Let s : N 7→ N+ be the successor function s(n) = n + 1. Define a function a : N2 7→ N by the rules • a(n, 0) = n + 0 = n (initial conditions) • a(n, m + 1) = s(a(n, m)) (recurrence equation) What does a(n, m) compute? 2. Show the function solves the recurrence. (a) f (n) = 2n − 1 solves f (0) = 0 and f (n) = 2 f (n − 1) + 1 for n > 0 (b) f (n) = n(n − 1)/2 solves f (0) = 0 and f (n) = f (n − 1) + (n − 1) for n > 0 (c) f (n) = lg n solves f (1) = 0 and f (n) = f (n/2) + 1 for n = 2k (d) f (n) = n lg n solves f (1) = 0 and f (n) = f (n/2) + n for n = 2k

Functions

x + + 1. Let b : R 7→ R be the base-b exponential function. Let logb x : R 7→ R be the base-b logarithm function. What are the values of the following expressions?

(a) blogb x √ n m (b) logb x n n n 2. Let b : N 7→ Zm be the base-b modular exponential function, that is bm = b mod m. Let logb,m x : 3 Zm 7→ N be the base-b modular logarithm function. For instance, 5 = 125 = (13 · 9 + 8) = 8 mod 13

and log5,13 8 = 3. The modular exponential is not too difficult compute. What are the values of the following expressions? cse 1400/mth 2051 discrete math notes 76

(a)2 8 mod 5 (c)3 4 mod 13 (b)2 8 mod 6 (d)3 4 mod 14

Computing the modular logarithm appears to be difficult. What are the values of the following expres- sions? k (a) log2,5 3, call it k where 2 = 3 mod 5. 3. Consider the statement: “Function f maps set X to Y,” in notation f : X 7→ Y. (a) Give a precise definition of the statement using English. (b) Give a precise definition of the statement using logic notation.

Pigeonhole Principle

1. Pretend a function maps n values to m values, for instance,

f : Zn 7→ Zm

 n  (a) Why must at least one of the m values be the image of m or fewer of the n values? Hint: Think n = 13 pigeons mapped to m = 5 pigeonholes.  n  (b) Why must at least one of the m values be the image of m or more of the n values? (c) Do the statements remain True when n = 5 and m = 13?

Permutations

1. An anagram is a permutation of a string. For instance, listen 7→ silent is an anagram. An anagram need not be an English word.

(a) Write the permutation listen 7→ silent in matrix and cycle notation. Use the Z6 = {0, 1, 2, 3, 4, 5} to index the letters {l, i, s, t, e, n}. (b) How many anagrams of “listen” are there? (c) How many anagrams are there of a 7 letter string when no letters are the same, for instance: “abcdefg” (d) How many anagrams are there of “mississippi”? How do multiple letters reduce the number of ana- grams? 2. Pretend you had a six charms with the letters “listen” on them. In how many ways can the charms be placed on a bracelet?

Relations

1. Let a and b be positive naturals. Show that a is relatively prime to b is (a) Not reflexive. (b) Symmetric. (c) Not antisymmetric. (d) Not transitive.

2. Consider the parallel relation on lines: `1 k `2. Is parallel reflexive? symmetric? antisymmetric? transi- tive? cse 1400/mth 2051 discrete math notes 77

3. A relation ∼ on X is asymmetric if

(∀ a, b ∈ X)((a ∼ b) ⇒ ¬(b ∼ a))

(a) Give examples of asymmetric relations (b) Can ∼ be asymmetric and reflexive?

Equivalences

Orders

Combinatorics

1. Find an expression for the number of m-elements subsets of an n-element set. Evaluate this expression for a few naturals 0 ≤ m ≤ n. How is this related to the value in row n, column m of Pascal’s triangle? 2. Find an expression for the number of subsets of an n-element set. How is this related to row n of Pas- cal’s triangle? 3. Let n = |X| and m = |Y| (a) How many relations X ∼ Y can be defined? (b) How does your answer to 3a relate to using adjacency matrices to represent relations? (c) How does your answer to 3a relate to using subsets of X × Y to describe a relation? 4. Let n = |X| and m = |Y| (a) How many functions f : X 7→ Y can be defined? (b) How does your answer to 4a relate to using adjacency matrices to represent relations? (c) How does your answer to 4a relate to using subsets of X × Y to describe a function?

Number Theory

1. Pick a few pairs of naturals and use the Euclidean algorithm to compute their greatest common divisors. 2. Pick a few pairs of relatively prime integers (a, m) and show how to use the extended Euclidean algo- rithm to solve the linear congruence equation

ax = b mod m

References

Hilber, D. (1927). The foundation of mathematics. In From Frege to Gödel: A Source Book in Mathematical Logic, 1879-1931.[page 73] cse 1400/mth 2051 discrete math notes 78

References

Hilber, D. (1927). The foundation of mathematics. In From Frege to Gödel: A Source Book in Mathematical Logic, 1879-1931. Kelly-Bootle, S. (1981). The Devil’s DP Dictionary. McGraw-Hill paperbacks. McGraw-Hill. Pierce, B. C. (2002). Types and Programming Languages. The MIT Press, 1st edition.