CSE 1400/MTH 2051 Discrete Math Notes William David Shoaff August 30, 2017
Total Page:16
File Type:pdf, Size:1020Kb
CSE 1400/MTH 2051 Discrete Math Notes William David Shoaff August 30, 2017 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: Tuesdays & Thursday, 5:30 p.m. to 7:30 p.m. in Olin Engineering 137 • 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 cse 1400/mth 2051 discrete math notes 2 Numbers Numbers are very useful. Numbers can be written in many different languages. The decimal language, base 10, is the lingua franca for common conversations. The decimal alphabet is D = f0, 1, 2, 3, 4, 5, 6, 7, 8, 9g 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 computing 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 = f0, 1g called bits 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 Fractions (rational numbers) 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 hexadecimal digits (hexits). The hexadecimal language, base 16 uses the alphabet H = f0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, Fg 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 4 = lg 16 times shorter than numbers written in binary and about 1.2 = log10 16 times shorter than numbers written in decimal. 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 = count of numerals required to write n in base b. cse 1400/mth 2051 discrete math notes 3 An example is a start at understanding: log10 314 + 1 = b≈ 2.4 ···c + 1 = 3 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 sexagesimal 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 degree. The number 60 = 223151 is highly composite: 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60 all divide 60. Properties of prime and composite numbers are studied in number theory. A topic with inter- esting uses. Given the prime factorization of n, being able to count n’s divisors is a useful skill too. I find it interesting that the Romans are known for their engineering, but, their number system The Roman numeral alphabet is fI, V, X, L, C, D, Mg. Arcane rules define the value interpretation (semantics) from the format (syntax) of a number written using Roman numerals. 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. Questions and Problems 1. What is the alphabet for base 3, ternary numbers? 2. What is the alphabet for base 8, octal 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 divisor 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 is a difficult problem. It is being solved incrementally. Translations between number systems is algorithmic. There is one basic efficient algorithm, 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 algorithms and efficiencies in special cases. Horner’s rule Horner’s rule converts numbers written in base b into numbers written in base c, using base c arithmetic. 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 binary number (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 n Horner’s schema can be expressed by the code below. The code evaluates the polynomial p(x) = an x + n−1 an−1 x + ··· + a1 x + a0 at the value of the base 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 ] 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. cse 1400/mth 2051 discrete math notes 5 Inverting Horner’s rule Horner’s rule uses multiplication; 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.