This preview shows pages 1–6. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Section 3. 6 Integers and Algorithms 95 SECTION 3.6 Integers and Algorithms 11. In addition to calculation exercises on the Euclidean algorithm, the base conversion algorithm, and algorithms
for the basic arithmetic operations, this exercise set introduces other forms of representing integers. These
are balanced ternary expansion, Cantor expansion, binary coded decimal (or BCD) representation,
and one’s and two’s complement representations. Each has practical and/or theoretical importance in
mathematics or computer science. Ifall else fails. one can carry out an algorithm by “playing computer” and
mechanically following the pseudocode step by step. . We divide repeatedly by 2, noting the remainders. The remainders are then arranged from right to left to obtain the binary representation of the given nnmber. a) We begin by dividing 231 by 2, obtaining a quotient of 115 and a remainder of 1. Therefore no 2 1. Next
115/2 2 57, remainder 1. Therefore (11 2 Then
28/2 2 14, remainder 0, so (13 2 0. Similarly 0.4 2 0, after we divide 14 by 2, obtaining 7 with remainder 0. 1. Similarly 57/2 2 28. remainder 1. Therefore 0.2 2 1. Three more divisions yield quotients of 3, 1. and 0. with remainders of '1, 1, and 1, respectively, so (15 2 (15 2
a7 2 1. Putting all this together, we see that the binary representation is (a7n505a.4u3a.201a0)2 2 (1110 0111)2.
As a check we can compute that 20 + 12'1 + 22 + 25 + 26 + 27 2 231. b) Following the same procedure as in part (a), we obtain successive remainders 0, 0, 1, 0, l, 1, 0, 1. l,
0, 0, 0.. 1. Therefore 4532 2 (1 0001 1011 0100)2. c) By the same method we obtain 97644 2 (1 0111 1101 0110 1.100)2. . a) (1 1'111)2 2 ‘2‘1 + 23 + 22 + 21+ 20 216 + 8 + 4 + 2 +12 31. An easier way to get. the answer is to note that (11111}2 = (10 0000b 41: 25 21:31. b) (10 0000 0001).; = 29 + 20 = 513 c) (10101 0101)2 :28+25+24+22+20= 256+64+16+4+12341 d) (1101001 00010000); 2 2” + 213 + 211+ 28 + 24 =16384 + 8192 + 2048 + 250 +16 = 26896 . Following Example 6, we simply write the binary equivalents of each digit: (Aha 2 (1010», (B)15 2 (1011)2, (Chg 2 (1100);, (13)“; = (1101);, (E)16 2 (1110);), and (F)16 2 (1111)2. Note that the blocking by groups
of four binary digits is just for readability by humans. a) (80E)16 = (1000 0000 1110)2 b) (135AB)15 = (0001 00110101 1010101112 c) (ABBA)15 =(10101011 1011 1010)2 d) (DEFACED)16 = (1101 11101111 1010110011101101)2 Following Example 6. we simply write the binary equivalents of each digit. Since (Ah; 2 (1010b, (BM; 2
(1011);), (0)1.3 : (1100», (mm = (1101):, (Em = (1110)2, and (1?)lﬁ : {1111)2, we see that (ABCDEF)16
2 (10101011110011011110111112. Following the convention shown in Exercise 3 of grouping binary digits by
fours, we can write this in a more readable form as 1010 1011 1100 1101 1110 1111. Following Example 6, we simply write the hexadecimal equivalents of each group of four binary digits. Thus
we have (10110111 1011)2 2 (B7B)15. We adopt a notation that will help with the explanation. Adding up to three leading 0’s if necessary, write
the binary expansion as . .[32302252]bgoblgblgbll010b03bggbolb00)2. The value of this numeral is boo + 21101 +
4002 + 8503 + 24510 + 25511 + 26012 + 27013 + 28020 + 29521 + 210522 + 2111523 +   H, which we can rewrite as
500 + 21101 + 41302 + 8503 +0710 + 2511+ 41302 + 8513194 + (020 + 2521+ 4522 + 8523128 +' '  NOW (biﬁbinilbi0)2
translates into the hexadecimal digit hi. So our number is l10+h1 “244—11228 +  2 hglhl 16+h;;162 +' > ,
which is the hexadecimal expansion . . h1hlh0)1ﬁ. 96 13. 15. 17. 19. 21. 23. Chapter 3 The F ni'idan'ientais: Algorithms, the Integers, and iliatrices
This is exactly the same as what we can do with hexadecimal expansion, replacing groups of four with groups
of three. Specifically. group together blocks of three binary digits, adding up to two initial 0’s if necr—rssai'y,
and translate each bloek of three binary digits into a single oetal digit. For example, (011 999 11(l)~2 2 (306)5 In each case we follow the method of Example 6, blocking by threes instead of fours. We replace each octal
digit of the given numeral by its 3digit binary equivalent and string the digits together. The ﬁrst digit
is (7)3 2 (111)2, the next is (3)5 2 (011)2, and so on, so we obtain (111911100101011919091)2. For the
other direction, we split the given binary numeral into blocks of three. digits, adding initial (T's to ﬁll it out:
001 010 111 011. Then we replace each block by its oetal equivalent, obtaining the answer (1273b. Since we have procedures for converting both octal and hexadecimal to and from binary (Example 6 and
Exercises 13—15), to convert from octal to hexadecimal, we ﬁrst convert from octal to binary and then convert from binary to hexadecimal. In effect, this algorithm computes 7 mod 645, 72 mod 84.5. 7'" mod G45. 7'8 mod 645, 7”" mod 645. ., and then multiplies (modulo 645) the required values. Since 644 2 (1010000100)2, we need to multiply
together 7" mod 645, 7‘28 mod G45. and 7"“12 mod 64:3, reducing modulo 645 at each step. We compute by
repeatedly squaring: 72 mod 645 2 49, 74 mod 645 2 49? mod 645 2 2401 mod 645 2 466, 78 mod 645 2
4662 mod 6:15 2 217156 mod 645 2 436, 71“ mod 645 2 4:362 mod 646 2 190096 mod 64.5 2 466. At this
point we see a pattern with period ‘2. so we have 732 mod 64‘ 2 3136, 70'1 mod 61:15 2 466, 7”3 mod 0'45 2
436, 7255 mod 645 2 466, and 7512 mod 645 2 436. Thus our final answer will be the product of 466. 436,
and 436, reduced modulo 645. we compute these one at a time: 466  436 mod 645 2 203176 mod 645 2 1,
and 1  436 Inod 611.5 2 436. So 7‘3” mod G45 2 436. A computer algebra system will verify,’ this: use the
command “7 a" 644 mod 645;” in ilieple. for example. The ampersand here tells Maple to Lise modular
exponentiation. rather than ﬁrst computing the integer 75“, which has over 500 digits, although it could
certainly handle this if asked. The point is that modular exponentiation is much faster and avoids having to deal with such large numbers. in eii'ect, this algorithm computes 3 mod 99, 3'2 mod 99, 3" mod 99, 35 mod 99, 31“ mod 99, and then multiplies (module 99) the required values. Since 2003 2 (1111101t)l)11)2, we need to multiply
together 3 mod 99. 32 mod 99. 3‘U mod 99. 35“ mod 99, 3125 mod 99. 3256 mod 99, 3512 mod 99,
and 3102“ mod 99, reducing module 99 at each step. We compute by repeatedly squaring: 32 mod 99 2 9,
3'1 mod 99 2 81, 38 mod 99 2 81'2 mod 99 2 (3561 mod 99 2 27, 3”5 mod 99 2 272 mod 99 2
729 mod 99 2 36, 332 mod 99 2 362 mod 99 2 1296' mod 99 2 9, and then the pattern repeats, so
3‘“ mod 99 2 81, 3‘28 mod 9.9 2 27, 3230' mod 99 2 :36, 351'2 mod 99 2 a, and 310'“ mod .09 2 81. Thus
our ﬁnal answer will be the product of 3, 9, 36, 81, ‘27, 36, 9, and 81. we compute these one at a time
modulo 99: 39 is 27, 2736 is 81, 818] is '27, 2727 is 36, 36  36 is 9, 9 i 9 is 81, and ﬁnally 81 ~81 is
27. So 32003 mod .99 2 27. a) By Lemma 1, gcd(12, 18) the same as the god of the smaller ofthese two numbers ( 12) and the remainder
when the larger (.18) is divided by the smaller. In this case the I'ClTlEllllClOl‘ is 6, so gcd(1‘2, 18) 2 gcd(12.6).
Now gcd(12. (i) is the same as the gcd of the smaller 01' these two numbers and the ren'iainder when the
larger (1‘2) is divided by the smaller, namelyr (1. This gives gcd(12. (i) 2 gcd(6,0). But gcd(:r:,(]) 2.1.: for all
positive integers, so ged(6,0) 2 Iii. Thus the answer is 6. In brief (the form we will use for the remaining
parts). ged(12,18) 2 ged(1‘2,6) 2 gcd(6,0) 2 (i. b) gcd(111.201) 2 gcd(111,90) 2 gecl(90, ‘21) 2 ged(21,6) 2 gcd((j,3) 2 gcd(3.0) 2 3 c) ged(1001,1331) 2 gcd(10{)l,,339) 2 gcd(330,11) 2 gcd(11,(})211 d) gcd(1‘2345,54321) 2 gcd(1‘23415, 4941) 2 gcd(4941, 2463) 2 ged(2463,15) 2 gcd(15, 3) 2 ged(3,0) 2 3 Section 3. 6 25. 27. 29. 31. 33. 37. Integers and Algorithms 97 e) gcd(1000, 50/10) : ged(1000, 40) : gun/10,0) = 40 f) gcr,l(9888,0060) : gcd(6060,3828} : gcd(3828,2232) : gcd(2232,1596) = ged(1596,636) = gcd(636,324)
= gctl(324, 312) : gcd(31'2,12) : gcd(12, 0) : 1‘2 111 carrying out the Euclidean algoritln‘n on this data, we divide successively by 34, 21, 13, 8, 5, 3, 2, and 1, so eight divisions are required. The binary expansion of an integer represents the integer as a sum of distinct. powers of 2. For example,
since 21 = (1 010112, we have ‘21 : 2“1 + 22 + 20. Since binary expansions are unique, each integer can be so represented uniquely. Let the deeima1 expansion of the integer n be given by n. : (enclamg . . claw”). Thus a = 10”_1r1,,_1 +
10’7'2n,,_2 + + 100. + no. Since 10 E 1 (mod 3), we have a E n,,_1 + (inc2 + + (H + (10 (mod
Therefore a E 0 (mod 3) if and only if the sum of the digits is congruent to 0 (mod 3). Since being divisible
by .‘3 is the same as being congruent to 0 (mod we have proved that a positive integer is divisible by 3 if
and only if the sum of its decimal digits is divisible by 3. Thus a = (1.0 +
1 (mod 3) when k is even, and Let the binary expansion of the positive integer n be given by a. : (an_1n,,_2 . . 111120)?
20.1 + 22(12 + + 2””(1ncl. Since 22 E 1 (mod 3), we see that 2" E
2iC E ‘2 E —1 (mod 3) when k is odd. Therefore we have a E (10 — n] + 0.2 H a3 +   :: e,,_1 (mod 3). Thus n E 0 (mod if and only if the sum of the binary digits in the evennnmbtéred positions minus the sum 01'
the binary digits in the oddnumbered pesitions is congruent to 0 modulo :3. Since being divisible by 3 is the
same as being congruent to 0 (mod our proof is complete. a) Since the leading bit is a 1, this represents a negative number. The binary expansion of the absolute value
of this number is the complement of the rest of the expansion, namely the complement 01' 1001, or 0110.
Since (0110); I 6, the answer is —0. b) Since the leading bit is a 0, this represents a positive number, namely the number whose binary expansion
is the rest of this string, 1101. Since (1101}3 = 13, the answer is 1.3. e) The answer is the negative of the complement of 0001, namely ~(1110); : 714. d) —(0000)2 : 0', note that 0 has two different representations, 0000 and 1111 . We must assume that the sum actually represents a number in the appropriate range. Assume that n. bits 1 and 2'”’1 can be represented. The answer is almost, are being used, so that numbers strictly between —2”_
but not quite, that to obtain the ones complement representation of the sum of two numbers. we simply add
the two strings representing these numbers using Algorithm 3. Instead, after performing this operation, there
may be a. carry out of the leftmost column; in such a case, we then add 1 more to the answer. For example,
suppose that ’{t 2 4; then rnn'nbers from —7 to 7 can be represented. To add ~5 and 3. we add 1010 and
00.11, obtaining 1101; there was no carry out of the left—most column. Since 1101 is the one’s complement
0n the other hand, to add —4 and —3. we add 1011 and 1100, obtaining 1 0111. The 1 that. was carried out of the leftmost column is instead added to 0111, representation of ~2, we have the correct answer. yielding 1000, which is the ones complement. representation of E7. A proof that this method works entails
considering the various cases determined by the signs and magnitudes of the nddends. If m. is positive. (or 0), then the leading bit (ensl) is 0, so the formula reads simply in. : (n2? , which
is clearly correct, since this is the binary expansion of m. (See Section 2.4 for the meaning of sunnnation
notation. This symbolism is a shorthand way of writing 00 + 201 + 4.11.2 +  '  + 2’1’2(1n2 Now suppose that
111, is negative. The onc‘s con'iplen'ient expansion for m, has its leading bit equal to 1. By the definition of one’s 98 39. 41. 43. 45. 47. Chapter 3 The Fundamentals: Algorithms, the Integers, and ﬂ/latrices complement, we can think of obtaining the remaining 11 — 1 bits by subtracting 7m, written in binary, from
111 . . .1 (with n e 1 1’5), since subtracting a bit from 1 is the same thing as complementing it. Equivalently,
if we view the bit string (a.,,_2an_1...ag) as a binary number, then it represents (2’“1 — 1) — (—m). in ~2
22:0 symbols, this says that (2“‘1 — l) — (—m) 0.12". Solving for m gives us the equation we are trying to prove (since a,,_1 = Following the definition, if the ﬁrst bit is a 0, then we just evaluate the binary expansion. If the first bit is a 1,
then we ﬁnd what number at is represented by the remaining four bits in binary; the answer is then ~(2‘1 —:1:).
3) Since the ﬁrst bit is a 1, and the remaining bits represent the number 9, this string represents the number
—(24 — 9) = —7. b) Since the first bit is a 0 and this is just the binary expansion of 13, the answer is 13. c) Since the first bit is a 1, and the remaining bits represent the number 1, this string represents the number
—(24 ~ 1) : 415. (1) Since the first bit is a 1, and the remaining bits represent the number 15, this string represents the number
—(24  15) = —1. Note that 10000 would represent ~(2‘l — 0) : negative number than positive number with this notation. —16, so in fact we can represent one extra The nice thing about two’s complement arithmetic is that we can just work as if it were all in base 2, since
—:r (where :r is positive) is represented by 2” — as; in other words, modulo 2”, negative numbers represent
themselves. However, if overflow occurs, then we must recognize an error. Let us look at some examples,
where n. = 5 (i.e., we use five bits to represent numbers between —15 and 15). To add 5 + 7', we write
00101 + 00111 = 01100 in base 2, which gives us the correct answer, 12. However, if we try to add 13 + 7
we obtain 01101 + 00111 : 10100, which represents —12, rather than 20, so we report an overflow error. (Of
course these two numbers are congruent module 32.) Similarly, for 5 + (—7), we write 00101 + 11001 : 11110
in base 2, and 11110 is the two’s complement representation of —'2, the right answer. For (~5) + (e7), we
write 11011 + 11001 2 110100 in base 2; if we ignore the extra 1 in the left—most column (which doesn’t exist),
then this is the two’s complement representation of —12, again the right answer. To summarize, to obtain
the two’s complement representation of the sum of two integers given in two’s complement representation,
add them as if they were binary integers, and ignore any carry out of the leftmost column. However, if the
left—most digits of the two addends agree and the left—most digit of the answer is dill'erent from their common
value, then an overﬂow has occurred, and the answer is not valid. If m is positive (or 0), then the leading bit (anal) is 0, so the formula reads simply m. : 2:02 (2,921, which
is clearly correct, since this is the binary expansion of m. (See Section 2.4 for the meaning of summation
notation. This symbolism is a shorthand way of writing on + 20:; + 402 +    + 2"“20n_2 Now suppose that.
m. is negative. The two’s complement expansion for m has its leading bit equal to 1. By the definition of
two’s complement, the remaining n a 1 bits are the binary expansion of 271‘] r (—m.). In symbols, this says that 2’“1 — (—m) = 2;]? (12'2". Solving for m. gives us the equation we are trying to prove (since anal = 1
Clearly we need 471 digits, four for each digit of the decimal representation. To find the Cantor expansion, we will work from left to right. Thus the first step will be to find the largest
number 71 whose factorial is still less than or equal to the given positive integer 3;. Then we determine the
digits in the expansion. starting with an and ending with a}. procedure Cuntor(;r : positive integer) n. z: 1; factorial z: 1 while (11 + l) ‘ factorial 5 a; begin Section 3.6 Integers and Algorithms 99 n :2 TL + l factorial :: factorial ~71
end {at this point we know that there are n digits in the expansion}
1} z: .1: {this is just so we do not destroy the original input}
while n > 0 begin
on :: ly/foctorioll
y 2: y — an fact0rial
factorial :: factorial/n
n :2 n e 1 end {we are done: .7: = a,,_n! + a,,,1(n ﬂ 1)! +    + (1221+ all!} 49. Note that n = 5. Initially the carry is c : 0, and we start the for loop with j : 0. Since 00 : 1 and b0 2 0, 51. we set (1 to be [(1 + 0 + 0)/2j : 0', then so : I + 0 + 0 i 2 0, which equals 1, and ﬁnally c = 0. At the end
of the first pass, then, the rightmost digit of the answer has been determined (it‘s a 1), and there is a carry of 0 into the next column. Now 3' : l, and we compute d to be [(01 +151 + c)/2J : [(1 +1 +0)/2J : 1; whereupon .31 becomes
1 + 1 + 0 e 2 1 = 0, and c is set to 1. Thus far we have determined that the. last two bits of the answer are
01 (from left to right), and there is a carry of 1 into the next column. The next three passes through the 100p are similar. As a result. of the pass when 3' = 2 we set (I = 1,
52 2 0, and then c : 1. When j : 3, we obtain d = 1, .93 : 0, and then c: = 1. Finally, when j = 4, we
obtain d. = l, 3,; = 1, and then c z 1. At this point the loop is terminated, and when we execute the ﬁnal
step, 3;, : 1. Thus the answer is 11 0001. We will assume that the answer is not negative, since otherwise we would need something like the one’s
complement representation. The algorithm is similar to the algorithm for addition, except that we need to
borrow instead of carry. Rather than trying to incorporate the two cases (borrow or no borrow) into one, as
was done in the algorithm for addition, we will use an if. . . then statement to treat the cases separately. The
notation is the usual one: o, = (0.n_1...(11a0)2 and b: (bu—1 ...b1b0)g procedure subtmct(o, b : nonnegative integers) borrow :2 0 for :20 to n—l if 0.3 — borrow 2 bi then begin
53' z: (13 w borrow ; I),
borrow :: 0 end else begin
5, := (:3 + 2 e borrow — b,
barr‘ow :: I end {assuming a 2 b, we have 0 —b : (sn_1.s,,_,2 . . . sisoh} 53. To determine which of two integers (we assume they are normegative), given in binary as a : (a,,__1 . . .olon)2 and b = (bwl . . .blbg)2, is larger, we need to compare digits from the most signiﬁcant end (i = n i 1) to the
least (i : 0), stopping if and when we find a difference. For variety here we record the answer as a character
string; in most applications it would probably be better to set compare to one of three code values (such as
—1, 1, and 0) to indicate which of the three possibilities held. 100 Chapter 3 The andamentals: Algorithms, the Integers, and Ma trices procedure compcrda, b : nonnegative integers) i :: n. e 1
while i > 0 and (21 : bi i :2 2'. ﬂ 1
if (1., > b? then answer :2 “a > b"
else if at < bi then answer :: "a. < b”
else answer 1: “a. = b” { the answer is recorded in answer] 55. There is one division for each pass through the while loop. Also: each pass generates one digit in the base b
expansion. Thus the number of divisions equals the number of digits in the base 1) expansion of n. This is
just [10gb n] + 1 (for example, numbers from 10 to 99, inclusive, have common logarithms in the interval
[1,2)). Therefore exactl}r Llogb nj + 1 divisions are required, and this is O(logn). (We are counting only the
actual division operation in the statement q :2 Lq/bj. If we also count the implied division in the statement
at. :2 q mod b, then there are twice as many as we computed here. The big—0 estimate is the same, of course.) 57. The only timeconsuming part of the algorithm is the while loop, which is iterated q times. The work done
inside is a subtraction of integers no bigger than a, which has ioga bits. The results now follows from Example 8. ...
View
Full
Document
This note was uploaded on 02/18/2012 for the course MATH 640:244 taught by Professor Ming during the Spring '09 term at Rutgers.
 Spring '09
 Ming

Click to edit the document details