16 1 / The Foundations: Logic and Proofs 1-16

Solution: The bitwise OR, bitwise AND, and bitwise XOR ofthese strings are obtained by taking the OR, AND, and XOR of the corresponding bits, respectively. This gives us 01 1011 0110 11 0001 1101 11 1011 1111 bitwise OR 01 0001 0100 bitwise AND 10 1010 1011 bitwise XOR

Exercises

1. Which ofthese sentences are propositions? What are the b) There is no pollution in New Jersey. truth values of those that are propositions? c) 2 + 1 = 3. a) Boston is the capital of Massachusetts. d) The summerin Maine is hot and sunny. b) Miami is the capital of Florida. 4. Let p and q be the propositions c) p : I bought a lottery ticket this week. 2 + 3 = 5. d) 5 + 7 = 10. Answer this question. q : I won the million dollar jackpot on Friday. e) x + 2 = II. 1) 2. Which ofthese are propositions? What are the truth values Express each ofthese propositions as an English sentence. of those that are propositions? a) --'p b) p v q c) p --+ q

d) p q e) p - q 1) --'p --+ --.q a) Do not pass go. /\ g) --'p --.q --'p (p q) b) What time is it? /\ b) v /\ 5. Let p and be the propositions "Swimming at the New c) There are no black flies in Maine. q Jersey shore is allowed" and "Sharks have been spotted d) 4 +x 5. = near the shore," respectively. Express each of these com­ The moon is made of green cheese. e) pound propositions as an English sentence. 1) 2n ::: 100. a) --.q b) p /\ q c) --'p v q 3. What is the negation of each ofthese propositions? d) p --+ --.q e) --.q --+ p 1) --'p --+ --.q p --.q --'p (p --.q) a) Today is Thursday. g) _ b) /\ V

unkS� JOHN WILDER TUKEY (1915-2000) Tukey, born in New Bedford, Massachusetts, was an only child. His parents, both teachers, decided home schooling would best develop his potential. His fo rmal education began at Brown University, where he studied mathematics and chemistry. He received a master's degree in chemistry from Brown and continued his studies at Princeton University, changing his field of study from chemistry to mathematics. He received his Ph.D. from Princeton in 1939 for work in topology, when he was appointed an instructor in mathematics at Princeton. With the start of World War II, he joined the Fire Control Research Office, where he began working in statistics. Tukey found statistical research to his liking and impressed several leading statisticians with his skills. In 1945, at the conclusion ofthe war, Tukey returnedto the mathematics department at Princeton as a professor of statistics, and he also took a position at AT&T Bell Laboratories. Tukey founded the Statistics Department at Princeton in 1966 and was its first chairman. Tukey made significant contributions to many areas of statistics, including the analysis ofvariance, the estimation of spectra of time series, inferences about the values of a set of parameters from a single experiment, and the philosophy of statistics. However, he is best known for his invention, with 1. W Cooley, of the fast Fourier transform. In addition to his contributions to statistics, Tukey was noted as a skilled wordsmith; he is credited with coining the terms bit and software. Tukey contributed his insight and expertise by serving on the President's Science Advisory Committee. He chaired several important committees dealing with the environment, education, and chemicals and health. He also served on committees working on nuclear disarmament. Tukey received many awards, including the National Medal of Science.

HISTORICAL NOTE There were several other suggested words for a binary digit, including binit and bigit, that never were widely accepted. The adoption of the word bit may be due to its meaning as a common English word. For an account of Tukey's coining of the word bit, see the April 1984 issue of Annals of the History of Computing. 1-1 7 1.1 Propositional Logic 17

6. Let p and q be the propositions "The election is de­ Write these propositions using p, q, and r and logical cided" and "The votes have been counted," respectively. connectives. Express each of these compound propositions as an a) Yo u get an A in this class, but you do not do every English sentence. exercise in this book. a) ""'p b) p vq b) You get an A on the final,you do every exercise in this c) ""'p/\ q d) q --+ P book, and you get an A in this class. e) ""'q --+ ""'p I) ""'p --+ ""'q c) To get an A in this class, it is necessary for you to get an A on the final. g) p ++ q b) ....,qv(....,P /\ q) d) You get an A on the final, but you don't do every ex­ Let p and q be the propositions 7. ercise in this book; nevertheless, you get an A in this p : It is below freezing. class. q : It is snowing. e) Getting an A on the final and doingevery exercise in Write these propositions using p and q and logical this book is sufficientfor getting an A in this class. connectives. I) Yo u will get an A in this class if and only if you either do every exercise in this book or you get an A on the a) It is below freezing and snowing. final. b) It is below freezingbut not snowing. 11. Let p, q, and r be the propositions c) It is not below freezingand it is not snowing. d) It is either snowing or below freezing (or both). p : Grizzly bears have been seen in the area. e) If it is below freezing, it is also snowing. q : Hiking is safe on the trail. I) It is either below freezingor it is snowing, but it is not r : Berries are ripe along the trail. snowing if it is below freezing. Write these propositions using p, q, and r and logical g) That it is below freezing is necessary and sufficient connectives. for it to be snowing. a) Berries are ripe along the trail, but grizzly bears have 8. Let p, q, and r be the propositions not been seen in the area. p : Yo u have the flu. b) Grizzly bears have not been seen in the area and hiking q : Yo u miss the final examination. on the trail is safe, but berries are ripe along the trail. c If berries are ripe along the trail, hiking is safe if and r : Yo u pass the course. ) only if grizzly bears have not been seen in the area. Express each ofthese propositions as an English sentence. d) It is not safe to hike on the trail, but grizzly bears have a) p --+ q b) ""'q ++ r not been seen in the area and the berries along the trail c) q --+ ....,r d) p v q V r are ripe. e) (p --+ ....,r) v (q --+ ....,r) e) For hiking on the trail to be safe, it is necessary but I) (p /\q)v(""'q /\r) not sufficient that berries not be ripe along the trail 9. Let p and q be the propositions and for grizzly bears not to have been seen in the area. p Yo u drive over 65 miles per hour. : I) Hiking is not safe on the trail whenever grizzly bears q Yo u get a speeding ticket. : have been seen in the area and berries are ripe along Write these propositions using p and q and logical the trail. connectives. 12. Determine whether these biconditionals are true or false. a Yo u do not drive over 65 miles per hour. ) a 2 + 2 = 4 if and only if + 1 = 2. b) Yo u drive over 65 miles per hour, but you do not get a ) I b) 1 + 1 2 if and only if2 + 3 4. speeding ticket. = = c 1 + = 3 if and only if monkeys can fly. c) Yo u will get a speeding ticket if you drive over ) I d) 0> if and only if2 > l. 65 miles per hour. I 13. Determine whether each of these conditional statements d) If you do not drive over 65 miles per hour, then you will not get a speeding ticket. is true or false. e) Driving over 65 miles per hour is sufficientfor getting a) If I + I = 2, then 2 + 2 = 5. a speeding ticket. b) If 1 + 1 = 3, then 2 + 2 = 4. c I) Yo u get a speeding ticket, but you do not drive over 65 ) If I + I = 3, then 2 + 2 = 5. miles per hour. d) If monkeys can fly, then 1 + I = 3. g) Whenever you get a speeding ticket, you are driving 14. Determine whether each of these conditional statements over 65 miles per hour. is true or false. 10. Let p, q, and r be the propositions a) If I + I = 3, then unicorns exists. p : Yo u get an A on the final exam. b) If 1 + 1 = 3, then dogs can fly. c q : Yo u do every exercise in this book. ) If I + I = 2, then dogs can fly. A 4, r : Yo u get an in this class. d) If2 + 2 = then I + 2 = 3. 18 I / The Foundations: Logic and Proofs I-Iii

" 15. For each of these sentences, determine whether an inclu­ 20. Write each ofthese statements in the form "if p, then q sive or or an exclusive or is intended. Explain your answer. in English. [Hint: Refer to the list of common ways to a) Coffee or tea comes with dinner. express conditional statements provided in this section.] b) A password must have at least three digits or be at least a) I will remember to send you the address only if you . eight characters long. send me an e-mail message. e) The prerequisite for the course is a course in number b) To be a citizen of this country, it is sufficient that you theory or a course in cryptography. were bornin the United States. d) Yo u can pay using U. S. dollars or euros. e) If you keep your textbook, it will be a usefulref erence 16. For each of these sentences, determine whether an inclu­ in your futurecourses. sive or or an exclusive or is intended. Explain your answer. d) The Red Wings will win the Stanley Cup iftheir goalie a) Experience with C++ or Java is required. plays well. b) Lunch includes soup or salad. e) That you get the job implies that you had the best e) To enter the country you need a passport or a voter credentials. registration card. t) The beach erodes whenever there is a storm. d) Publish or perish. g) It is necessary to have a valid password to log on to the server. For each of these sentences, state what the sentence means 17. You will reach the summit unless you begin your climb ifthe or is an inclusive or (that is, a disjunction) versus an h) too late. exclusive or. Which of these meanings of or do you think is intended? 21. Write each of these propositions in the form "p if and " a) To take discrete mathematics, you must have taken only if q in English. calculus or a course in computer science. a) If it is hot outside you buy an ice cream cone, and if b) When you buy a new car from Acme Motor Company, you buy an ice cream cone it is hot outside. you get $2000 back in cash or a 2% car loan. b) For you to win the contest it is necessary and sufficient e) Dinner for two includes two items from column A or that you have the only winning ticket. three items fromcolumn B. e) Yo u get promoted only if you have connections, and d) School is closed if more than 2 fe et of snow falls or if you have connections only if you get promoted. the wind chill is below -100. d) If you watch television your mind will decay, and " 18. Write each of these statements in the form "if p, then q conversely. in English. [Hint: Refer to the list of common ways to e) The trains run late on exactly those days when I express conditional statements provided in this section.] take it. " a) It is necessary to wash the boss's car to get promoted. 22. Write each of these propositions in the form p if and " b) Winds fromthe south imply a spring thaw. only if q in English. e) A sufficient condition for the warranty to be good is a) For you to get an A in this course, it is necessary and that you bought the computer less than a year ago. sufficientthat you learn how to solve discrete mathe­ d) Willy gets caught whenever he cheats. matics problems. e) Yo u can access the website only if you pay a subscrip­ b) If you read the newspaper every day, you will be in­ tion fee. formed, and conversely. t) Getting elected follows from knowing the right e) It rains ifit is a weekend day, and it is a weekend day people. ifit rains. g) Carol gets seasick whenever she is on a boat. d) You can see the wizard only if the wizard is not in, " 19. Write each of these statements in the form "if p, then q and the wizard is not in only if you can see him. in English. [Hint: Refer to the list of common ways to 23. State the converse, contrapositive, and inverse of each of express conditional statements.] these conditional statements. a) It snows whenever the wind blows fromthe northeast. a) Ifit snows today, I will ski tomorrow. b) The apple trees will bloom if it stays warm for a week. b) I come to class whenever there is going to be a quiz. e) That the Pistons win the championship implies that e) A positive integer is a prime only if it has no divisors they beat the Lakers. other than and itself. d) It is necessary to walk 8 miles to get to the top of I Long's Peak. 24. State the converse, contrapositive, and inverse of each of e) To get tenure as a professor, it is sufficient to be these conditional statements. world-famous. a) Ifit snows tonight, then I will stay at home. t) If you drive more than 400 miles, you will need to buy b) I go to the beach whenever it is a sunny summer day. gasoline. c) When I stay up late, it is necessary that I sleep until g) Yo ur guarantee is good only if you bought your CD noon. player less than 90 days ago. 25. How many rows appear in a truth table for each of these h) Jan will go swimming unless the water is too cold. compound propositions? /-/9 1.1 Propositional Logic 19

p -+ -'p Construct a truth table for a) 35. (p ++ q) ++ (r ++ s). b) (p v -.r)/\ (q V -.s ) 36. What is the value of x after each of these statements is c) q v p v -'s V -.r V -.t V u encountered in a computer program, if x = 1 before the d) (p /\ r /\ t) ++ (q /\ t) statement is reached? if 1 + 2 3 x x + 1 26. How many rows appear in a truth table for each of these a) = then := compound propositions? b) if (1 + 1 = 3) OR (2 + 2 = 3) then x := x + 1 a) (q -+ -'p)v (-'P -+ -.q) c) if (2 + 3 = 5) AND (3 + 4 = 7) then x := x + 1 b) (p v -.t)/\ (p v d) if (1 + 1 = 2) XOR (1 + 2 = 3) then x := x + 1 -'s) -.t) (-,u v) e if x < 2 then x := x + 1 c) (p -+ r) V (-,s -+ v -+ ) d) (p /\ r /\ s) V (q /\ t) V (r /\ -.t) 37. Find the bitwise OR, bitwise AND, and bitwise XOR of each ofthese pairs of bit strings. 27. Construct a truth table for each of these compound propositions. a) 101 1110, 010 0001 b) 1111 0000, 1010 1010 a) p /\ -.p b) P v -'p c) (p v -'q) -+q d) (p vq)-+(p/\q) c) 00 0111 0001, 10 0100 1000 e) (p -+ q) ++ (-.q -+ -'p) d) 11 1111 1111, 00 0000 0000 t) (p -+ q) -+ (q -+ p) 38. Evaluate each of these expressions. a 1000 /\(0 1011 v 1 1011) 28. Construct a truth table for each of these compound ) I propositions. b) (0 1111 /\ 1 0101) v 0 1000 c) (0 1010 tIJ 1 1011) tIJ 0 1000 a) p -+ -'p b) P ++ -'P d) (1 1011 v 01010) /\ (1 0001 v 1 1011) c) ptIJ(pvq) d) (p /\ q) -+ (p v q) e) (q -+ -'p) ++ (p ++ q) Fuzzy logic is used in artificial intelligence. In fuzzy logic, a t) (p ++ q) tIJ(p ++ -.q) proposition has a truth value that is a number between 0 and I inclusive. A proposition with a truth value of 0 is false and one, 29. Construct a truth table for each of these compound propositions. with a truth value of 1 is true. Truth values that are between 0 and 1 indicate varying degrees of truth. For instance, the truth a) (p vq)-+(p tIJq) b) (p tIJ q) -+ (p /\ q) value 0.8 can be assigned to the statement "Fred is happy," c) (p V q) tIJ (p /\ q) d) (p ++ q) tIJ (-'P ++ q) because Fred is happy most of the time, and the truth value e) (p ++ q) tIJ (-'P ++ -.r) 0.4 can be assigned to the statement "John is happy," because t) (p tIJq) -+ (p tIJ -.q) John is happy slightly less than halfthe time. Construct a truth table for each of these compound 30. 39. The truth value of the negation of a proposition in fuzzy propositions. logic is 1 minus the truth value of the proposition. What a) p tIJ p b) P tIJ -'p are the truth values of the statements "Fred is not happy" c) p tIJ -.q d) -.ptIJ -.q and "John is not happy"? e) (p tIJ q) v (p tIJ -.q) t) (p tIJ q) /\ (p tIJ -.q) 40. The truth value of the conjunction of two propositions in 31. Construct a truth table for each of these compound fuzzy logic is the minimum of the truth values of the two propositions. propositions. What are the truth values of the statements "Fred and John are happy" and "Neither Fred nor John is a) p -+ -.q b) -'p ++ q c) (p -+ q) v (-'P -+ q) d) (p -+ q) /\ (-'P -+ q) happy"? e) (p ++ q) v (-'P ++ q) 41. The truth value of the disjunction of two propositions in t) (-'P ++ -.q) ++ (p ++ q) fuzzy logic is the maximum ofthe truth values of the two propositions. What are the truth values of the statements 32. Construct a truth table for each of these compound propositions. "Fred is happy, or John is happy" and "Fred is not happy, or John is not happy"? a) (p vq)vr b) (p vq)/\r Is the assertion "This statement is false" a proposition? c) (p /\q) V r d) (p /\ q) /\ r *42. The nth statement in a list of 100 statements is "Exactly e) (p V q) /\ -.r t) (p /\ q) V -'r *43. n ofthe statements in this list are false." 33. Construct a truth table for each of these compound propositions. a) What conclusions can you draw from these statements? a p -+ (-.q r ) V ) b) Answer part (a) if the nth statement is "At least n of b) -'p -+ (q -+ r) the statements in this list are false." c) (p -+ q) v (-'P -+ r) c) Answer part (b) assuming that the list contains d) (p -+ q) /\ (-'P -+ r) 99 statements. e (p ++ q) v (-.q ++ r) ) 44. An ancient Sicilian legend says that the barber in a re­ t) (-'P ++ -.q) ++ (q ++ r) mote town who can be reached only by traveling a dan­ 34. Construct a truth table for «p -+ q) -+ r) -+ s. gerous mountain road shaves those people, and only those 20 1 / The Foundations: Logic and Proofs /-20

people, who do not shave themselves. Can there be such then they can save new files.Ifusers cannot save new files, a barber? then the system software is not being upgraded." 45. Each inhabitant of a remote village always tells the truth 51. Are these system specifications consistent? "The router or always lies. A villager will only give a "Yes" or a "No" can send packets to the edge system only if it supports the response to a question a tourist asks. Suppose you are a new address space. For the router to support the new ad­ tourist visiting this area and come to a fo rk in the road. dress space it is necessary that the latest software release One branch leads to the ruins you want to visit; the other be installed. The router can send packets to the edge sys­ branch leads deep into the jungle. A villager is standing tem if the latest software release is installed, The router at the fo rk in the road. What one question can you ask the does not support the new address space." villager to determine which branch to take? 52. Are these system specifications consistent? "If the file 46. An explorer is captured by a group of cannibals. There are system is not locked, then new messages will be queued. two types of cannibals-those who always tell the truth If the file system is not locked, then the system is func­ and those who always lie. The cannibals will barbecue tioning normally, and conversely. Ifnew messages are not the explorer unless he can determine whether a particular queued, then they will be sent to the message buffer. If the cannibal always lies or always tells the truth. He is allowed file system is not locked, then new messages will be sent to ask the cannibal exactly one question. to the message buffer. New messages will not be sent to a) Explain why the question "Are you a liar?" does not the message buffer." work. 53. What Boolean search would you use to look for Web pages b) Find a question that the explorer can use to determine about beaches in New Jersey? What if you wanted to find whether the cannibal always lies or always tells the Web pages about beaches on the isle of Jersey (in the truth. English Channel)? What Boolean search would you use to look for Web pages 47. Express these system specificationsusing the propositions 54. p "The message is scanned for viruses" and q "The mes­ about hiking in We st Virginia? What if you wanted to sage was sent fr om an unknown system" together with find Web pages about hiking in Virginia, but not in West logical connectives. Virginia? a) "The message is scanned for viruses whenever the Exercises 55-59 relate to inhabitants of the island of knights message was sent from an unknown system." and knaves created by Smullyan, where knights always tell the b) "The message was sent from an unknown system but truth and knaves always lie. Yo u encounter two people, A and it was not scanned for viruses." Determine, if possible, what A and are if they address c) "It is necessary to scan the message for viruses when­ youB. in the ways described. Ifyou cannot Bdetermine what these ever it was sent fr om an unknown system." two people are, can you draw any conclusions? d) "When a message is not sent froman unknown system 55. A says "At least one of us is a knave" and says nothing. it is not scanned for viruses." B 56. A says "The two of us are both knights" and says " A is 48. Express these system specifications using the proposi­ a knave." B tions p "The user enters a valid password," q "Access is A says "I am a knave or is a knight" and says nothing. granted," and r "The user has paid the subscription fe e" 57. Both A and say "I amB a knight." B and logical connectives. 58. A says "We areB both knaves" and says nothing. a) "The user has paid the subscription fe e, but does not 59. B enter a valid password." Exercises 60-65 are puzzles that can be solved by translating b) "Access is granted whenever the user has paid the sub­ statements into logical expressions and reasoning from these scription fee and enters a valid password." expressions using truth tables. c) "Access is denied ifthe user has not paid the subscrip­ 60. The police have three suspects for the murder of Mr. tion fe e." Cooper: Mr. Smith, Mr. Jones, and Mr. Williams. Smith, d) "If the user has not entered a valid password but has Jones, and Williams each declare that they did not kill paid the subscription fe e, then access is granted." Cooper. Smith also states that Cooper was a friend of Jones and that Williams disliked him. Jones also states 49. Are these system specifications consistent? "The system is in multiuser state if and only if it is operating normally. that he did not know Cooper and that he was out of town If the system is operating normally, the kernel is func­ the day Cooper was killed. Williams also states that he saw tioning. The kernel is not functioning or the system is in both Smith and Jones with Cooper the day of the killing interrupt mode. If the system is not in multiuser state, and that either Smith or Jones must have killed him. Can then it is in interrupt mode. The system is not in interrupt you determine who the murderer was if mode." a) one of the three men is guilty, the two innocent men are telling the truth, but the statements of the guilty 50. Are these system specifications consistent? "Whenever the system software is being upgraded, users cannot ac­ man may or may not be true? cess the file system. If users can access the file system, b) innocent men do not lie? 1-21 1.2 Propositional Equivalences 21

61. Steve would like to determine the relative salaries ofthree "Diana did it." Diana said "Carlos lied when he said that coworkers using two facts. First, he knows that if Fred I did it." is not the highest paid of the three, then Janice is. Sec­ a) Ifthe authorities also know that exactly one of the fo ur ond, he knows that if Janice is not the lowest paid, then suspects is telling the truth, who did it? Explain your Maggie is paid the most. Is it possible to determine the reasoning. relative salaries of Fred, Maggie, and Janice from what b) If the authorities also know that exactly one is lying, Steve knows? If so, who is paid the most and who the who did it? Explain your reasoning. least? Explain your reasoning. *65. Solve this famous logic puzzle, attributed to Albert Einstein, and known as the zebra puzzle. Five men with 62. Five friends have access to a chat room. Is it possible to determine who is chatting if the fo llowing information is different nationalities and with differentjo bs live in con- known? Either Kevin or Heather, or both, are chatting. � secutive houses on a street. These houses are painted Either Randy or Vijay, but not both, are chatting. If Abby different colors. The men have differentpets and have dif­ is chatting, so is Randy. Vijay and Kevin are either both ferent favorite drinks. Determine who owns a zebra and chatting or neither is. If Heather is chatting, then so are whose favorite drink is mineral water (which is one ofthe Abby and Kevin. Explain your reasoning. favorite drinks) given these clues: The Englishman lives in the red house. The Spaniard owns a dog. The Japanese 63. A detective has interviewed fo ur witnesses to a crime. man is a painter. The Italian drinks tea. The Norwegian From the stories of the witnesses the detective has con­ lives in the first house on the left. The green house is cluded that if the butler is telling the truth then so is the immediately to the right of the white one. The photogra­ cook; the cook and the gardener cannot both be telling the pher breeds snails. The diplomat lives in the yellow house. truth; the gardener and the handyman are not both lying; Milk is drunk in the middle house. The owner ofthe green and if the handyman is telling the truth then the cook is house drinks coffee. The Norwegian's house is next to the lying. For each ofthe four witnesses, can the detective de­ blue one. The violinist drinks orange juice. The fox is in termine whether that person is telling the truth or lying? a house next to that of the physician. The horse is in a Explain your reasoning. house next to that of the diplomat. [Hint: Make a table 64. Four friends have been identifiedas suspects for an unau­ where the rows represent the men and columns represent thorized access into a computer system. They have made the color of their houses, their jobs, their pets, and their statements to the investigating authorities. Alice said favorite drinks and use logical reasoning to determine the "Carlos did it." John said "I did not do it." Carlos said correct entries in the table.]

1.2 Propositional Equivalences

Introduction An important type of step used in a mathematical argument is the replacement of a statement with another statement with the same truth value. Because of this, methods that produce propo­ sitions with the same truth value as a given compound proposition are used extensively in the construction of mathematical arguments. Note that we will use the term "compound propo­ sition" to refer to an expression formed from propositional variables using logical operators, such as /\ We pbegin q. our discussion with a classification of compound propositions according to their possible truth values.

DEFINITION 1 A compoundproposition that is always true,no matterwhat the truth values ofthe propositions that occur in it, is called a tautology. A compound proposition that is always false is called a contradiction. A compound proposition thatis neither a tautology nor a contradiction is called a contingency.

Tautologies and contradictions are oftenimportant in mathematical reasoning. Example I illus­ trates these types of compound propositions. 28 I / The Foundations: Logic and Proofs 1-28

Exercises

Use truth tables to verifythese equivalences. Show that each of these conditional statements is a tau­ 1. 9. tology by using truth tables. a) p /\ T == p b) p V F == P c) P /\ F == F d) p v T == T a) (P /\q) -,; p b) p -'; (p v q) e) p v p == p P /\P == P c) -'p-'; (p -'; q) d) (p /\q) -'; (p -'; q) t) e) -'(p q) -'; P t) -'(p-'; q) -'; -.q Show that -'(-'p)and p are logically equivalent. -'; 2. Show that each of these conditional statements is a tau­ Use truth tables to verifythe commutative 10. 3. tology by using truth tables. laws a) [-'P /\ (p V q)] -'; q a) p q == q p p /\ q == q /\ V v b) P b) [(p -'; q) (q/\ -'; r)] -'; (p -'; r) Use truth tables to verify the associative laws 4. c) [p /\(p -'; q)] -'; q a) (p v q) V r == p V (q V r) d) [(p v q) /\ (p -'; r) /\ (q -'; r)] -'; r Show that each conditional statement in Exercise 9 is a b) (p /\q) /\ r == p /\ (q /\r) 11. Use a truth table to verifythe distributive law tautology without using truth tables. 5. Show that each conditional statement in Exercise lO is a p /\ (q V r) == (p /\ q) V (p /\r) . 12. tautology without using truth tables. Use a truth table to verify the first De Morgan law 6. Use truth tables to verifythe absorption laws. -'(p /\ q) == -'pv -'q. 13. a) p v (p /\q) == p b) p /\(p V q) == P Use De Morgan's laws to findthe negation of each of the 7. Determine whether is a fo llowing statements. 14. (-'P /\(p -'; q» -'; -.q tautology. a) Jan is rich and happy. Determine whether is a 15. (-.q /\(p -'; q» -'; -'p b) Carlos will bicycle or run tomorrow. tautology. c) Mei walks or takes the bus to class. Each of Exercises 16-28 asks you to show that two compound d) Ibrahim is smartand hard working. propositions are logically equivalent. To do this, either show 8. Use De Morgan's laws to findthe negation of each of the that both sides are true, or that both sides are false, for exactly fo llowing statements. the same combinations of truth values of the propositional a) Kwame will take ajob in industry or go to graduate variables in these expressions (whichever is easier). school. Show that and are 16. p *+ q (p /\q) V (-'P /\ -.q) b) Yo shiko knows Java and calculus. equivalent. c) James is young and strong. Show that and are logically 17. -'(p*+ q) p *+ -.q d) Rita will move to Oregon or Washington. equivalent.

HENRY MAURICE SHEFFER (1883-1964) Henry Maurice Sheffer, born to Jewish parents in the western Ukraine, emigrated to the United States in 1892 with his parents and six siblings. He studied at the Boston Latin School before entering Harvard, where he completed his undergraduate degree in 1905, his master's in 1907, and his Ph.D. in philosophy in 1908. Afterholding a postdoctoral position at Harvard, Henry traveled to Europe on a fe llowship. Upon returning to the United States, he became an academic nomad, spending one year each at the University of Washington, Cornell, the University of Minnesota, the University of Missouri, and City College in New York. In 1916 he returned to Harvard as a faculty member in the philosophy department. He remained at Harvard until his retirement in 1952. Sheffer introduced what is now known as the Sheffer stroke in 1913; it became well known only afterits use in the 1925 edition of Whitehead and Russell's Principia Mathematica. In this same edition Russell wrote that Sheffer had invented a powerful method that could be used to simplifythe Principia. Because of this comment, Sheffer was something ofa mystery man to logicians, especially because Sheffer, who published little in his career, never published the details of this method, only describing it in mimeographed notes and in a brief published abstract. Sheffer was a dedicated teacher of mathematical logic. He liked his classes to be small and did not like auditors. When strangers appeared in his classroom, Sheffer would order them to leave, even his colleagues or distinguished guests visiting Harvard. Sheffer was barely five feet tall; he was noted for his wit and vigor, as well as for his nervousness and irritability. Although widely liked, he was quite lonely. He is noted for a quip he spoke at his retirement: "Old professors never die, they just become emeriti." Sheffer is also credited with coining the term "Boolean algebra" (the subject of Chapter 11 ofthis text). Sheffer was briefly married and lived most of his later life in small rooms at a hotel packed with his logic books and vast files of slips of paper he used to jot down his ideas. Unfortunately, Sheffer suffered from severe depression during the last two decades of his life. 1-29 1.2 Propositional Equivalences 29

18. Show that p -+ qand �q -+ �p are logically equivalent. combination of values for which the propositional vari­ able is true. Each conjunction should include each of the 19. Show that �p ++ qand p ++ �q are logically equivalent. three propositional variables or their negations.] 20. Show that �(p EB q) and p ++ q are logically equivalent. l? Suppose that a truth table in n propositional variables is 21. Show that �(p ++ q) and �p ++ q are logically 42. equivalent. specified. Show that a compound proposition with this truth table can be formed by taking the disjunction of Show that (p -+ q) /\(p -+ r) and p -+ (q /\ r) are log­ 22. conjunctions of the variables or their negations, with one ically equivalent. conjunction included for each combination of values for 23. Show that (p -+ r) /\(q -+ r) and (p v q) -+ r are log­ which the compound proposition is true. The resulting ically equivalent. compound proposition is said to be in disjunctive normal 24. Show that (p -+ q) v (p -+ r) and p -+ (q V r) are log­ fo rm. ically equivalent. A collection of logical operators is called functionally com­ 25. Show that (p -+ r) V (q -+ r) and (p /\ q) -+ r are log­ plete if every compound proposition is logically equiva­ ically equivalent. lent to a compound proposition involving only these logical 26. Show that � p -+ (q -+ r) and q -+ (p V r) are logically operators. equivalent. 43. Show that �, /\, and v form a functionally complete col­ 27. Show that p ++ q and (p -+ q) /\(q -+ p) are logically lection oflogical operators. [Hint: Use the fact that every equivalent. compound proposition is logically equivalent to one in 28. Show that p ++ qand �p ++ �q are logically equivalent. disjunctive normal form, as shown in Exercise 42.] 29. Show that (p -+ q) /\ (q -+ r) -+ (p -+ r)isatautology. *44. Show that � and /\ form a functionally complete collec­ Show that (p v q) /\ (�p V r) -+ (q V r) is a tautology. tion of logical operators. [Hint: First use a De Morgan 30. law to show that p v q is equivalent to �(�p /\ �q).] 31. Show that (p -+ q) -+ r and p -+ (q -+ r) are not equivalent. *4 5. Show that � and v form a functionally complete collec- tion oflogical operators. 32. Show that (p /\ q) -+ r and (p -+ r) /\(q -+ r) are not equivalent. The fo llowing exercises involve the logical operators NA ND (r -+ s) d(p -+ r) -+ (q -+ s) and NOR. The proposition p NA ND q is true when either p or 33. Show that (p -+ q) -+ an are not logically equivalent. q, or both, are false; and it is false when both p and q are true. The proposition p q is true when both p and q are false, The dual of a compound proposition that contains only the NOR and it is false otherwise. The propositions p q and p logical operators v, /\, and � is the compound proposition NA ND q are denoted by p I q and p + q, respectively. (The op­ obtained by replacing each v by /\, each /\ by v, each T by NOR erators I and + are called the Sheffer stroke and the Peirce F, and each F by T. The dual of s is denoted by s*. arrow after H. M. Sheffer and C. S. Peirce, respectively.) 34. Find the dual of each ofthese compound propositions. Construct a truth table for the logical operator NA ND . a) p V �q b) P /\(q V (r /\T» 46. c) (p /\�q)v(q/\F) 47. Show that p I q is logically equivalent to �(p /\q). Construct a truth table for the logical operator NOR. 35. Find the dual of each of these compound propositions. 48. a) p/\�q/\�r b) (p /\q/\r)vs 49. Show that p + q is logically equivalent to �(p V q). c) (p v F) /\(q v T) 50. In this exercise we will show that {+} is a functionally complete collection of logical operators. 36. When does s* = s, where s is a compound proposition? a) Show that p + p is logically equivalent to �p. 37. Show that (s*)* = s when s is a compound proposition. b) Show that (p + q) + (p + q) is logically equivalent to 38. Show that the logical equivalences in Table 6, except for the double negation law, come in pairs, where each pair pvq. contains compound propositions that are duals of each c) Conclude from parts (a) and (b), and Exercise 49, that other. {+} is a functionally complete collection of logical operators. **39. Why are the duals of two equivalent compound proposi­ tions also equivalent, where these compound propositions *51. Find a compound proposition logically equivalent to contain only the operators /\, v,and �? p -+ q using only the logical operator +. Show that {I} is a functionallycomplete collection oflog­ 40. Find a compound proposition involving the propositional 52. variables p, q, and r that is true when p and q are true and ical operators. r is false, but is false otherwise.[H int: Use a conjunction 53. Show that p I q and q I p are equivalent. of each propositional variable or its negation.] 54. Show that p I (q I r) and (p I q) I r are not equivalent, so that the logical operator I is not associative. 41. Find a compound proposition involving the propositional variables p, q, and r that is true when exactly two of p, q, *55. How many different truth tables of compound proposi­ and r are true and is false otherwise. [Hint: Form a dis­ tions are there that involve the propositional variables p junction of conjunctions. Include a conjunction for each and q? 30 1 / The Foundations: Logic and Proofs /-30

56. Show that if p, q, and r are compound propositions simultaneously true by an assignment of truth values to such that p and q are logically equivalent and q and r p, q,r, and s? are logically equivalent, then p and r are logically A compound proposition is satisfiable if there is an assign­ equivalent. ment of truth values to the variables in the compound propo­ 57. The following sentence is taken fromthe specificationof sition that makes the statement form true. a telephone system: "If the directory database is opened, 60. Which of these compound propositions are satisfiable? then the monitor is put in a closed state, if the system is a) (p q V -,r (p V -'q V -,s) (p V V /\ v ) /\ /\ -'r -,s) not in its initial state." This specificationis hard to under­ (-'p v -'q v -,s) /\ (p V q V -,s) stand because it involves two conditional statements. Find b) (-'p v -'q v r /\ (-'p v q v -,s) /\ (p V -'qV -'s) /\ an equivalent, easier-to-understand specification that in­ ) (-'p v -,r V -,s /\ (p V q V -'r /\ (p V -'r V -,s volves disjunctions and negations but not conditional ) ) ) c) (p v q v r /\ (p V -'q V -,s) /\ (q V -'r V s) /\ statements. ) (-'p v r V s) /\ (-'p v q v -,s) /\ (p V -'q V -,r) /\ 58. How many of the disjunctions p v -'q, -'pv q, q V r, (-'p v -'q v s) /\ (-'p v -'r V -,s) q V -'r,and -'qv -,rcan be made simultaneously true 61. Explain how an algorithm for determining whether a by an assignment of truth values to p, q, and r? compound proposition is satisfiable can be used to de­ 59. How many of the disjunctions p v -'q v s, -,pV -,rV s, termine whether a compound proposition is a tautology. -'p V -'rV -,s, -'p v q v -'s,q V r V -'s,q V -,rV -'S , [Hint: Look at -'P,where p is the compound proposition -'pV -'q V -'S , P v r v s, and p v r v-'s can be made that is being examined.]

1.3 Predicate� and Quantifiers

Introduction

Propositional logic, studied in Sections 1.1 and 1.2, cannot adequately express the meaning of statements in mathematics and in natural language. For example, suppose that we know that

"Every computer connected to the university network is functioningproperly."

No rules of propositional logic allow us to conclude the truth of the statement

"MATH3 is functioningprope rly,"

where MATH3 is one of the computers connected to the university network. Likewise, we cannot use the rules of propositional logic to conclude fromthe statement

"CS2 is under attack by an intruder,"

where CS2 is a computer on the university network, to conclude the truth of

"There is a computer on the university network that is under attack by an intruder."

In this section we will introduce a more powerfultype of logic called predicate logic. We . will see how predicate logic can be used to express the meaning of a wide range of statements in mathematics and computer science in ways that permit us to reason and explore relationships between objects. To understand predicate logic, we first need to introduce the concept of a predicate. Afterward, we will introduce the notion of quantifiers, which enable us reason with statements that assert that a certain property holds for all objects of a certain type and with statements that assert the existence of an object with a particular property. 46 I / The Foundations: Logic and Proofs 1-46

A new predicate teaches(p, s), representing that professor p teaches student s, can be definedusing the Prolog rule

teaches (P, S) :- instructor (P,C) , enrolled (S,C)

which means that teaches(p, s) is true if there exists a class c such that professor p is the instructor of class c and student s is enrolled in class c. (Note that a comma is used to represent a conjunction of predicates in Prolog. Similarly, a semicolon is used to represent a disjunction of predicates.) Prolog answers queries using the facts and rules it is given. For example, using the facts and rules listed, the query

?enrolled ( kevin,math2 73 )

produces the response

yes

because the fact enrolled (kevin, math273) was provided as input. The query

?enrolled (X,math273)

produces the response

kevin kiko

To produce this response, Prolog determines all possible values of X for which enrolled(X, math273) has been included as a Prolog fact. Similarly, to find all the professors who are instructors in classes being taken by Juana, we use the query

?teaches (X, juana )

This query returns

patel grossman

Exercises

1. Let P(x) denote the statement "x :s 4." What are the truth c) Q(Massachusetts, Boston) values? d) Q(New Yo rk, New Yo rk) a) P(O) b) P(4) c) P(6) 4. State the value of x afterthe statement if P (x) then x := 1 is executed, where P(x) is the statement "x if the 2. Let P(x) be the statement "the word x contains the letter > I," a." What are the truth values? value of x when this statement is reached is a) P(orange) b) P(lemon) a) x = O. b) x = 1. c) P(true) d) P(false) c) x = 2. 3. Let Q(x, y) denote the statement "x is the capital of y." 5. Let P (x) be the statement "x spends more than fivehours What are these truth values? every weekday in class," where the domain for x consists a) Q(Denver, Colorado) of all students. Express each of these quantifications in b) Q(Detroit, Michigan) English. 1-47 1.3 Predicates and Quantifiers 47

3xP(x) Vx P(x) Vn(n n) 3n(2n 3n) a) b) a) + 1 > b) = 3x .....P( x) "Ix .....P( x) 3n(n -n) Vn(n2 :::: n) e) d) e) = d) 6. Let N(x) be the statement "x has visited North Dakota," 14. Determine the truth value of each of these statements if where the domain consists of the students in your school. the domain consists of all real numbers. Express each of these quantifications in English. 3x(x3 3X(x4 x2) a) = -1) b) < 3xN(x) Vx N(x) ..... 3xN(x) VX « _X)2 x2) Vx(2x x) a) b) e) e) = d) > d) 3x .....N( x) e) .....Vx N(x) t) Vx .....N( x) 15. Determine the truth value of each of these statements if 7. Translate these statements into English, where C(x) is "x the domain for all variables consists of all integers. F(x) "x Vn(n2 3n(n2 is a comedian" and is is funny" and the domain a) :::: 0) b) = 2) Vn(n2 :::: n) 3n(n2 consists of all people. e) d) < 0) a) Vx (C(x) -+ F(x» b) Vx (C(x) /\ F(x» 16. Determine the truth value of each of these statements if 3x(C(x) F(x» 3x(C(x) F(x» the domain of each variable consists of all real numbers. e) -+ d) /\ 3x(x2 3x(x2 8. Translate these statements into English, where R(x) is "x a) = 2) b) = -1) Vx(x2 Vx (x2 =1= x) is a rabbit" and H (x) is "x hops" and the domain consists e) + 2 :::: 1) d) of all animals. 17. Suppose that the domain of the propositional function a) Vx (R(x) -+ H(x» b) Vx (R(x) /\ H(x» P(x) consists of the integers 0, 1, 2, 3, and 4. Write out 3x(R(x) H(x» 3x(R(x) H(x» each of these propositions using disjunctions, conjunc­ e) -+ d) /\ 9. Let P(x) be the statement "x can speak Russian" and let tions, and negations. 3x P(x) Vx P(x) 3x .....P( x) Q(x) be the statement "x knows the computer language a) b) e) C++." Express each of these sentences in terms of P (x), d) Vx .....P( x) e) .....3x P(x) t) .....Vx P(x) Q(x), quantifiers, and logical connectives. The domain 18. Suppose that the domain of the propositional function for quantifiers consists of all students at your school. P(x) consists of the integers -2, -1, 0, 1, and 2. Write out each of these propositions using disjunctions, con­ a) There is a studentat your school who can speak Rus­ sian and who knows C++. junctions, and negations. b) There is a student at your school who can speak Rus­ 3xP(x) Vx P(x) 3x .....P( x) a) b) e) sian but who doesn't know C++. d) Vx .....P( x) e) .....3x P(x) t) .....Vx P(x) Every student at your school either can speak Russian e) 19. Suppose that the domain of the propositional function or knows C++. P(x) consists of the integers 1, 2, 3, 4, and 5. Express No student at your school can speak Russian or knows d) these statements without using quantifiers, instead using C++. only negations, disjunctions, and conjunctions. 10. Let C(x) be the statement "x has a cat," let D(x) be the a 3x P(x) b) Vx P(x) statement "x has a dog," and let F(x) be the statement "x ) .....3x P(x) Vx P(x) has a ferret." Express each of these statements in terms e) d) ..... of C (x), D(x), F (x), quantifiers, and logical connectives. e) Vx« x =1= 3) -+ P(x» v 3x..... P( x) Let the domain consist of all students in your class. 20. Suppose that the domain of the propositional function P(x) consists of -5, - 3 , -1, 1, 3, and 5. Express these a) A student in your class has a cat, a dog, and a ferret. b) All students in your class have a cat, a dog, or a ferret. statements without using quantifiers, instead using only negations, disjunctions, and conjunctions. e) Some student in your class has a cat and a fe rret, but 3xP(x) Vx P(x) not a dog. a) b) Vx « x =1= P(x» d) No student in your class has a cat, a dog, and a ferret. e) 1) -+ e) For each of the three animals, cats, dogs, and ferrets, d) 3x« x :::: 0) /\ P(x» there is a student in your class who has one of these e) 3x( .....P( x» /\ Vx« x < 0) -+ P(x» animals as a pet. 21. For each of these statements finda domain for which the 11. Let P(x) be the statement "x = x2." If the domain con­ statement is true and a domain for which the statement is sists ofthe integers, what are the truth values? false. P(O) P(l) P( ) a) b) e) 2 a) Everyone is studying discrete mathematics. d) P(-l) e) 3x P(x) t) Vx P(x) b) Everyone is older than 21 years. Q(x) "x x 12. Let be the statement + 1 > 2 ." If the domain e) Every two people have the same mother. consists of all integers, what are these truth values? d) No two different people have the same grandmother. Q(O) Q(-I) Q(l) a) b) e) 22. For each of these statements finda domain for which the d) 3x Q(x) e) Vx Q(x) t) 3x .....Q( x) statement is true and a domain for which the statement is g) Vx .....Q( x) false. 13. Determine the truth value of each of these statements if a) Everyone speaks Hindi. the domain consists of all integers. b) There is someone older than 21 years. 48 1 / The Foundations: Logic and Proofs 1-48

e) Every two people have the same firstname. b) All tools are in the correct place and are in excellent d) Someone knows more than two other people. condition. 23. Translate in two ways each of these statements into logi­ e) Everything is in the correct place and in excellent cal expressions using predicates, quantifiers, and logical condition. connectives. First, let the domain consist of the students d) Nothing is in the correct place and is in excellent in your class and second, let it consist of all people. condition. a) Someone in your class can speak Hindi. e) One of your tools is not in the correct place, but it is b) Everyone in your class is friendly. in excellent condition. 29. Express each of these statements using logical operators, e) There is a person in your class who was not born in California. predicates, and quantifiers. d) A student in your class has been in a movie. a) Some propositions are tautologies. e) No student in your class has taken a course in logic b) The negation of a contradiction is a tautology. programming. e) The disjunction of two contingencies can be a 24. Translate in two ways each of these statements into logi­ tautology. cal expressions using predicates, quantifiers, and logical d) The conjunction of two tautologies is a tautology. connectives. First, let the domain consist of the students 30. Suppose the domain of the propositional function P (x , y) in your class and second, let it consist of all people. consists of pairs x and y, where x is I, 2, or 3 and y is a) Everyone in your class has a cellular phone. 1, 2, or 3. Write out these propositions using disjunctions b) Somebody in your class has seen a foreign movie. and conjunctions. There is a person in your class who cannot swim. e) a) 3x P(x, 3) b) 'v'y P(I,y) All students in your class can solve quadratic d) e) 3y-'P(2, y) d) 'v'x -'P(x, 2) equations. 31. Suppose that the domain of Q(x, y, z) consists of triples e) Some student in your class does not want to be rich. x, y, z, where x = 0, I, or 2, y = 0 or I, and z = 0 or 1. 25. Translate each ofthese statements into logical expressions Write out these propositions using disjunctions and con­ using predicates, quantifiers, and logical connectives. junctions. a) No one is perfect. a) 'v'yQ(O, y, 0) b) 3x Q(x, I, I) b) Not everyone is perfect. e) 3z-'Q(O, 0, z) d) 3x-'Q(x,O, I) e) All your friends are perfect. 32. Express each ofthese statements using quantifiers. Then d) At least one of your friends is perfect. form the negation of the statement so that no negation is e) Everyone is your friendand is perfect. to the left of a quantifier. Next, express the negation in f) Not everybody is your friend or someone is not simple English. (Do not simply use the words "It is not perfect. the case that.") 26. Translate each ofthese statements into logical expressions a) All dogs have fleas. in three different ways by varying the domain and by using b) There is a horse that can add. predicates with one and with two variables. e) Every koala can climb. a) Someone in your school has visited Uzbekistan. d) No monkey can speak French. b) Everyone in your class has studied calculus and C++. e) There exists a pig that can swim and catch fish. e) No one in your school owns both a bicycle and a 33. Express each of these statements using quantifiers. Then motorcycle. form the negation of the statement, so that no negation d) There is a person in your school who is not happy. is to the left ofa quantifier.Next, express the negation in e) Everyone in your school was born in the twentieth simple English. (Do not simply use the words "It is not century. the case that.") 27. Translate each ofthese statements into logical expressions a) Some old dogs can learnnew tricks. in three differentways by varying the domain and by using b) No rabbit knows calculus. predicates with one and with two variables. e) Every bird can fly. a) A student in your school has lived in Vietnam. d) There is no dog that can talk. b) There is a student in your school who cannot speak e) There is no one in this class who knows French and Hindi. Russian. e) A student in your school knows Java, Prolog, and 34. Express the negation of these propositions using quanti­ C++. fiers, and then express the negation in English. d) Everyone in your class enjoys Thai food. a Some drivers do not obey the speed limit. e Someone in your class does not play hockey. ) ) b) All Swedish movies are serious. 28. Translate each ofthese statements into logical expressions e) No one can keep a secret. using predicates, quantifiers,and logical connectives. d) There is someone in this class who does not have a a) Something is not in the correct place. good attitude. /-49 1.3 Predicates and Quantifiers 49

35. Find a counterexample, if possible, to these universally d) Video on demand can be delivered when there are at quantifiedstatements, where the domain for all variables least 8 megabytes of memory available and the con­ consists of all integers. nection speed is at least 56 kilobits per second. a) 41. Express each of these system specifications using predi­ b) \lx(x2 >::: 0x) v < 0) cates, quantifiers, and logical connectives. c) \lx(x= 1) x a) At least one mail message, among the nonempty set 36. Find\lx(x a counterexamp le, if possible, to these universally of messages, can be saved if there is a disk with more quantifiedstatements, where the domain for all variables than 10 kilobytes of free space. consists of all real numbers. b) Whenever there is an active alert, all queued messages are transmitted. a) b) 2) ¥ ¥ c) The diagnostic monitor tracks the status of all systems c) \lx(x2 > x)0) \lx(x2 except the main console. 37. Express\lx(lx eachl of these statements using predicates and d) Each participant on the conference call whom the host quantifiers. of the call did not put on a special list was billed. a) A passenger on an airline qualifies as an elite flyer if 42. Express each of these system specifications using predi­ the passenger flies more than 25,000 miles in a year cates, quantifiers, andlogical connectives. or takes more than 25 flights during that year. a) Every user has access to an electronic mailbox. b) A man qualifiesfor the marathon if his best previous b) The system mailbox can be accessed by everyone in time is less than 3 hours and a woman qualifies for the group if the filesystem is locked. the marathon if her best previous time is less than 3.5 c) The firewall is in a diagnostic state only if the proxy hours. server is in a diagnostic state. c) A student must take at least 60 course hours, or at least d) At least one router is functioning normally if the 45 course hours and write a master's thesis, and receive throughput is between 100 kbps and 500 kbps and a grade no lower than a B in all required courses, to the proxy server is not in diagnostic mode. receive a master's degree. 43. Determine whether -+ and -+ d) There is a student who has taken more than 21 credit are logically\lx equivalent.(P(x) JustifYQ(x» your \lxanswer.P(x) hours in a semester and received all A's. \IxQ(x) 44. Determine whether � and � Exercises 38-42 deal with the translation between system are logically\lx equivalent.(P(x) JustifYQ(x» your\Ix answer.P( x) specification and logical expressions involving quantifiers. \lxQ(x) 45. Show that v and v are 38. Translate these system specifications intoEnglish where logically equivalent.3x(P(x) Q(x» 3xP(x) 3xQ(x) the predicate y) is is in state y" and where the do­ Exercises 46-49 establish rules for that main for andS(x y , consists"x of all systems and all possible null quantification we can use when a quantifiedvariable does not appear in part states, respectively.x of a statement. a) open) 46. b) 3xS(x, malfunctioning) v diagnostic» Establish these logical equivalences, where does not occur as a free variable in Assume that thex domain is c) \lx(S(x,open) v diagnostic)Sex, d) 3xS(x, available)3x S(x, nonempty. A. e) 3x...,S(x, working) a) v == v 39. Tran\lxslate...,S(x these, specificationsinto English where is b) (\lxP(x» v A == \lx(P(x) v A) "Printer is out of service," is "Printer isF( busyp) ," 47. Establish(3xP( xthese» Alogical 3x (P(x)equivalences, A) where does not is "Printp job is lost," Band (p) is "Printp job is occur as a freevariable in Assume that thex domain is L(queued."j) j Q(j) j nonempty. A. a) 3p(F(p) 1\ B(p» -+ 3jL(j) a) 1\ == 1\ b) -+ b) (\IxP(x» 1\ A == \lx(P(x) 1\ A) c \lpB(p) 1\ 3jQ(j)-+ (3xP(x» A 3x(P(x) A) ) 3j(Q(j) L(j 3pF(p) 48. Establish these logical equivalences, where does not d) » -+ (\lpB(p) 1\ \ljQ(j» 3jL(j) occur as a free variable in Assume that thex domain is 40. Express each of these system specifications using predi­ nonempty. A. cates, quantifiers,and logical connectives. a) -+ == -+ a) When there is less than 30 megabytes free on the hard \lx(A P(x A \lxP(x) b) -+ » == -+ disk, a warning message is sent to all users. 49. Establish3x(A theseP( xlogical» A equivalences,3xP(x) where does not b) No directories in the file system can be opened and occur as a free variable in Assume that thex domain is no files can be closed when system errors have been A. nonempty. detected. c) The filesystem cannot be backed up if there is a user a) \lx(P(x) -+ A) == 3xP(x) -+ A currently logged on. b) 3x(P(x) -+ A) == \lxP(x) -+ A 50 1 I The Foundations: Logic and Proofs 1-50

50. Show that V and v are either by using a semicolon to separate predicates or by not logically"Ix equivalent.P( x) Vx Q(x) Vx(P(x) Q(x)) putting these predicates on separate lines.]

51. Show that 1\ and 1\ are Exercises are based on questions found in the book not logically3x equivalent.P(x) 3x Q(x) 3x(P(x) Q(x» Symbolic Logic59-62 by Lewis Carroll. 52. As mentioned in the text, the notation denotes 59. Let and be the statements is a profes­ "There exists a unique such that3! x P(x)is true." sor,"P( x),is Q(ignorant,"x), R(x)and is vain," respectively."x Express each of these statements using quantifiers; logical con­ If the domain consists of allx integers, whatP(x) are the truth "x "x nectives; and and where the domain values of these statements? consists of all P(people.x), Q( x), R(x), a) > 1) b) = 1) a) No professors are ignorant. c) 3!x(x + 3 = 3!x(x2= + 1) b) All ignorant people are vain. 53. What3! x(arex the truth2x values) ofthese d) 3! statements?x(x x c) No professors are vain. a) -+ Does (c) fo llow from (a) and (b)? b) 3!xP(x)-+ 3xP(x) 60. Letd) and be the statements is a clear c) VxP(x) -+3!xP( x) explanation,"P(x), Q( x), is satisfR(x)actory," and is "xan excuse," 54. Write3!x-'P( out x) -,VxwhereP(x) the domain consists ofthe inte­ respectively. Suppose"x that the domain for"x consists of all gers 1, 2, and3!x P(3, x),in terms of negations, conjunctions, and English text. Express each ofthese statementsx using quan­ disjunctions. tifiers, logical connectives, and and 55. Given the Prolog facts in Example 28, what would Prolog P(x), Q(x), R(x). a) All clear explanations are satisfactory. returngiven these queries? b) Some excuses are unsatisfactory. a) ?instructor ( chan ,math273 ) c) Some excuses are not clear explanations. b) ?instructor (patel ,cs301) Does (c) follow from(a) and (b)? c) ?enrolled (X, cs301) 61.*d) Let and be the statements is ?enrolled (kiko,Y) a baby,"P(x), Q(is x),logical," R(x), is S(ablex) to manage a crocodile,""x d) ?teaches ( grossman,Y) and is"x despised ," respectively."x Suppose that the domain consists of all people. Express each of these statements 56. Givene) the Prolog facts in Example 28, what would Prolog "x using quantifiers; logical connectives; and return when given these queries? and P(x), Q(x), a ?enrolled ( kevin ,ee222 ) ) aR(x),Babies S(arex) illogical. . b) ?enrolled (kiko,math273 ) ) b) Nobody is despised who can manage a crocodile. c) ?instruc tor ( grossman,X) c) Illogical persons are despised. d) ?instructor (X, cs301) Babies cannot manage crocodiles. ?teaches (X, kevin) >te)d) Does (d) fo llow from (a), (b), and (c)? If not, is there 57. e)Suppose that Prolog facts are used to definethe predicates a correct conclusion? mother(M, Y) andfather(F, X), which represent that M 62. Let and be the statements is the mother of Y and F is the father of X, respectively. is a P(duck,"x), Q(x),is R( onex), of my S(poulx) try," is an officer,""x Give a Prolog rule to define the predicate sibling(X, y), and is willing"x to waltz," respectively."x Express each of which represents that X and Y are siblings (that is, have these"x statements using quantifiers; logical connectives; the same mother and the same father). and and 58. P(x), Q(x), R(x), S(x). Suppose that Prolog facts are used to define the pred­ a) No ducks are willing to waltz. icates mother(M, Y) and fa ther(F, X), which repre­ b) No officers ever decline to waltz. sent that is the mother of Y and is the father of M F c) All my poultry are ducks. X, respectively. Give a Prolog rule to define thepredicate My poultry are not officers. grandfather(X, y), which represents that X is the grand­ d) Does (d) fo llow from(a), (b), and (c)? If not, is there father of Y. [Hint: You can write a disjunction in Prolog *e) a correct conclusion?

1.4 Nested Quantifiers

Introdudion

In Section 1.3 we definedthe existential and universal quantifiers and showed how they can be used to represent mathematical statements. We also explained how they can be used to translate 58 I / The Foundations: Logic and Proofs 1-58

Successively applying the rules for negating quantifiedexpr essions, we construct this sequence of equivalent statements

-,vE>0 38 >0 Vx (O < Ix - al <8 -* If(x) - LI < E)

== 3E >0 -.38 >0 Vx (O < Ix - al < 8 -* If(x) - LI < E)

== 3E >0 V8 >0 -.VX(O < Ix - al < 8 -* If(x) - L I < E)

== 3E >0 V8 >0 3x -.(0 < Ix - al < 8 -* If(x) - L I < E)

== 3E >0 V8 >0 3x(0 < Ix - al <8 /\ If(x) - LI � E).

In the last step we used the equivalence -'(p -* q) == P /\ -'q, which fo llows from the fifth equivalence in Table 7 of Section 1.2. Because the statement "limx---+a f(x) does not exist" means fo r all real numbers L, limx---+a f(x) i= L, this can be expressed as

VL3E >0 V8 >0 3x(0 < Ix - al < 8/\If (x) - L I � E).

This last statement says that for every real number L there is a real number E > 0 such that for every real number 8 > 0, there exists a real number x such that 0 < Ix - a I < 8 and If(x) - LI � E. �

Exercises

1. Translate these statements into English, where the domain school and the domain for y consists of all websites. for each variable consists of all real numbers. Express each of these statements by a simple English a) 'v'x3y(x < y) sentence.

b) 'v'x'v'y(((x ::: 0) 1\ (y ::: 0» -+ (xy ::: 0» a) W(Sarah Smith, www.att.com) c) 'v'x'v'y3z(xy = z) b) 3xW(x, www.imdb.org) 2. Translate these statements into English, where the domain c) 3yW(Jose Orez, y) fo r each variable consists of all real numbers. 3y(W(Ashok Puri,y) 1\ W(Cindy Yo on, y» d) 3y'v'z(y i= (David Belcher) 1\ (W(David Belcher, z) a) 3x'v'y(xy = y) - e) b) 'v'x'v'y(((x ::: 0) 1\ (y < 0» -+ (x y > 0» -+ W(y,z» ) c) 'v'x'v'y3z(x = Y + z) 1) 3x 3y'v'z((x i= y) 1\ (W(x,z) B W(y, z))) 3. Let Q(x, y) be the statement "x has sent an e-mail mes­ 6. Let C (x , y) mean that student x is enrolled in class y, sage to y," where the domain for both x and y consists of where the domain for x consists of all students in your all students in your class. Express each of these quantifi­ school and the domain for y consists of all classes being cations in English. given at your school. Express each of these statements by 3x'v'yQ(x,y) a simple English sentence. a) 3x 3y Q(x,y) b) 3y'v'x Q(x, y) c) 'v'x 3y Q(x, y) a) C(Randy Goldberg, CS 252) 'v'y3x Q(x,y) 1)d) 'v'x'v'yQ(x,y) b) 3xC(x, Math 695) 4. Lete) P(x, y) be the statement "student x has taken class c) 3yC(Carol Sitea, y) y," where the domain for x consists of all students in your 3x(C(x, Math 222) 1\ C(x, CS 252»

class and fo r y consists of all computer science courses d) 3x3y'v'z((x i= y) 1\ (C(x , z) -+ C(y, z» ) at your school. Express each of these quantifications in e)1) 3x3y'v'z((x i= y) 1\ (C(x, z) B C(y, z» )

English. 7 . . Let T (x , y) mean that student x likes cuisine y, where the 3x'v'yP(x,y) domain for x consists of all students at your school and a) 3x 3yP(x, y) b) c) 'v'x 3yP(x, y) 3y 'v'x P(x,y) the domain for y consists of all cuisines. Express each of 'v'y3x P(x, y) 1)d) 'v'x'v'yP(x, y) these statements by a simple English sentence. 5. Lete) W (x , y) mean that student x has visited website y, a) -.T(Abdallah Hussein, Japanese) where the domain for x consists of all students in your b) 3xT(x, Korean) 1\ 'v'x T(x, Mexican) 1-59 1.4 Nested Quantifiers S9

c) 3y(T(Monique Arsenault, y) V c) Every faculty member has either asked Professor T(Jay Johnson, y» Miller a question or been asked a question by Profes­ 'v'x'v'z3y« x =f. z) --+ -'(T(x, y) /\ T(z, y» ) sor Miller. d) 3x 3z'v'y(T(x , y) T(z, y» Some student has not asked any faculty member a *+ d) question. t)e) 'v'x'v'z3y(T(x ,y) *+ T(z, y» 8. Let Q(x, y) be the statement "student x has been a There is a faculty member who has never been asked contestant on quiz show y." Express each of these e) a question by a student. sentences in terms of Q(x, y), quantifiers,and logical con­ t) Some student has asked every faculty member a nectives, where the domain for x consists of all students question. at your school and for y consists of all quiz shows on g) There is a faculty member who has asked every other television. faculty member a question. h) Some student has never been asked a question by a a) There is a student at your school who has been a con­ faculty member. testant on a television quiz show. 12. Let (x) be the statement "x has an Internet connection" b) No student at your school has ever been a contestant I on a television quiz show. and C (x , y) be the statement "x and y have chatted over the Internet," where the domain for the variables x and c) There is a student at your school who has been a con­ testant on Jeopardy and on Wheelof Fo rtune. y consists of all students in your class. Use quantifiers to Every television quiz show has had a student from express each ofthese statements. d) your school as a contestant. a) Jerry does not have an Internet connection. At least two students fromyour school have been con­ b) Rachel has not chatted over the Internet with e) testants on Jeopardy. Chelsea. c) Jan and Sharonhave never chatted over the Internet. 9. Let L(x, y) be the statement "x loves y," where the do­ main for both x and y consists of all people in the world. No one in the class has chatted with Bob. Use quantifiers to express each of these statements. d) Sanjay has chatted with everyone except Joseph. t)e) Someone in your class does not have an Internet a) Everybody loves Jerry. connection. b) Everybody loves somebody. g) Not everyone in your class has an Internet connection. c) There is somebody whom everybody loves. h) Exactly one student in your class has an Internet Nobody loves everybody. connection. d) There is somebody whom Lydia does not love. i) Everyone except one student in your class has an t) There is somebody whom no one loves. e) Internet connection. g) There is exactly one person whom everybody loves. j) Everyone in your class with an Internet connection h) There are exactly two people whom Lynn loves. has chatted over the Internet with at least one other i) Everyone loves himself or herself. student in your class. j) There is someone who loves no one besides himself k) Someone in your class has an Internetconnection but or herself. has not chatted with anyone else in your class. 10. Let F(x, y) be the statement "x can fool y," where the do­ I) There are two students in your class who have not main consists of all people in the world. Use quantifiers chatted with each other over the Internet. to express each of these statements. m) There is a student in your class who has chatted with a) Everybody can fo ol Fred. everyone in your class over the Internet. b) Evelyn can fo ol everybody. n) There are at least two students in your class who have c) Everybody can fo ol somebody. not chatted with the same person in your class. There is no one who can fo ol everybody. 0) There are two students in the class who between them d)) Everyone can be fo oled by somebody. have chatted with everyone else in the class. t)e No one can fo ol both Fred and Jerry. 13. Let M (x , y) be "x has sent y an e-mail message" and g) Nancy can fool exactly two people. T (x , y) be "x has telephoned y," where the domain h) There is exactly one person whom everybody can fo ol. consists of all students in your class. Use quantifiers to i) No one can fo ol himself or herself. express each of these statements. (Assume that all e-mail j) There is someone who can fool exactly one person messages that were sent are received, which is not the besides himself or herself. way things oftenwork.) 11. Let S(x) be the predicate "x is a student," F(x) the pred­ a) Chou has never sent an e-mail message to Koko. icate "x is a faculty member," and A(x , y) the predicate b) Arlene has never sent an e-mail message to or tele­ "x has asked y a question," where the domain consists of phoned Sarah. all people associated with your school. Use quantifiersto c) Jose has never received an e-mail message from express each of these statements. Deborah. a) Lois has asked Professor Michaels a question. Every student in your class has sent an e-mail mes­ b) Every student has asked Professor Gross a question. d) sage to Ken. 60 I / The Foundations: Logic and Proofs 1-60

e) No one in your class has telephoned Nina. 16. A discrete mathematics class contains I mathematics 1) Everyone in your class has either telephoned Av i or major who is a freshman, mathematics majors who sent him an e-mail message. are sophomores, computer12 science maj ors who are g) There is a student in your class who has sent everyone sophomores, mathema15 tics maj ors who are juniors, else in your class an e-mail message. computer science2 majors who are juniors, and 1 computer2 h) There is someone in your class who has either sent an science major who is a senior. Express each of these state­ e-mail message or telephoned everyone else in your ments in terms of quantifiers and then determine its truth class. value. i) There are two different students in your class who a) There is a student in the class who is a junior. have sent each other e-mail messages. b) Every student in the class is a computer science maj or. j) There is a student who has sent himself or herself an c) There is a student in the class who is neither a math­ e-mail message. ematics major nor a junior. k) There is a student in your class who has not received d) Every student in the class is either a sophomore or a an e-mail message fromanyone else in the class and computer science major. who has not been called by any other student in the e) There is a major such that there is a student in the class. class in every year of study with that major. Every student in the class has either received an I) 17. Express each of these system specifications using predi­ e-mail message or received a telephone call from cates, quantifiers, and logical connectives, if necessary. another student in the class. a Every user has access to exactly one mailbox. m) There are at least two students in your class such that ) b) There is a process that continues to runduring all error one student has sent the other e-mail and the second conditions only if the kernel is working correctly. student has telephoned the first student. c) All users on the campus network can access all web­ n) There are two different students in your class who sites whose urI has a .edu extension. between them have sent an e-mail message to or *d) There are exactly two systems that monitor every telephoned everyone else in the class. remote server. 14. Use quantifiersand predicates with more than one variable 18. Express each of these system specifications using predi­ to express these statements. cates, quantifiers, and logical connectives, if necessary. a) There is a student in this class who can speak Hindi. a) At least one console must be accessible during every b) Every student in this class plays some sport. fault condition. c) Some student in this class has visited Alaska but has The e-mail address of every user can be retrieved not visited Hawaii. b) whenever the archive contains at least one message d) All students in this class have learned at least one sent by every user on the system. programming language. c For every security breach there is at least one mecha­ e) There is a student in this class who has taken ev­ ) nism that can detect that breach if and only if there is ery course offered by one of the departments in this a process that has not been compromised. school. There are at least two paths connecting every two 1) Some student in this class grew up in the same town d) distinct endpoints on the network. as exactly one other student in this class. e) No one knows the password of every user on the sys­ g) Every student in this class has chatted with at least tem except for the system administrator, who knows one other student in at least one chat group. all passwords. 15. Use quantifiers andpredicates with more thanone variable 19. Express each of these statements using mathematical and to express these statements. logical operators, predicates, and quantifiers, where the Every computer science student needs a course in a) domain consists of all integers. discrete mathematics. a) The sum of two negative integers is negative. b) There is a student in this class who owns a personal The difference of two positive integers is not neces­ computer. b) sarily positive. c) Every student in this class has taken at least one computer science course. c) The sum ofthe squares of two integers is greater than or equal to the square of their sum. d) There is a student in this class who has taken at least one course in computer science. d) The absolute value of the product of two integers is e) Every student in this class has been in every building the product of their absolute values. on campus. 20. Express each of these statements using predicates, quan­ 1) There is a student in this class who has been in every tifiers, logical connectives, and mathematical operators room of at least one building on campus. where the domain consists of all integers. Every student in this class has been in at least one g) a) The product of two negative integers is positive. room of every building on campus. b) The average of two positive integers is positive. /-6/ 1.4 Nested Quantifiers 61

e) The difference of two negative integers is not neces­ e) = sarily negative. 3xVy(xy+ 0) + The absolute value of the sum of two integers does not e)d) 3x3y(x y ::/= y =x) d) exceed the sum ofthe absolute values ofthese integers. t) Vx(x ::/= --+3y(xy 1)1 3xVy(y0::/= 0xy --+ = » 21. Use predicates, quantifiers, logical connectives, and g) Vx3y(x + y = 1) mathematical operators to express the statement that b) + = + = 5) every positive integer is the sum of the squares of four i) 3x3y(x + 2y= 2 /\2x - Y4y = 1) integers. j) Vx3y(x y= 2/\+ 2x 22. Use predicates, quantifiers,logical connectives, and math­ 29. SupposeVxVy3z(z the domain (x ofy)/2)the propositional function ematical operators to express the statement that there is a consists ofpai� and where is 1, 2, or 3 andP(x, y)is positive integer that is not the sum ofthree squares. 1,2, or 3. Write outx thesey, propositions x using disjunctionsy 23. Express each of these mathematical statements using and conjunctions. predicates, qUahtifiers, logical connectives, and mathe­ a) b) matical operators. VxVyP(x,y) 3x3yP(x,y) a) The product of two negative real numbers is positive. e) 30. Rewrite3xVyP( eachx, ofy) these statementsd) Vy3x so thatP(x, negations y) ap­ b) The difference of a real number and itself is zero. pear only within predicates (that is, so that no negation Every positive real number has exactly two square e) is outside a quantifier or an expression involving logical roots. connectives). A negative real number does not have a square root d) that is a real number. a) -.3y3xP(x, y) b) -.Vx3yP(x, y) 24. Translate each of these nested quantificationsinto an En­ e) -.3y(Q(y) /\Vx -.R(x, y» glish statement that expresses a mathematical fact. The d) -.3y(3xR(x,y) v VxS(x, y» domain in each case consists of all real numbers. e) -.3y(Vx3zT(x, y, z) v 3xVzU(x, y, z» a) + = 31. Express the negations of each of these statements so that b) 3xVy(x �y y) < > all negation symbols immediately precede predicates. e) VxVy(((x � 0) /\(y � 0» --+(x -yY > 0» a) 3x3y(((x 0) /\(y 0» _/\ (x - 0» b) Vx3yVzT(x, y, z) 25. Trand) VxVy((xslate each ::/= of0 ) these/\ (y nested 0::/= ) quantifications(xy ::/=0 » into an En­ c) Vx3yP(x,y)vVx3yQ(x,y) glish statement that expresses a mathematical fact. The Vx3y(P(x, y) /\3z R(x, y, z» domain in each case consists of all real numbers. 32. Expressd) Vx3y( theP( negationsx,y)--+ Q(of x, eachy» ofthese statements so that a) = all negation symbols immediately precede predicates. b) 3xVy(xy y) < < > a) VxVy(((x > 0) /\(y < 0» --+(xy 0» e) 3zVyVxT(x,y,z) 3x3y((x2 + y) =/\ (x y» b) d) VxVy3z(x y z) 3x3yP(x,y)/\_VxVy Q(x,y) 26. Let be the statement + = If the e) domainQ(x, for y) both variables consists"x ofy all xintegers, -y." what 3x3y(Q(x, y) Q(Y, x» are the truth values? 33. Rewrited) Vy3x each3z(T( ofx, thesey,z)v statements Q(x,y» so that negations ap­ a) 1) b) pear only within predicates (that is, so that no negation is outside a quantifier or an expression involving logical e) Q(I, Q(2,0) e) VyQ(I, y) t)d) 3xQ(x,2) connectives). g) 3x3yQ(x, y) b) Vx3yQ(x,y) a) b) i) 3yVxQ(x, y) Vy3xQ(x,y) -.VxVyP(x, y) -.Vy3xP(x, y) VxVyQ(x,y) e) -.VyVx(P(x, y) v Q(x, y» 27. Determine the truth value of each of these statements if d) -.(3x3y-.P(x, y) /\VxV yQ(x, y» the domain for all variables consists of all integers. e) -.Vx(3yVzP(x, y, z) /\3zV yP(x, y, z» a) 2 < b) < 34. Find a common domain for the variables and e) Vn3m(n + m)= 3nVm(n =m2) for which the statement x, y,= Vz e) Vn3m(n +m =0) 5) d)t) 3nVm(nm+ m)= 6) = is true and anotherVxVy((x domain ::/= for y)whichVz((z --+ it is fax)lse. g) 3n3m(n2+ m=2 =3n 1)3m (n2 m2 35. Find(z ya» common) domain for the variables and h) 3n3m(n + m = 44/\ n -m= for which the statement x, y, z, i) 3n3m(n m= +/\ n -m 2) w is true and anotherVxVyV z3wcommon((w ::/= domainx) /\(w for ::/= 28. DetermineVnVm3p(p the truth(m valuen)/2) of each of these statements y)these /\ (wvariables ::/= z» for which it is false. if the domain of each variable consists of all real 36. Express each of these statements using quantifiers. Then numbers. form the negation of the statement so that no negation is a) Vx3y(x2 = y) to the left of a quantifier. Next, express the negation in 62 I / The Foundations: Logic and Proofs 1-62

simple English. (Do not simply use the words "It is not that a quadratic polynomial with real number coefficients the case that.") has at most two real roots.

a) No one has lost more than one thousand dollars play­ 45. Determine the truth value ofthe statement = 1) ing the lottery. ifthe domain for the variables consists of Vx 3y( x y b) There is a student in this class who has chatted with a) the nonzero real numbers. exactly one other student. b) the nonzero integers. c) No student in this class has sent e-mail to exactly two c) the positive real numbers. other students in this class. 46. Determine the truth value of the statement :s: Some student has solved every exercise in this book. if the domain for the variables consists of 3xVy(x y2) e)d) No student has solved at least one exercise in every a) the positive real numbers. section of this book. b) the integers. 37. Express each of these statements using quantifiers. Then c) the nonzero real numbers. fo rm the negation of the statement so that no negation is 47. Show that the two statements and to the left of a quantifier. Next, express the negation in where both quantifiers-.3xV overy P(the x, first y) vari­ simple English. (Do not simply use the words "It is not Vxable3y in-.P( x, y),have the same domain, and both quantifiers the case that.") over theP second (x, y) variable in have the same domain, a) Every student in this class has taken exactly two are logically equivalent. P (x, y) mathematics classes at this school. *48. Show that v and v b) Someone has visited every countryin the world except where all quantifiersVxP(x) haveVxQ(x) the sameVxV nonemptyy(P(x) domain,Q(Y» , Libya. are logically equivalent. (The new variable is used to c) No one has climbed every mountain in the Himalayas. combine the quantifications correctly.) y Every movie actor has either been in a movie with *49. a) Show that is logically equivalent d) Kevin Bacon or has been in a movie with someone 1\ to VxP(x) 3xwhereQ(x) all quantifiershave the who has been in a movie with Kevin Bacon. 1\ sameVx3y nonempty (P(x) domain.Q(Y» , 38. Express the negations of these propositions using quan­ b) Show that v is equivalent to tifiers, and in English. v VxP(x) where3xQ(x) all quantifiers have the a) Every student in this class likes mathematics. Vxsame3y (P(nonemptyx) Q(Y domain.» , b) There is a student in this class who has never seen a A statement is in prenex normal fo rm (PNF) if and only if computer. it is of the form c) There is a student in this class who has taken every mathematics course offered at this school. There is a student in this class who has been in at least Q\x\ Q X ... QkXkP(X\, X , ..., Xk), where each 2 2 i = 1 ,2, is2 either the existential quanti­ one room of every building on campus. ..., k, d) fier or the universalQi, quantifier,and is a predicate 39. Find a counterexample, if possible, to these universally involving no quantifiers.For example,P(x\ , ... ,Xk) 1\ quantified statements, where the domain for all variables is in prenex normal form, whereas 3xVy(P(v x, y) Q(is noty» consists of all integers. (because the quantifiers do not all occur3xP( first).x) VxQ(x) a) = -+ = Every statement fo rmed from propositional variables, b) VxVy(x2 = y2 X y) predicates, T, and F using logical connectives and quantifiers c) Vx3y(y2 x) is equivalent to a statement in prenex normal fo rm. Exercise 5 1 asks for a proof of this fact. 40. FindVxVy(xy a counterexam:::x) ple, if possible, to these universally quantified statements, where the domain for all variables *50. Put these statements in prenex normal form. [Hint: Use consists of all integers. logical equivalence fr om Tables 6 and 7 in Section 1.2, Table 2 in Section 1.3, Example 19 in Section 1.3, Ex­ a) = ercises 45 and 46 in Section 1.3, and Exercises 48 and b) Vx3y(x l/y)< 100) 49 in this section.] c Vx3y(y2 -X=1= ) 3 a) v v A, where A is a proposition not 41. UseVxVy(x2 quantifiers y to )express the associative law for multi­ involving3xP(x) any3xQ(x) quanti fiers. plication of real numbers. b) v

42. Use quantifiers to express the distributive laws of multi­ c) -.(VxP(x)-+ VxQ(x» plication over addition for real numbers. **51. Show3x howP(x) to transf3xQ(x)orm an arbitrary statement to a state­ 43. Use quantifiers and logical connectives to express the ment in prenex normal form that is equivalent to the given fact that every linear polynomial (that is, polynomial of statement. degree 1) with real coefficients and where the coefficient *52. Express the quantification introduced on page of is nonzero, has exactly one real root. 38, using universal quantifications,3!xP(x), existential quantifica­ 44. Usex quantifiers and logical connectivesto express the fact tions, and logical operators. 72 I I The Foundations: Logic and Proofs 1-72

Solution: Let P(n) denote "n > 4" and Q(n) denote "n2 < 2n ." The statement "For all positive integers n, ifn is greater than 4, then n2 is less than 2n" can be represented by Vn(P(n) --+ Q(n» , where the domain consists of all positive integers. We are assuming that Vn(P(n) --+ Q(n» is true. Note that P(l 00) is true because 100 > 4. It fo llows by universal modus ponens that Q(n) 2 100 is true, namely that 100 < 2 • �

Another useful combination of a rule of inference from propositional logic and a rule of inference for quantified statements is universal modus tollens. Universal modus toll ens combines universal instantiation and modus tollens and can be expressed in the fo llowing way:

Vx(P(x) --+ Q(x» -'Q(a), where a is a particular element in the domain :. -,P(a)

We leave the verificationof universal modus tollens to the reader (see Exercise 25). Exercise 26 develops additional combinations of rules of inference in propositional logic and quantified statements.

Exercises

1. Find the argument form for the following argument and a) Kangaroos live in Australia and are marsupials. There­ determine whether it is valid. Can we conclude that the fore, kangaroos are marsupials. conclusion is true if the premises are true? b) It is either hotter than 100 degrees today or the pollu­ tion is dangerous. It is less than 100 degrees outside If Socrates is human, then Socrates is mortal. today. Therefore, the pollution is dangerous. Socrates is human. c) Linda is an excellent swimmer. If Linda is an excellent ... Socrates is mortal. swimmer, then she can work as a lifeguard. Therefore, Linda can work as a lifeguard. 2. Find the argument form for the fo llowing argument and Steve will work at a computer company this summer. determine whether it is valid. Can we conclude that the d) Therefore, this summer Steve will work at a computer conclusion is true if the premises are true? company or he will be a beach bum. If George does not have eight legs, then he is not an e) If! work all night on this homework, then I can answer insect. all the exercises. If! answer all the exercises, I will un­ George is an insect. derstand the material. Therefore, ifI work all night on this homework, then I will understand the material. .'. George has eight legs. 5. Use rules ofinference to show that the hypotheses "Randy 3. What rule of inference is used in each of these works hard," "If Randy works hard, then he is a dull boy," arguments? and "If Randy is a dull boy, then he will not get the job" imply the conclusion "Randy will not get the job." a) Alice is a mathematics major. Therefore, Alice is ei­ ther a mathematics major or a computer science major. 6. Use rules of inference to show that the hypotheses "If it b) Jerry is a mathematics major and a computer science does not rain or if it is not foggy, then the sailing race will major. Therefore, Jerry is a mathematics major. be held and the lifesaving demonstration will go on," "If c) If it is rainy, then the pool will be closed. It is rainy. the sailing race is held, then the trophy will be awarded," Therefore, the pool is closed. and "The trophy was not awarded" imply the conclusion If it snows today, the university will close. The uni­ "It rained." d) versity is not closed today. Therefore, it did not snow 7. What rules of inference are used in this famous argu­ today. ment? "All men are mortal. Socrates is a man. Therefore, e) If! go swimming, then I will stay in the sun too long. Socrates is mortal." If! stay in the sun too long, then I will sunburn.There­ 8. What rules of inference are used in this argument? "No fore, if I go swimming, then I will sunburn. man is an island. Manhattan is an island. Therefore, 4. What rule of inference is used in each ofthese arguments? Manhattan is not a man." 1-73 1.5 Rules of Inference 73

9. For each of these sets of premises, what relevant conclu­ b) "Somebody in this class enjoys whale watching. Every sion or conclusions can be drawn?Explain the rules of in­ person who enj oys whale watching cares about ocean fe rence used to obtain each conclusion fromthe premises. pollution. Therefore, there is a person in this class who a) "IfI take the day off,it either rains or snows." "I took cares about ocean pollution." Tuesday offor I took Thursday off.""It was sunny on c) "Each of the 93 students in this class owns a personal Tuesday." "It did not snow on Thursday." computer. Everyone who owns a personal computer b) "If ! eat spicy fo ods, then I have strange dreams." "I can use a word processing program. Therefore, Zeke, have strange dreams if there is thunder while I sleep." a student in this class, can use a word processing pro­ "I did not have strange dreams." gram." c) "I am either clever or lucky." "I am not lucky." "If I d) "Everyone in New Jersey lives within 50 miles ofthe am lucky, then I will win the lottery." ocean. Someone in New Jersey has never seen the e d) "Every computer science major has a personal com­ ocean. Ther fore, someone who lives within 50 miles puter." "Ralph does not have a personal computer." of the ocean has never seen the ocean." "Ann has a personal computer." 14. For each of these arguments, explain which rules of infer­ "What is good for corporations is good for the United ence are used for each step. e) States." "What is good for the United States is good a) "Linda, a student in this class, owns a red convertible. for you." "What is good for corporations is for you to Everyone who owns a red convertible has gotten at buy lots of stuff." least one speeding ticket. Therefore, someone in this t) "A ll rodents gnaw their fo od." "Mice are rodents." class has gotten a speeding ticket." "Rabbits do not gnaw their fo od." "Bats are not ro­ b) "Each of five roommates, Melissa, Aaron, Ralph, Ve ­ dents." neesha, and Keeshawn, has taken a course in discrete 10. For each of these sets of premises, what relevant conclu­ mathematics. Every student who has taken a course in sion or conclusions can be drawn? Explain the rules of in­ discrete mathematics can take a course in algorithms. fe rence used to obtain each conclusion fromthe premises. Therefore, all fiveroommates can take a course in al­ a) "If! play hockey, then I am sore the next day." "I use the gorithms next year." whirlpool if I am sore." "I did not use the whirlpool." c) "All movies produced by John Sayles are wonder­ b) "If! work, it is either sunny or partly sunny.""I worked ful. John Sayles produced a movie about coal min­ last Monday or I worked last Friday." "It was not sunny ers. Therefore, there is a wonderful movie about coal on Tu esday." "It was not partly sunny on Friday." miners." c) "All insects have six legs." "Dragonflies are insects." d) "There is someone in this class who has been to "Spiders do not have six legs." "Spiders eat dragon­ France. Everyone who goes to France visits the Lou­ flies." vre. Therefore, someone in this class has visited the "Every student has an Internetaccount." "Homer does Louvre." d) not have an Internet account." "Maggie has an Internet 15. For each of these arguments determine whether the argu­ account." ment is correct or incorrect and explain why. "All fo ods that are healthy to eat do not taste good." a) All students in this class understand logic. Xavier is e) "Tofu is healthy to eat." "You only eat what tastes a student in this class. Therefore, Xavier understands good." "You do not eat tofu." "Cheeseburgers are not logic. healthy to eat." b) Every computer science major takes discrete mathe­ t) "I am either dreaming or hallucinating." "I am not matics. Natasha is taking discrete mathematics. There­ dreaming." "IfI am hallucinating, I see elephants run­ fore, Natasha is a computer science major. ning down the road." c) All parrots like fruit. My pet bird is not a parrot. There­ 11. Show that the argument fo rm with premises PI, fo re, my pet bird does not like fruit. P2 , ..., Pn and conclusion q -+ r is valid ifthe argument d) Everyone who eats granola every day is healthy. Linda form with premises PI, P2 , ... , Pn , q, and conclusion r is not healthy. Therefore, Linda does not eat granola is valid. every day. 12. Show that the argument fo rm with premises /\ t) -+ 16. For each of these arguments determine whether the argu­ (r v s), q -+ (u /\ t), u -+ and --.s and conclusion(p ment is correct or incorrect and explain why. q -+ r is valid by firstusing p, Exercise 11 and then using a) Everyone enrolled in the university has lived in a dor­ rules of inference fromTab le 1. mitory. Mia has never lived in a dormitory. Therefore, 13. For each of these arguments, explain which rules of infer­ Mia is not enrolled in the university. ence are used for each step. b) A convertible car is fun to drive. Isaac's car is not a a) "Doug, a student in this class, knows how to write convertible. Therefore, Isaac's car is not fun to drive. programs in JAVA . Everyone who knows how to write c) Quincy likes all action movies. Quincy likes the movie programs in JAVA can get a high-paying job. There­ Eight Men Out. Therefore, Eight Men Out is an action fo re, someone in this class can get a high-paying job." movie. 74 1/The Foundations: Logic and Proofs 1-74

All iobstennenset at least a dozen traps. Hamilton is a 26. Justify the rule of universal transitivity, which states d) lobstennan. Therefore, Hamilton sets at least a dozen that ifVx(P(x) � Q(x» and Vx (Q(x) � R(x» are true, traps. then Vx (P(x) � R(x» is true, where the domains of all 17. What is wrong with this argument? Let H(x) be "x is quantifiers are the same. happy." Given the premise 3x H (x), we conclude that 27. Use rules of inference to show that ifVx(P(x) � (Q(x) 1\ H(Lola). Therefore, Lola is happy. Sex»�) and Vx (P(x) 1\ R(x» are true, then Vx (R(x) 1\ 18. What is wrong with this argument? Let S(x , be "x is Sex»� is true. shorter than Given the premise 3s S(s, Max)y), it fo llows 28. Use rules of inference to show that if Vx (P(x) v that S(Max, yMa." x). Then by existential generalization it Q(x» and Vx « --.P(x) 1\ Q(x» � R(x» are true, then fo llows that 3x S(x, x), so that someone is shorter than Vx(--.R(x) � P(x» is also true, where the domains of himself. all quantifiersare the same. 19. Detennine whether each of these arguments is valid. Ifan 29. Use rules of inference to show that ifVx(P(x) v Q(x» , argument is correct, what rule of inference is being used? Vx(--.Q(x) v Sex»�, Vx (R(x) � --.S(x» , and 3x--.P(x) If it is not, what logical error occurs? are true, then 3x--.R(x) is true. 2 a) If n is a real number such that n > 1, then n > 1. 30. Use resolution to show the hypotheses "Allen is a bad boy 2 Suppose that n > 1. Then n > 1. or Hillary is a good girl" and "Allen is a good boy or David 2 b) Ifn is areal number with n > 3, then n > 9. Suppose is happy" imply the conclusion "Hillary is a good girl or 2 that n :s 9. Then n :s 3. David is happy." 2 c) Ifn is a real number with n > 2, then n > 4. Suppose 31. Use resolution to show that the hypotheses "It is not rain­ 2 that n :s 2. Then n :s 4. ing or Yvette has her umbrella," "Yvette does not have 20. Detennine whether these are valid arguments. her umbrella or she does not get wet," and "It is raining a) If x is a positive real number, then x2 is a positive real or Yvette does not get wet" imply that "Yvette does not number. Therefore, if a2 is positive, where a is a real get wet." number, then a is a positive real number. 32. Show that the equivalence p 1\ --'p == F can be derived b) If x2 =1= 0, where x is a real number, then x =1= O. Let a using resolution together with the fact that a condi­ be a real number with a2 =1= 0; then a =1= O. tional statement with a false hypothesis is true. [Hint: Let 21. Which rules of inference are used to establish the q = r = F in resolution.] conclusion of Lewis Carroll's argument described in 33. Use resolution to show that the compound proposition Example 26 of Section 1.3? (p v q) 1\ (--'P V q) 1\ (p V --.q) 1\ (--'P V --'q) is not sat­ 22. Which rules of inference are used to establish the conclu­ isfiable. sion of Lewis Carroll's argument described in Example *34. The Logic Problem, taken from WFF 'N PROOF, The 27 of Section 1.3? Game of Logic, has these two assumptions: 23. Identify the error or errors in this argument that sup­ 1. "Logic is difficultor not many students like logic." posedly shows that if 3x P(x) 1\ 3x Q(x) is true then 2. "If mathematics is easy, then logic is not difficult." 3x(P(x) 1\ Q(x» is true. By translating these assumptions into statements involv­ ing propositional variables and logical connectives, deter­ 1. 3x P(x) 1\ 3x Q(x) Premise 2. 3x P(x) Simplification from (1) mine whether each of the fo llowing are valid conclusions 3. Pee) Existential instantiation from(2) of these assumptions: 4. 3x Q(x) Simplificationfrom (1) a) That mathematics is not easy, if many students like 5. Q(e) Existential instantiation from (4) logic. 6. Pee) 1\ Q(e) Conjunctionfrom (3) and (5) b) That not many students like logic, if mathematics is 3x(P(x) 1\ Q(x» Existential generalization not easy. 24. Identify7. the error or errors in this argument that sup­ c) That mathematics is not easy or logic is difficult. posedly shows that if Vx (P(x) v Q(x» is true then That logic is not difficultor mathematics is not easy. Vx P(x) v Vx Q(x) is true. e)d) That if not many students like logic, then either math­ ematics is not easy or logic is not difficult. 1. Vx (P(x) v Q(x» Premise 2. Pee) v Q(e) Universal instantiation from (1) *35. Detenninewhether this argument,taken fromKalish and 3. Pee) Simplification from (2) Montague [KaM064], is valid. 4. Vx P(x) Universal generalization from (3) If Superman were able and willing to prevent evil, 5. Q(e) Simplificationfrom (2) he would do so. If Supennanwere unable to prevent 6. Vx Q(x) Universal generalization from (5) evil, he would be impotent; if he were unwilling Vx(P(x) v Vx Q(x» Conjunction from (4) and (6) to prevent evil, he would be malevolent. Supennan 25. Justify7. the rule ofuniversal modus tollens by showing that does not prevent evil. If Supennan exists, he is nei­ the premises Vx(P(x) � Q(x» and --.Q(a) for a partic­ ther impotent nor malevolent. Therefore, Supennan ular element a in the domain, imply --.P(a). does not exist. /-85 1.6 Introduction to Proofs 85

Chapter 4, including mathematical induction, which can be used to prove results that hold fo r all positive integers. In Chapter 5 we will introduce the notion of combinatorial proofs. In this section we introduced several methods for proving theorems of the form Vx(P(x) --+ Q(x )), including direct proofs and proofs by contraposition. There are many theorems of this type whose proofs are easy to construct by directly working through the hypotheses and definitions of the terms of the theorem. However, it is oftendifficult to prove a theorem without resorting to a clever use of a proof by contraposition or a proof by contradiction, or some other proof technique. In Section 1.7 we will address proof strategy. We will describe various approaches that can be used to find proofs when straightforward approaches do not work. Constructing proofs is an art that can be learned only through experience, including writing proofs, having your proofs critiqued, and reading and analyzing other proofs.

Exercises

1. Use a direct proofto show that the sum of two odd integers a) a proof by contraposition. is even. b) a proof by contradiction. 2. Use a direct proof to show that the sum oftwo even inte­ 19. Prove the proposition P(O), where P(n) is the proposition 2 gers is even. "If n is a positive integer greater than I, then n > n." 3. Show that the square of an even number is an even number What kind of proof did you use? using a direct proof. 20. Prove the proposition P(I), where pen) is the proposi­ 2 4. Show that the additive inverse, or negative, of an even tion "If n is a positive integer, then n :::: n." What kind of number is an even number using a direct proof. proof did you use? 5. Prove that if m + n and n + p are even integers, where 21. Let pen) be the proposition "If a and b are positive real numbers, then b)n n bn ." Prove that P (I) is m, n, and p are integers, then m + p is even. What kind (a + :::: a + of proof did you use? true. What kind of proof did you use? 6. Use a direct proof to show that the product of two odd 22. Show that if you pick three socks froma drawer contain­ numbers is odd. ing just blue socks and black socks, you must get either a 7. Use a direct proof to show that every odd integer is the pair of blue socks or a pair of black socks. difference of two squares. 23. Show that at least 10 of any 64 days chosen must fall on 8. Prove that if n is a perfect square, then n + 2 is not a the same day of the week. perfect square. 24. Show that at least 3 of any 25 days chosen must fall in the 9. Use a proof by contradiction to prove that the sum of an same month of the year. irrational number and a rational number is irrational. 25. Use a proofby contradiction to show that there is no ratio­ 3 10. Use a direct proof to show that the product of two rational nal number r for which r + r + = O. [Hint: Assume numbers is rational. that r = a / b is a root, where a and Ib are integers and a / b is in lowest terms. Obtain an equation involving integers 11. Prove or disprove that the product of two irrational num­ by multiplying by b3 Then look at whether and b are bers is irrational. • a each odd or even.] 12. Prove or disprove that the product of a nonzero rational 26. Prove that if n is a positive integer, then n is even if and number and an irrational number is irrational. only if n + 4 is even. 13. Prove that if x is irrational, then 1/x is irrational. 27. Prove that7 if n is a positive integer, then n is odd if and x x =1= I/x 14. Prove that if is rational and 0, then is rational. only if 5n + 6 is odd. 15. Use a proof by contraposition to show that if x + y :::: 2, 2 2 28. Prove that m = n if and only if m = n or m = -n. where x and y are real numbers, then x :::: I or y :::: I . 29. Prove or disprove that if m and n are integers such that S'16. Prove that if m and n are integers and m n is even, then m mn = 1, then either m = I and n = I, or else m = -I is even or n is even. and n = -1. 3 17. Show that if n is an integer and n + 5 is odd, then n is 30. Show that these three statements are equivalent, where even using a and b are real numbers: (i) a is less than b, (ii) the a) a proof by contraposition. average of a and b is greater than a, and (iii) the average b) a proof by contradiction. of a and b is less than b. 18. Prove that if n is an integer and 3n + 2 is even, then n is 31. Show that these statements about the integer x are equiv­ even using alent: (i) 3x + 2 is even, (ii) x + 5 is odd, (iii) x2 is even. 86 1 / The Foundations: Logic and Proofs 1-86

32. Show that these statements about the real number x are to be equivalent by showing that PI ++ P4, P2 ++ P3 , and equivalent: (i) x is rational, (ii) x /2 is rational, and (iii) PI ++ P3 · 3x - is rational. 1 37. Show that the propositions PI, P2 , P3 , P4, and P5 can be 33. Show that these statements about the real number x are shown to be equivalent by proving that the conditional equivalent: (i) x is irrational, (ii) 3x + 2 is irrational, (iii) statements PI -+ P4, P3 -+ PI . P4 -+ P2, P2 -+ P5, and x /2 is irrational. P5 -+ P3 are true. 34. Is this reasoning for findingthe solutions of the equation 38. Find a counterexample to the statement that every positive .J2x2 = x .J2x2 = x integer can be written as the sum of the squares of three - 1 correct? (1) - 1 is given; (2) 2X2 - 1 = x2, obtained by squaring both sides of (1); integers. 2 2 (3) x - 1 = 0, obtained by subtracting x from bothsides of 39. Prove that at least one of the real numbers aI. a2 , ..., an (2); (x )(x = (4) - 1 + 1) 0, obtained by factoring the left­ is greater than or equal to the average of these numbers. x2 1; x = x = hand side of - (5) 1 or -1, which fo llows What kind of proof did you use? because ab = 0 implies that a = 0 or b = O. 40. Use Exercise to show that if the first 10 positive inte­ 35. Are these steps for finding the solutions of .Jx + = gers are placed39 around a circle, in any order, there exist - x correct? .Jx = - x x three integers in consecutive locations around the circle (1) + is given; (2) 3 + 3 = x2 - 6x + obtained3 by 3 squaring both sides of that have a sum greater than or equal to 17. = x2 - 7x x (I); (3) 0 9, + 6, obtained by subtracting + 41. Prove that if is an integer, these fo ur statements are (2); = (x - 1)(x from both sides of (4) 0 - 6), equivalent: (i)n is even, (ii) + 1 is odd, (iii) + 1 is obtained3 by factoring the right-hand side of odd, (iv) is even.n n 3n x = x = (5) 1 or 6, which fo llows from (4) because(3); 42. Prove that3n these four statements about the integer are ab = 0 implies that a = 0 or b = O. equivalent: (i) n2 is odd, (ii) 1 - is even, (iii) n3 isn odd, 36. Show that the propositions PI,P 2, P3 , and P4 can be shown (iv) n2 + 1 is even. n

1.7 Proof Methods and Strategy

Introduction

In Section 1.6 we introduced a variety of differentmethods of proof and illustrated how each Assessment � method is used. In this section we continue this effort.We will introduce several other important proof methods, including proofs where we consider differentcases separately and proofs where we prove the existence of objects with desired properties. In Section 1.6 we only briefly discussed the strategy behind constructing proofs. This strategy includes selecting a proof method and then successfullyconstructing an argument step by step, based on this method. In this section, afterwe have developed a wider arsenal of proof methods, we will study some additional aspects of the art and science of proofs. We will provide advice on how to finda proof of a theorem. We will describe some tricks of the trade, including how proofs can be found by working backward and by adapting existing proofs. When mathematicians work, they formulate conjectures and attempt to prove or disprove them. We will briefly describe this process here by proving results about tiling checkerboards with dominoes and other types of pieces. Looking at tilings of this kind, we will be able to quickly fo rmulate conjectures and prove theorems without first developing atheory. We will conclude the section by discussing the role of open questions. In particular, we will discuss some interesting problems either that have been solved after remaining open fo r hundreds of years or that still remain open.

Exhaustive Proof and Proof by Cases

Sometimes we cannot prove a theorem using a single argument that holds for all possible cases. We now introduce a method that can be used to prove a theorem, by considering different cases 102 I / The Foundations: Logic and Proofs 1-102

The 3x + 1 conjecture has an interesting history and has attracted the attention of math­ ematicians since the 1950s. The conjecture has been raised many times and goes by many other names, including the Collatz problem, Hasse's algorithm, Ulam's problem, the Syracuse problem, and Kakutani's problem. Many mathematicians have been diverted from their work to spend time attacking this conjecture. This led to the joke that this problem was part of a conspiracy to slow down American mathematical research. See the article by Jeffrey Lagarias [La85] for a fascinating discussion of this problem and the results that have been found by mathematicians attacking it. ....

In Chapter 3 we will describe additional open questions about prime numbers. Students already familiar with the basic notions about primes might want to explore Section 3.4, where these open questions are discussed. We will mention other important open questions throughout the b09k.

Additional Proof Methods

In this chapter we introduced the basic methods used in proofs. We also described how to leverage these methods to prove a variety of results. We will use these proof methods in Chapters 2 and 3 to prove results about sets, functions, algorithms, and number theory. Among the theorems we will prove is the famous halting theorem which states that there is a problem that cannot be solved using any procedure. However, there are many important proof methods besides those we have covered. We will introduce some of these methods later in this book. In particular, in Section 4.1 we will discuss mathematical induction, which is an extremely usefulmethod for proving statements of the form Vn P(n), where the domain consists of all positive integers. In Section 4.3 we will introduce structural induction, which can be used to prove results about recursively defined sets. We will use the Cantor diagonalization method, which can be used to prove results about the size of infinite sets, in Section 2.4. In Chapter 5 we will introduce the notion of combinatorial proofs, which can be used to prove results by counting arguments. The reader should note that entire books have been devoted to the activities discussed in this section, including many excellent works by George P6lya ([P061], [P07 1], [P090]). Finally, note that we have not given a procedure that can be used fo r proving theorems in mathematics. It is a deep theorem of mathematical logic that there is no such procedure.

Exercises

2 1. Prove that n + I ::: n when n is a positive integer with 6. Prove that there is a positive integer that equals the sum 1 :::: n :::: 2 of the positive integers not exceeding it. Is your proof 2. Prove that4. there are no positive perfect cubes less 1000 constructive or nonconstructive? that are the sum ofthe cubes of two positive integers. 7. Prove that there are 100 consecutive positive integers that 3. Prove that if x and y are real numbers, then max(x , y) + are not perfect squares. Is your proof constructive or non­ min(x , y) = x + y. [Hint: Use a proof by cases, with constructive? 500 500 the two cases corresponding to x ::: y and x < y, 8. Prove that either 10 + 1 5 or 10 + 16 is respectively. ] not a perfect square.2· Is your proof2· constructive or 4. Use a proof by cases to show that min(a, min(b, c» = nonconstructive? min(min(a, b), c) whenever a, b, and c are real numbers. 9. Prove that there exists a pair of consecutive integers such 5. Prove the triangle inequality, which states that ifx and y that one of these integers is a perfect square and the other are real numbers, then Ixl + Iyl ::: Ix + yl (where Ixl rep­ is a perfect cube. resents the absolute value of x, which equals x if x ::: 0 10. Show that the product of two of the numbers 65 1000 - 2001 + 177 1212 _ 2001 - 8192 and equals -x if x < 0). 8 3 , 79 92399 + 2 , and 244493 5 + 1-103 1.7 Proof Methods and Strategy 103

71777 is nonnegative. Is your proof constructive or noncon­ 25. Formulate a conjecture about the decimal digits that ap­ structive? Do not try to evaluate these numbers!] pear as the final decimal digit of the fourth power of an 11. Prove or disprove[Hint: that there is a rational number x andan integer. Prove your conjecture using a proof by cases. irrational number y such that xY is irrational. 26. Formulate a conjecture about the final two decimaldigits 12. Prove or disprove that if a and b are rational numbers, of the square of an integer. Prove your conjecture using a then ab is also rational. proof by cases. 13. Show that each of these statements can be used to ex­ 27. Prove that there is no positive integer such that 2 + press the fact that there is a unique element x such that = too. n n P(x) is true. [Note that we can also write this statement 28. Proven3 that there are no solutions in integers x and y to the as 3!xP(x).] equation 2x2 + 5y2 = 14. a) 3xVy(P(y) ++x = y) 29. Prove that there are no solutions in positive integers x and b) 3x P(x) /\ VxVy(P(x) /\ P(y) -+ X = y) y to the equation x4 + y4 = 625. c) 3x(P(x) /\ Vy(P(y)-+ x = y» 30. Prove that there are infinitely many solutions in posi­ 2 2 2 14. Show that if a, b, and c are real numbers and a =I 0, then tive integers x, y, and z to the equation x + y = z . Let x m2 - n2, y and z m2 + 2, there is a unique solution of the equation ax + b = c. = = = where[Hint: m and are integers.] 2mn, n 15. Suppose that a and b are odd integers with a =I b. Show 31. Adapt the proofn in Example in Section 1.6 to prove that there is a unique integer c such that la - cl = Ib - cl. if abc, where a, b, and 4c are positive integers, then 16. r = Show that if is an irrational number, there is a unique a �n �, b � �, or c ��. integer such that the distance between r and is less than 1/2.n n 32. Prove that � is irrational. 33. Prove that between every two rational numbers there is an 17. Show that if is an odd integer, then there is a unique integer suchn that is the sum of - 2 and + 3. irrational number. k k k 34. Prove that between every rational number and every irra­ 18. Prove that given a realn number x there exist unique num­ tional number there is an irrational number. bers and E such that x + E, is an integer, and = ... O�E

43. Use a proof by exhaustion to show that a tiling using dominoes of a 4 x 4 checkerboard with opposite cor­ ners removed does not exist. First show that you can assume that the squares in[H theint: upper left and lower right corners are removed. Number the squares of the original checkerboard from 1 to 16, starting in the first row, moving right in this row, then starting in the left­ most square in the second row and moving right, and so on. Remove squares 1 and 16. To begin the proof, note that square is covered either by a domino laid horizontally, which2 covers squares and 3, or vertically, which covers squares and 6. Consider2 each of these cases separately, and work2 through all the subcases that arise.] *44. Prove that when a white square and a black square are removed from an 8 x 8 checkerboard (colored as in the text) you can tile the remaining squares of the checkerboard using dominoes. Show that when one black and one white square [Hareint: removed, each part Figure for Exercise 44. of the partition of the remaining cells formed by insert­ ing the barriers shown in the figure can be covered by arguments based on coloring and rotations to eliminate dominoes.] as many squares as possible from consideration.] 45. Show that by removing two white squares and two black *4 7. a) Draw each of the fivedifferent tetrominoes, where a squares from an 8 x 8 checkerboard (colored as in the tetromino is a polyomino consisting of four squares. text) you can make it impossible to tile the remaining b) For each of the fivediff erent tetrominoes, prove or squares using dominoes. disprove that you can tile a standard checkerboard *4 6. Find all squares, ifthey exist, on an 8 x 8 checkerboard so using these tetrominoes. that the board obtained by removing one of these square *48. Prove or disprove that you can tile a lO x 10 checkerboard can be tiled using straight triominoes. [Hint: First use using straight tetrominoes.

Key Te rms and Results

TERMS inverse ofp - q: the conditional statement ""'p � ""'q " " p � (biconditional): the proposition if and only if , proposition: a statement that is true or false q p q which is true if and only if and have the same truth propositional variable: a variable that represents a proposition p q value truth value: true or false ....,(negation p ofp) : the proposition with truth value opposite bit: either a 0 or a 1 Boolean variable: a variable that has a value of 0 or 1 to the truth value of p logical operators: operators used to combine propositions bit operation: an operation on a bit or bits compound proposition: a proposition constructed by combin­ bit string: a list of bits ing propositions using logical operators bitwise operations: operations on bit strings that operate on truth table: a table displaying the truth values of propositions each bit in one string and the corresponding bit in the other " " string p V q (disjunction ofp and q): the proposition p or q , which is true if and only if at least one of p and q is true tautology: a compound proposition that is always true " " contradiction: a compound proposition that is always p /\ q (conjunction of p and q): the proposition p and q which is true if and only ifboth p and q are true false " " contingency: a compound proposition that is sometimes true p EB q (exclusive or of p and q): the proposition p XOR q which is true when exactly one of p and q is true and sometimes false " compound propositions p - q (p implies q): the proposition "if p, then q , which is consistent compound propositions: false if and only if p is true and q is false for which there is an assignment of truth values to the vari­ ables that makes all these propositions true converse ofp _ q: the conditional statement q � p contrapositive of p _ q: the conditional statement logically equivalent compound propositions: compound ""'q �""'p propositions that always have the same truth values The Foundations: Logic and Proofs 1-106 106 1 I

b) Describe the different ways to show that two com­ 10. a) Describe what is meant by a direct proof, a proof by pound propositions are logically equivalent. contraposition, and a proof by contradiction ofa con­ c) Show in at least two different waysthat the compound ditional statement P -+ q. propositions �P v (r -+ �q) and �P v �q V �r are b) Give a direct proof, a proof by contraposition and a equivalent. proof by contradiction of the statement: "If n is even, 5. then n + 4 is even." (D epends on the Exercise Set in Section 1.2) 11. a) Given a truth table, explain how to use disjunctive nor­ a) Describe a way to prove the biconditional P # q. mal fo rm to construct a compound proposition with b) Prove the statement: "The integer 3n + 2 is odd if this truth table. and only if the integer 9n + 5 is even, where n is an b) Explain why part (a) shows that the operators /\, v, integer." and � are functionally complete. 12. To prove that the statements PI , P2, P3, and P4 are c) Is there an operator such that the set containing just equivalent, is it sufficient to show that the conditional this operator is functionally complete? statements P4 -+ P2, P3 -+ PI, and PI -+ P2 are valid? 6. What are the universal and existential quantifications of If not, provide another set of conditional statements a predicate P(x)? What are their negations? that can be used to show that the four statements are 7. a) What is the difference between the quantification equivalent. 13. 3xVyP(x, y) and Vy3xP(x,y), where P(x,y) is a a) Suppose that a statement ofthe fo rm Vx P(x) is false. predicate? How can this be proved? b) Show that the statement "For every positive integer n, b) Give an example of a predicate P (x , y) such that 2 3xVyP(x, y) and Vy3xP(x, y) have different truth n ::: 2n" is fa lse. values. 14. What is the difference between a constructive and 8. Describe what is meant by a valid argument in proposi­ nonconstructive existence proof? Give an example of tional logic and show that the argument "If the earth is each. flat, then you can sail offthe edge of the earth," "You can­ 15. What are the elements of a proof that there is a unique not sail offthe edge of the earth," therefore, "The earth is element x such that P(x), where P(x) is a propositional not flat" is a valid argument. function? 9. Use rules of inference to show that if the premises "All 16. Explain how a proof by cases can be used to prove a result zebras have stripes" and "Mark is a zebra" are true, then about absolute values, such as the fact that Ixyl = Ixllyl the conclusion "Mark has stripes" is true. fo r all real numbers x and y.

Supplementary Exercises

1. Let P be the proposition "I will do every exercise in this 5. Given a conditional statement P -+ q, find the converse book" and q be the proposition "I will get an ''I>:' in this of its inverse, the converse of its converse, and the con­ course." Express each of these as a combination of P verse of its contrapositive. and q. 6. Given a conditional statement p -+ q, findthe inverse of a) I will get an "Pl' in this course only if! do every exer­ its inverse, the inverse of its converse, and the inverse of cise in this book. its contrapositive. b) I will get an "Pl' in this course and I will do every 7. Find a compound proposition involving the propositional exercise in this book: variables p, q, r, and s that is true when exactly three c) Either I will not get an "Pl'in this course or I will not of these propositional variables are true and is false do every exercise in this book. otherwise. For me to get an "A" in this course it is necessary and d) sufficientthat I do every exercise in this book. 8. Show that these statements are inconsistent: "If Sergei takes the job offerthen he will get a signing bonus." "If 2. Find the truth table of the compound proposition (p v q) -+ (p /\ �r). Sergei takes the job offer, then he will receive a higher salary." "If Sergei gets a signing bonus, then he will not 3. Show that these compound propositions are tautologies. receive a higher salary." "Sergei takes the job offer." a (�q /\ (p -+ q» -+ �P ) 9. Show that these statements are inconsistent: "If Miranda b) «pvq)/\�p) -+ q does not take a course in discrete mathematics, then she 4. Give the converse, the contrapositive, and the inverse of will not graduate." "If Miranda does not graduate, then these conditional statements. she is not qualified for the job." "If Miranda reads this a) If it rains today, then I will drive to work. book, then she is qualified for the job." "Miranda does b) If lxl = x, then x ::: O. not take a course in discrete mathematics but she reads 2 this book." c) If n is greater than 3, then n is greater than 9. I-I 1!7 Supplementary Exercises 107

10. Suppose that you meet three people, A, B, and C, on the ing the propositional function G(x , y), which represents island of knights and knaves described in Example 18 in "x is the grandmother of y." Section 1.1. What are A, B, and C if A says "I am a knave 19. Use existential and universal quantifiers to express the and B is a knight" and B says "Exactly one of the three statement "Everyone has exactly two biological parents" of us is a knight"? using the propositional function P(x , y), which repre­ 11. (Adapted from [Sm78]) Suppose that on an island there sents "x is the biological parent of y." are three types of people, knights, knaves, and normals. 20. The quantifier 3n denotes "there exists exactly n ," so that Knights always tell the truth, knaves always lie, and nor­ 3nx P (x) means there exist exactly n values in the do­ mals sometimes lie and sometimes tell the truth. Detec­ main such that P(x) is true. Determine the true value tives questioned three inhabitants of the island-Amy, of these statement where the domain consists of all real Brenda, and Claire-as part of the investigation of a numbers. crime. The detectives knew that one ofthe three commit­ z a) 30x(x = -1) b) 3\x(lxl = 0) ted the crime, but not which one. They also knew that the z criminal was a knight, and that the other two were not. Ad­ c) 3zx(x = 2) d) 33x(x = Ix I) ditionally, the detectives recorded these statements: Amy: 21. Express each of these statements using existential and "I am innocent." Brenda: "What Amy says is true." Claire: universal quantifiers andpropositional logic where 3n is "Brenda is not a normal." Afteranalyzing their informa­ definedin Exercise 20. tion, the detectives positively identified the guilty party. a) 30xP(x) b) 3\xP(x) Who was it? c) 3zxP(x) d) 33X P(X) 12. Show that if is a proposition, where is the condi­ 22. Let P(x, y) be a propositional function. Show that tional statementS "If is true, then unicornsS live," then 3x Vy P(x , y) --+ Vy 3x P(x, y) is a tautology. "Unicorns live" is true.S Show that it fo llows that can­ 23. Let P(x) and Q(x) be propositional functions. Show that not be a proposition. (This paradox is known asS Lob s 3x (P(x) --+ Q(x» and "Ix P(x) --+ 3x Q(x) always have paradox.) the same truth value. 13. Show that the argument with premises "The tooth fairy is 24. If Vy 3x P(x , y) is true, does it necessarily fo llow that a real person" and "The truth fairy is not a real person" and 3x Vy P(x , y) is true? conclusion "You can find gold at the end of the rainbow" 25. If "Ix 3y P(x, y) is true, does it necessarily follow that is a valid argument. Does this show that the conclusion is 3x Vy P(x , y) is true? true? 26. 14. Let P(x) be the statement "student x knowscalculus" and Find the negations of these statements. let Q(y) be the statement "class y contains a student who a) If it snows today, then I will go skiing tomorrow. knows calculus." Express each ofthese as quantifications b) Every person in this class understands mathematical of P(x) and Q(y). induction. c) Some students in this class do not like discrete math­ a) Some students know calculus. ematics. b) Not every student knows calculus. d) In every mathematics class there is some student who c) Every class has a student in it who knows calculus. falls asleep during lectures. d) Every student in every class knows calculus. 27. Express this statement using quantifiers: "Every student e) There is at least one class with no students who know in this class has taken some course in every department calculus. in the school of mathematical sciences." 15. Let P(m , n) be the statement "m divides n," where the 28. Express this statement using quantifiers:"There is a build­ domain for both variables consists of all positive inte­ ing on the campus of some college in the United States in gers. (By "m divides n" we mean that n = for some which every room is painted white." integer Determine the truth values of eachkm of these 29. Express the statement "There is exactly one student in this statements.k. ) class who has taken exactly one mathematics class at this a) P(4, 5) b) P(2, 4) school" using the uniqueness quantifier.Then express this c) "1m "InP( m , n) d) 3m "In P(m, n) statement using quantifiers, withoutusing the uniqueness e) 3n "1mP( m , n) 1) Vn P(I, n) quantifier. 16. Find a domain for the quantifiers in 3x3y(x =1= y 1\ 30. Describe a rule of inference that can be used to prove that Vz«z =1= x) 1\ (z =1= y» such that this statement is there are exactly two elements x and y in a domain such true. that P(x) and P(y) are true. Express this rule of inference 17. Find a domain for the quantifiers in 3x3y(x =1= y 1\ as a statement in English. Vz« z = x) V (z = y» ) such that this statement is 31. Use rules of inference to show that if the premises false. Vx(P(x) --+ Q(x» , Vx(Q(x) --+ R(x» , and -.R(a), 18. Use existential and universal quantifiers to express the where a is in the domain, are true, then the conclusion statement "No one has more than three grandmothers" us- -'P(a) is true. 108 1/The Foundations: Logic and Proofs /-/08

32. Prove that if 3 is irrational, then is irrational. 37. Disprove the statement that every positive integer is the 33. Prove that ifx is irrational and x � 0, then .jX is irra­ sum of the cubes of eight nonnegative integers. tional. x x 38. Disprove the statement that every positive integer is the 34. Prove that given a nonnegative integer there is a unique sum of at most two squares and a cube of nonnegative nonnegative integer such that � n, < + integers. 2 2 1000 35. Prove that there existsm an integer msuchn that (m > 110) . . 39. Is your proof constructive or nonconstructive?m m 2 Disprove the statement that every positive integer is the sum of 36 fifthpowers of nonnegative integers. 36. Prove that there is a positive integer that can be written· as the sum of squares of positive integers in two differ­ 40. Assuming the truth of the theorem that states that ...;nis ent ways. (Use a computer or calculator to speed up your irrational whenever is a positive integer that is not a work.) perfect square, proven that ..fi+ .../3is irrational.

Computer Projects

Write programs with the specified input and output.

1. Given the truth values of the propositions p and q, findthe junction of p and q (see Exercises 40 and 41 of Section truth values of the conjunction, disjunction, exclusive or, 1.1). conditional statement, and biconditional ofthese proposi­ *4. Given positive integers and interactively play the game tions. of Chomp. m n, 2. Given two bit strings of length find the bitwise AND, * 5. Given a portion of a checkerboard, look for tilings of this bitwise OR, and bitwise XOR of n,these strings. checkerboard with various types ofpol yomi noes, including 3. Given the truth values of the propositions p and q in fuzzy dominoes, the two types oftriominoes, and larger polyomi­ logic, find the truth value of the disjunction and the con- noes.

Computations and Explorations

Use a computational program or programs you have written to do these exercises.

1. Look for positive integers that are not the sum of the cubes ten as the sum of cubes of positive integers, in two different of nine different positive integers. ways, sharing this property with 1729. *4. Try to findwinning strategies for the game of Chomp for 2. Look for positive integers greater than 79 that are not the different initial configurations of cookies. sum of the fourth powers of 18 positive integers. *5. Look for tilings of checkerboards and parts of checker­ 3. Find as many positive integers as you can that can be writ- boards with polynominoes.

Writing Proj ects

Respond to these with essays using outside sources.

1. Discuss logical paradoxes, including the paradox of Epi­ 4. Read some of the writings of Lewis Carroll on symbolic menides the Cretan, Jourdain's card paradox, and the bar­ logic. Describe in detail some of the models he used to ber paradox, and how they are resolved. represent logical arguments and the rules of inference he 2. Describe how fuzzy logicis being applied to practical ap­ used in these arguments. plications. Consult one or more of the recent books on 5. Extend the discussion of Prolog given in Section 1.3, ex­ fuzzy logicwritten for general audiences. plaining in more depth how Prolog employs resolution.

3. Describe the basic rules of WFF 'N PROOF, 6. Discuss some of the techniques used in computational developed by Layman Allen.The GameGive logic, including Skolem's rule. ofexamples Modern of Logic, some of the games included in WFF 'N 7. ''Automatedtheorem proving" is the task of using com­ PROOF. puters to mechanically prove theorems. Discuss the goals 146 2/ Basic Structures: Sets, Functions, Sequences, and Sums 2-36

Example 29 illustrates that the factorial function grows extremely rapidly as n grows. The rapid growth of the factorial function is made clearer by Stirling's fo rmula, a result from higher mathematics that tell us that n! '" J2rrn(n/et . Here, we have used the notation f(n) '" g(n), which means that the ratio f(n)/g(n) approaches 1 as n grows without bound (that is, limn-4oof(n )/g(n ) = 1). The symbol '" is read "is asymptotic to." Stirling's formula is named after James Stirling, a Scottish mathematician of the eighteenth century.

Exercises

1. Why is not a functionfrom R to R if c) the functionthat assigns to a bit string the number of a) f= ones minus the number of zeros in the string f(x) .JX1/x?? d) the function that assigns to each positive integer the largest integer not exceeding the square root of the b)c) f(x) = ±J(x,.--,,..-2 -I)? integer 2. Determinef(x) whether + is a function fromZ to R if f e) the function that assigns to a bit string the longest a) = ± . string of ones in the string f(n) J1i2+1n . 7. Find the domain and range of these functions. b)c) f(n) = - 4). a the functionthat assigns to each pair of positive inte­ 3. Determinef(n) whether1/(n2 is a functionfr om the set of all bit ) gers the maximum of these two integers strings to the set of integersf if the function that assigns to each positive integer the a) is the position of a 0 bit in b) number of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 that do f(S) is the number of 1 bits in S. not appear as decimal digits of the integer c)b) f(S) is the smallest integer i suchS. that the ith bit of c) the functionthat assigns to a bit string the number of f(SSis ) 1 and 0 when is the empty string, the times the block 11 appears string with f(Sno bits.) = S d) the function that assigns to a bit string the numerical 4. Find the domain and range of these functions. Note that position of the first 1 in the string and that assigns the in each case, to findthe domain, determine the set of ele­ value 0 to a bit string consisting of all Os ments assigned values by the function. 8. Find these values. a) the functionthat assigns to each nonnegative integer a its last digit ) c) Ll.1J b)d) r-01.11· 1 1 the function that assigns the next largest integer to a e) rL 2.-O.99l1J 1) r -2.991 b) positive integer g) h) r 1 1 c) the function that assigns to a bit string the number of L! + r!lJ r L! J + r! + ! one bits in the string 9. Find these values. d) the function that assigns to a bit string the number of a ) bits in the string c) r�l b)d) L�J 5. Find the domain and range of these functions. Note that e) r3-1 �l 1) L-�J in each case, to findthe domain, determine the set of ele­ g) 1 h) L -lJ ments assigned values by the function. 10. DetermineL! + r � whetherJ each of L!these' L � J Jfunctions from a) the functionthat assigns to each bit string the number d} to itself is one-to-one. of ones minus the number of zeros a){a , b, c, d the function that assigns to each bit string twice the f(a) = b, f(b) = a, f(c) = d,c, f(d) = b) number of zeros in that string cb) f(a) = d,b, f(b) = b, f(c) = f(d) = dc c) the function that assigns the number of bits left over ) 11. Whichf(a functions) = f(b in) =Exercise b, f(c )10 = are c, f(donto?) = when a bit string is split into bytes (which are blocks of 8 bits) 12. Determine whether each of these functions from Z to Z d) the functionthat assigns to each positive integer the is one-to-one. largest perfect square not exceeding this integer a) = - 1 2 1 6. Find the domain and range of these functions. c) f(n) n b)d) f(n) = n 1+2 1 13. Whichf(n functions) = n3 in Exercise 12 f(nare onto?) = rn a) the function that assigns to each pair of positive inte­ 14. Z Z Z gers the firstinteger of the pair Determine whether f: x � is onto if the function that assigns to each positive integer its a) b) largest decimal digit f(m,n 2m n.2 • b) f(m, n)=) = m2 -n 2-3 7 2.3 Functions 147

c) = + + 1. 26. Let S = {-I, 0, 2, 4, 7}. Find f(S) if d) f(m, n) = m n a) f(x) = 1. b) f(x) = 2x + 1. e) f(m, n) = Iml-- 4.In l. c) f(x) = fx /5l d) f(x) = L(x2 + I)/3J. 15. f(m, n) m2 Detenninewhether the functionf: Z x Z � Z is onto if 27. Let f(x) = Lx2/3J. Find f(S) if a) = + a) S = { 2, -1, 0, 1 ,2, 3}. b) f(m, n) = m +n . b) S = {O-, 1 , 2, 3,4, 5}. c) f(m, n) = m2 n2• c) S = {l, 5, 7, ll}. d) f(m, n) = m. d) S = {2, 6, 10, 14}. e) f(f(m , n)=) Inl.- . 28. Let f(x) = 2x. What is 16. Give anm examplen m of na function from N to N that is a) f(Z)? b) f(N)? c) f(R)? a) one-to-one but not onto. 29. Suppose that g is a functionfrom A to B and f is a func­ onto but not one-to-one. b) tion from B to C. c) both onto and one-to-one (but differentfrom the iden­ tity function). a) Show that ifboth f and g are one-to-one functions, d) neither one-to-one nor onto. then fog is also one-to-one. b) Show that if both f and g are onto functions, then 17. Give an explicit fonnula for a function from the set of fog is also onto. integers to the set of positive integers that is *30. If f and fog are one-to-one, does it fo llow that g is a) one-to-one, but not onto. one-to-one? Justifyyour answer. b) onto, but not one-to-one. c) one-to-one and onto. *31. If f and fog are onto, does it fo llow that g is onto? d) neither one-to-one nor onto. Justifyyour answer. 32. 2 18. Detenninewhether each of these functions is a bijection Find fog and g 0 f, where f(x) = x + 1 and g(x) = from Rto R. x + 2, are functions from R to R. 33. a) f(x) = -3x +4 Find f + g and fg for the functions f and g given in Exercise 32. b) f(x) = -3x2 + 7 c) f(x) = (x + I)/(x + 2) 34. Let f(x) = x + bandg(x) = ex + where b, e, and d) f(x) = x5 + I are constants.a Detennine for whichd, constantsa, b, e, 19. Detennine whether each of these functions is a bijection dand it is true that fog = g 0 f. a, from R to R. 35. Showd that the function f(x) = x + b from R to R is a) f(x) = 2x + 1 invertible, where and b are constants,a with #- 0, and findthe inverse of f. b) f(x) = x2 + 1 a a c) f(x) = x3 36. Let f be a functionfrom the setA to the set Let S and d) f(x) = (x2 + I)/(x2 + 2) T be subsets of A. Show that B.

20. Let f: R � R and let f(x) > 0 for all x E R. Show a) f(S U T) = f(S) U f(T). that f(x) is strictly increasing if and only if the function b) f(S n T) c;:: f(S) n f(T). g(x) = 1/f(x ) is strictly decreasing. 37. Give an example to show that the inclusion in part (b) in 21. Let f: R � Rand let f(x) > O. Show that f(x)is strictly Exercise 36 may be proper. decreasing if and only if the function g(x) = 1/f(x ) is Let f be a function from the set A to the set B. Let S be a strictly increasing. subset of B. We definethe inverse image of S to be the subset 22. Qive an example of an increasing function with the set of A whose elements are precisely all pre-images of all ele­ I of real numbers as its domain and codomain that is not ments of S. We denote the inverse image of S by f- (S), so I I one-to-one. f- (S) = { E A f( ) E S}. The notation f- is used in two adifferent I wa ays. Do not(Beware: confuse the notation intro­ 23. Give an example of a decreasing function with the set duced here with the notation f-I for the value at of the of real numbers as its domain and codomain that is not y inverse of the invertible function f.(y) Notice also that f-I (S), one-to-one. the inverse image of the set S, makes sense for all functionsf, 24. Show that the function f(x) = from the set of real not just invertible functions.) number to the set of real numberseX is not invertible, but 38. Let f be the functionfrom R to R definedby if the codomain is restricted to the set of positive real f(x) = x2• Find numbers, the resulting function is invertible. a) f-I ({I }). b) f- I ({x 10< x < I}). 25. Show that the function f(x) = x I from the set of real c) f-I ({x x 4}) numbers to the set of nonnegativeI real numbers is not I > . invertible, but if the domain is restricted to the set 39. Let g(x) = LxJ. Find of nonnegative real numbers, the resulting function is a) g-I ({O}). b) g-I ({-I , O, I}). invertible. c) g-I ({X 10< x < I}). 148 2 / Basic Structures: Sets, Functions, Sequences, and Sums 2-38

40. Let f be a functionfrom A to B. Let S and T be subsets 57. Data are transmitted over a particular Ethernet network of B. Show that in blocks of 1500 octets (blocks of 8 bits). How many

a) f- I (S U T) = f-I (S) U f- I (T). blocks are required to transmit the fo llowing amounts of data over this Ethernetnetwork? (Note that a byte is a syn­ b) f-I (S n T) = f-I (S) n f- I (T). onym for an octet, a kilobyte is 1000 bytes, and a megabyte 41. Let f be a functionfr om A to B. Let S be a subset of B. is 1,000,000 bytes.) Show that f-I = f-I (S). (:5) a) 150 kilobytes of data 42. Show that Lx + � J is the closest integer to the number x, b) 384 kilobytes of data except when x is midway between two integers, when it c) 1.544 megabytes of data is the larger of these two integers. d) 45.3 megabytes of data 43. Show that fx - � 1 is the closest integer to the number x, 58. Draw the graph of the functionf en) = 1 - n2 from Z to except when x is midway between two integers, when it z. is the smaller of these two integers. 59. Draw the graph of the function f(x) = L2xJ from R to 44. Show that if x is a real number, then fxl - LxJ = I R. if x is not an integer and fxl - LxJ = 0 if x is an 60. Draw the graph of the function f(x) = Lx /2J from R to integer. R. 45. Show that if x is a real number, then x-I < Lx J ::::: x ::::: 61. Draw the graph of the functionf(x ) = Lx J + Lx /2J from fxl < x + 1. Rto R. 46. Show that if x is a real number and m is an integer, then 62. Draw the graph ofthe function f(x) = fxl + Lx /2J from fx + ml = fxl + m. Rto R. 47. Show that if x is a real number and n is an integer, then 63. Draw graphs of each of these functions. a) f(x) Lx + J b) f(x) L2x + IJ a) x < n if and only if LxJ < n. = � = c) f(x) fx/3 d) b) n < x if and only ifn < fxl = 1 f(x) = fl/xl e) f(x) fx - 2 + x + 2J 48. Show that if x is a real number and n is an integer, then = 1 L f) f(x) = L2xHx/21 g) f(x) = f Lx - �J + a) x ::::: n ifand only i[ fxl ::::: n. �l b) n ::::: x ifand only ifn ::::: LxJ. 64. Draw graphs ofeach ofthese functions. a) f(x) = f3x - 21 b) f(x) = fO.2xl 49. Prove that ifn is an integer, then Ln/2J = n/2 ifn is even c) d) f(x) Lx2 and (n - 1)/2 ifn is odd. f(x) = L -1/xJ = J e) f(x) = fx/21 Lx/2J f) f(x) = Lx/2J + fx/2J 50. Prove that if x is a real number, then L -x J = - fx 1 and g) f(x) = L2 fx/2l + J f -xl = - LxJ. � 65. 3 51. The function INT is found on some calculators, where Find the inverse functionof fex) = x + 1. INT(x) = Lx J when x is a nonnegative real number and 66. Suppose that f is an invertible function from Y to Z and INT(x) = xl when x is a negative real number. Show g is an invertible function from X to Y. Show that the inverse of the composition fog is given by (f 0 g)-I that this INTf function satisfies the identity INT( -x) = I I = -INT(x). g- of- .

52. Let a and be real numbers with a < Use the floor 67. Let S be a subset of a universal set The characteristic function and/orceil ingb functionsto express the numberb. of integers fs of S is the function fromU. to the set to, 1} n that satisfythe inequality a ::::: n ::::: such that fs (x) = 1 if x belongs to S andU fs ex) = 0 if x b. does not belong to S. Let A and B be sets. Show that for 53. Let a and be real numbers with a < Use the floor and/orceil ingb functionsto express the numberb. of integers all x, . n that satisfy the inequality a < n < a) hnBeX) = hex) fBeX) . 54. How many bytes are required to encodeb. n bits of data b) hUB(X) = hex) + /H ex) - fA(X) /H ex) where n equals c) hex) = 1 - fA(X) a) 4? b) 1O? c) 500? d) 3000? d) fAEBB(X) = fAeX) + fBeX) - 2h(x)fBeX ) 55. How many bytes are required to encode n bits of data 68. Suppose that f is a functionfrom A to B, where A and B where n equals are finite sets with 1 A 1 = 1 B I . Show that f is one-to-one a) 7? b) 17? c) 1001? d) 28,800? if and only if it is onto. 56. How many ATM cells (described in Example 26) can be 69. Prove or disprove each ofthese statements about the floor transmitted in 10 seconds over a link operating at the fo l­ and ceiling functions. lowing rates? a) r Lx J 1 = Lx J for all real numbers x. a) 128 kilobits per second (1 kilobit = 1000 bits) b) L2xJ = 2LxJ whenever x is a real number. b) 300 kilobits per second c) fxl + fyl - fx + yl = 0 or 1 �henever. x andya re c) I megabit per second (1 megabit = 1,000,000 bits) real numbers. 2-39 2.4 Sequences and Summations 149

d) rxyl = rxl ryl for all real numbers x and y. 73. For each of these partial functions,determine its domain, x 1 codomain, domain of definition, and the set of values for e) = for all real numbers x. I � l l ; J which it is undefined.Also, determine whether it is a total 70. Prove or disprove each ofthese statements about the floor function. and ceiling functions. a) f: Z --+ R, fen) = lin a) Lrxl J = rxl for all real numbers x. b) f: Z --+ Z, fen) = rn/21 b) Lx + y J = LxJ + LY J for all real numbers x and y. c) f: ZxZ--+ Q,f(m, n) = m/n c) rrx/21 /21 = rx/41 for all real numbers x. d) f: ZxZ--+ Z,f(m, n) =mn d) L J = J for all positive real numbers x. ,JrxT e) f:ZxZ--+Z,f(m,n)=m- nifm>n e) LxJ + LYJ+ L..jX Lx + yJ .::: L2xJ + L2yJ for all real numbers x and y. 74. a) Show that a partial functionfrom A to B can be viewed as a function f* fromA to B {u}, where u is not an 71. Prove that ifx is a positive real number, then U element of B and a) LvTxTJ = L J. b) r,JrxT l = ..jX f(a) if a belongs to the domain 72. Let x be a real..jX number.l. Show that L3xJ = LxJ + r f*(a) = of definitionof f Lx + J + Lx + J. 1 � u if f is undefined at a. A program designed to evaluate a function may not produce I the correct value ofthe functionfor all elements in the domain of this function. For example, a program may not produce a b) Using the construction in (a), findthe functionf* cor­ correct value because evaluating the functionmay lead to an responding to each partial function in Exercise 73. infinite loop or an overflow. Similarly, in abstract mathemat­ E3f>75. a) Show that if a set S has cardinality m, where m is a ics, we oftenwant to discuss functionsthat are definedonly for positive integer, then there is a one-to-one correspon­ a subset of the real numbers, such as 1/x, and arcsin (x). dence between S and the set {I, 2, ..., m }. Also, we may want to use such notions as the..jX, "youngest child" b) Show that if S and T are two sets each with m ele­ function,which is undefinedfor a couple having no children, ments, where m is a positive integer, then there is a or the "time of sunrise," which is undefined for some days one-to-one correspondence between S and T. above the Arctic Circle. *76. Show that a set S is infiniteif and only ifthere is a proper To study such situations, we use the concept of a partial subset A of S such that there is a one-to-one correspon­ function. A f from a set A to a set B is an partial fu nction dence between A and S. assignment to each element a in a subset of A, called the do­ f, *77. Show that the polynomial function f : Z+ x Z+ --+ Z+ main of definitionof of a unique element in B. The sets f(m, n) = (m n - 2)(m n - 1)/2 m A and B are called the domain and codomainb of f, respec­ with + + + is one­ tively. We say that f is undefined for elements in A that are to-one and onto. not in the domain of definitionof f. We write f : A --+ B to *78. Show that when you substitute (3n + 1)2 for each occur­ denote that f is a partial function from A to B. (This is the rence of n and (3m + 1)2 for each occurrence of m in the same notation as is used for functions. The context in which right-hand side of the formula for the function f(m, n) the notation is used determines whether f is a partial function in Exercise 77, you obtain a one-to-one polynomial func­ or a total function.)When the domain of definitionof f equals tion Z x Z --+ Z. It is an open question whether there is A, we say that f is a total function. a one-to-one polynomial functionQ x Q --+ Q.

2.4 Sequences and Summations

Introduction

Sequences are ordered lists of elements. Sequences are used in discrete mathematics in many ways. They can be used to represent solutions to certain counting problems, as we will see in Chapter 7. They are also an important data structure in computer science. This section contains a review of the notation used to represent sequences and sums of terms of sequences. When the elements of an infinite set can be listed, the set is called countable. We will conclude this section with a discussion of both countable and uncountable sets. We will prove that the set of rational numbers is countable, but the set of real numbers is not. 164 2/ Basic Structures: Sets, Functions, Sequences, and Sums 2-54 Review Questions

1. Explain what it means for one set to be a subset of another b) Definewhat it means for a function from the set of set. How do you prove that one set is a subset of another positive integers to the set of positive integers to be set? onto. 2. What is the empty set? Show that the empty set is a subset c) Give an example of a function fr om the set of posi­ of every set. tive integers to the set of positive integers that is both 3. a) Define lSI, the cardinality of the set S. one-to-one and onto. b) Give a formula for IA U BI, where A and B are sets. d) Give an example of a function fromthe set of positive 4. a) Definethe power set of a set S. integers to the set ofpositive integers that is one-to-one b) When is the empty set in the power set of a set S? but not onto. c) How many elements does the power set of a set S with e) Give an example of a function from the set of posi­ n elements have? tive integers to the set of positive integers that is not one-to-one but is onto. 5. a) Define the union, intersection, difference, and sym­ metric difference of two sets. f) Give an example of a function fromthe set of positive b) What are the union, intersection, difference,and sym­ integers to the set of positive integers that is neither metric differenceof the set of positive integers and the one-to-one nor onto. set of odd integers? 10. a) Definethe inverse of a function. 6. a) Explain what it means for two sets to be equal. b) When does a function havean inverse? b) Describe as many of the ways as you can to show that c) Does the functionf(n ) = 10 - n fromthe set ofinte­ two sets are equal. gers to the set of integers have an inverse? If so, what c) Show in at least two different ways that the sets is it? A - (B n C) and (A - B) U (A - C) are equal. 11. a) Definethe floorand ceiling functionsfrom the set of 7. Explain the relationship between logical equivalences and real numbers to the set of integers. Lx J set identities. b) For which real numbers x is it true that = rxl ? 8. a) Define the domain, codomain, and the range of a 12. Conjecture a formula for the terms of the sequence that function. begins 8, 14, 32, 86, 248 and findthe next three terms of b) Let f(n) be the function from the set of integers to your sequence. the set of integers such that f(n) n2 1. What are = 13. What is the sum ofthe terms ofthe geometric progression the domain, codomain, and range of this+ function? n a ar ar when r =1= I? 9. a) Define what it means for a function from the set of + + ... + positive integers to the set of positive integers to be 14. Show that the set ofodd integers is countable. one-to-one. 15. Give an example of an uncountable set.

Supplementary Exercises

1. Let A be the set of English words that contain the letter 4. Let E denote the set of even integers and 0 denote the x, and let B be the set of English words that contain the set of odd integers. As usual, let Z denote the set of all letter q. Express each of these sets as a combination of A integers. Determine each of these sets. and B. a) EUO b) EnO c) Z-E d) Z-O a) The set of English words that do not contain the letter 5. Show that if A and B are sets, then A - (A - B) = x. AnB. b) The set ofEnglish words that contain both an x and a 6. q. Let A and B be sets. Show that A � B if and only if c) The set ofEnglish words that contain an x but not a q. AnB = A. d) The set ofEnglish words that do not contain either an 7. Let A, B, and C be sets. Show that (A - B) - C is not x or a q. necessarily equal to A - (B - C). e) The set ofEnglish words that contain an x or a q, but not both. 8. Suppose that A, B, and C are sets. Prove or disprove that (A - B) - C = (A - C) - B. 2. Show that if A is a subset of B, then the power set of A is a subset of the power set of B. 9. Suppose that A, B, C, and D are sets. Prove or disprove D) - C) - - D). 3. Suppose that A and B are sets such that the power set of that (A - B) - (C - = (A (B A is a subset of the power set of B. Does it follow that A 10. Show that if A and B are finite sets, then IA n BI :::: is a subset of B? IA U BI. Determine when this relationship is an equality. 2-55 Computer Projects 165

11. Let A and B be sets in a finiteuniversal set U. List the 20. For which real numbers and is it true that following in order of increasing size. x y A A UB A n B U rx + y1 = rx1 + ryl? a) I I, I I, I I, l I, 101 21. For which real numbers and IS it true that b) IA - B I, IA EEl BI, IAI IBI, IA UBI, 101 x y 12. Let A and B be subsets ofthe+ finiteuniversal set U. Show 22. Proverx + y that1 = rx1 + LYJ? for all integers IAn I UI - IAI - IB I IA n BI that 81 · 23. Prove that Lnif/2 J rn/2is an1 =integer, Ln2/4 J then n. 13. Let 1 and g be= functions from {I,+ 2, 3, 4} to {a, b, c, d} - 1, unless m is an integer, in which Lxcase,J + itLm equals -xJ = and from {a, b, c, d} to {I, 2, 3, 4}, respectively, such 24. Provem that if xis a real number, then m. that d, c, and 1(4) b, and a, 25. Prove that xif is an odd integer,LLx /2thenJ/2 J = Lx /4J. 1(1)2, =g(b) 1(2)I, = 1(3)3, and = g(d) = 2. = n rn2/41 = g(aa) Is) = 1 one-to-one?= g(Is c)g one-to-one?= (n2 + 3)/4. 26. Prove that if and n are positive integers and is a real b) Is 1 onto? Is g onto? number, thenm x c) Does either 1 or g have an inverse? If so, find this inverse. 14. Let 1 be a one-to-one function from the set A to the set Lx +n x n B. Let S and T be subsets of A. Show that n T) l J J = l � J n I(S m I(S) I(T). = 15. Give an example to show that the equality in Exercise 14 *27. Prove that if is a positive integer and is a real number, may not hold if1 is not one-to-one. then m x Suppose that 1 is a functionfrom A to B. We definethe func­ P(A) P(B) tion SI from to by the rule SI(X) I(X) for l l A. LmxJ = LxJ + x + �J + x + �J + ... each subset X of Similarly, we define the function= SI-I from P(B) to P(A) by the rule for each � 1 B. SI-I(y) I-I (y) m subset Y of Here, we are using Definition= 4, and the defi­ + lx + J nition of the inverse image of a set found in the preamble to Exercise 38, both in Section 2.3. *28. We define the U1am numbers by setting U I 1 and U 2. Furthermore, after determining whether= the in­ *16. a) Prove that if 1 is a one-to-one function from A to B, tegers= less than are Ulam numbers, we set equal to then is a one-to-one functionfrom P(A) to P(B). 2 SI the next Ulam numbern if it can be written uniquelyn as the b) Prove that if 1 is an onto function from A to B, then sum of two different Ulam numbers. Note that U 3, is an onto functionfrom P(A) to P(B). 3 SI U 4, U 6, and U6 8. = c) Prove that if 1 is an onto function from A to B, then 4 = 5 = = is a one-to-one function from P(B) to P(A). a) Find the first20 Ulam numbers. SI -I d) Prove that if 1 is a one-to-one function from A to B, b) Prove that there are infinitelymany Ulam numbers. P(B) P(A). 29. ! O I then SI-I is an onto functionfrom to Determine the value ofn �I . (The notation used here e) Use parts (a) through (d) to conclude that if 1 is a for products is defined in the kpreamblet to Exercise 27 in A B, one-to-one correspondence from to then SI is Section 2.4.) a one-to-one correspondence from P(A) to P(B) and *30. Determine a rule for generating the terms ofthe sequence P(B) SI -I is a one-to-one correspondence from to that begins 1, 3, 4, 8, 15, 27, 50, 92, ...,a nd find the next P(A). four terms of the sequence. 17. Prove that if 1 and g are functions from A to B and *31. Determine a rule for generating the terms of the se­ SI Sg (using the definitionin the preamble to Exercise quence that begins 2, 3, 3, 5, 10, 13, 39, 43, 172, 1 77, 16),= then for all E A. 885, 891, ... , and find the next four terms of the 18. Show thatI(x if ) is= ang(x integer,) thenx Ln/2J . sequence. 19. n n = rn/ 21 + *32. A B A B For which real numbers and is it true that Prove that if and are countable sets, then x is x y also a countable set. Lx + yJ = LxJ + LYJ?

Computer Projects

Write programs with the specified inputand output.

1. Given subsets A and B of a set with elements, 2. Given multisets A and B from the sameuniversal set, find use bit strings to find A, AU B, An B, nA - B, and AU B, A n B, A - B, and A B (see preamble to Exer­ A EEl B. cise 59 of Section 2.2). + 3-25 3.2 The Growth of Functions 191

Exercises

n 2 In Exercises 1-14, to establish a big-O relationship, findwit­ c) (n ! + 2 )(n3 + log(n + I» nesses C and such that If(x)1 ::: Clg(x)1 whenever x > k k. 20. Give a big-O estimate for each ofthese functions.For the 1. Determine whether each of these functions is O(x). function g in your estimate f(x) is O(g), use a simple a) f(x) = 10 b) f(x) = 3x + 7 functiong of smallest order. f(x) = x2 +x + 1 d) f(x) = S logx a) (n3 + n2 10g n)(log n + I) + (1710g n + 19)(n3 + 2) c) n n e) f(x) = LxJ t) f(x) = fx/21 b) (2 + n2)(n3 + 3 ) n n sn n 2. Determine whether each of these functions is 0(x2). c) (n + n2 + )(n ! + s ) a) f(x) = 17x + 11 b) f(x) = x2 + 1000 21. Give a big-O estimate for each ofthese functions.For the function g in your estimate that f(x) is O(g(x» , use a c) f(x) =xlog x d) f(x) =x4/2 e) f(x) = 2x t) f(x) = LxJ . fxl simple function g of the smallest order. 2 2 3. Use the definition of "f(x) is O(g(x» " to show that a) n log(n + I) + n 10g n 2 2 x4 + 9x3 + 4x + 7 is 0(X4). b) (n log n + 1) + (log n + 1)(n + I) 2n n2 4. Use the definition of "f(x) is O(g(x» " to show that c) n + n 2x + 17 is OW). 22. For each function in Exercise I, determine whether that 5. Show that (x2 + I)/(x + I) is O(x). function is Q(x) and whether it is 8(x). 23. For each function in Exercise 2, determine whether that 6. Show that (x3 + 2x)/(2x + I) is 0(x2). functionis Q(x2) and whether it is 8(x2). 7. Find the least integer n such that f(x) is O(xn) for each 24. of these functions. a) Show that 3x + 7 is 8(x). 2 2 a) f(x) = 2x3 + x2 10g x b) Show that 2x + x - 7 is 8(x ). f(x) = 3x3 c) Show that Lx + 1/2J is 8(x). b) + (logx)4 2 f(x) = (x4 + x2 + 1)/(x3 + I) d) Show that log(X + I) is 8(log2 x). c) Show that 10g x is 8(log x). d) f(x) = (x4 + S logx)/(x4 + 1) e) IO 2 8. Find the least integer n such that f(x) is O(xn) for each 25. Show that f(x) is 8(g(x» if and only if f(x) is O(g(x» of these functions. and g(x) is O(f(x» . a) f(x) = 2x2 + x3 10g x 26. Show that if f(x) and g(x) are functions from the set b) f(x) = 3x5 + (logx)4 of real numbers to the set of real numbers, then f(x) is 2 O(g(x» if and only if g(x) is Q(f(x» . c) f(x) = (x4 + x + 1)/(x4 + 1) d) f(x) = (x3 + S log x)/(x4 + I) 27. Show that if f(x) and g(x) are functions from the set 9. Show that x2 + 4x + 17 is 0(x3) but that x3 is not of real numbers to the set of real numbers, then f(x) is 0(x2 + 4x + 17). 8(g(x» if and only if there are positive constants C1 , and C such that C Ig(x)1 ::: If(x)1 ::: C Ig(x)1 wheneverk, 10. Show that x3 is 0(X4) but that X4 is not 0(x3). 2 I 2 x> 11. Show that 3x4 + I is 0(x4/2) and x4/2 is 0(3x4 + I). 28. a Showk. that 3x2 + x + I is 8(3x2) by directly finding 12. Show that x logx is 0(x2) but that x2 is not O(x logx). ) the constants C1 , and C2 in Exercise 27. 13. Show that 2n is 0(3n) but that 3n is not 0(2n). b) Express the relationshipk, in part (a) using a picture 2 . 2 14. Is it true that x3 is o (g(x »,if g is the given function?[For showing the functions 3x + x + I, C I 3x , and 2 example, if g(x) = x + I, this question asks whether x3 C2 ·3x , and the constant on the x-axis, where is O(x + I).] C1 , C2, and are the constantsk you found in part (a) 2 to show that k3x 2 + x + I is 8(3x2). a) g(x) = x b) g(x) = x3 2 3 2 29. Express the relationship f(x) is 8(g(x» using a picture. c) g(x) = x + x d) g(x) = x +x4 e) g(x) = ]X t) g(x) = x3/2 Show the graphs of the functions f(x), CI Ig(x)l, and 15. Explain what it means for a functionto be 0(1). C2 Ig(x)l, as well as the constant k on the x-axis. 16. Show that if f(x) is O(x), then f(x) is 0(x2). 30. Explain what it means for a functionto be Q(1). 17. Suppose that f(x), g(x), and h(x) are functionssuch that 31. Explain what it means for a function to be 8(1). f(x) is O(g(x» and g(x) is O(h(x» . Show that f(x) is 32. Give a big-O estimate of the product of the first n odd O(h(x» . positive integers. 18. Let be a positive integer. Show that I k + 2k + ...+ nk 33. Show that if f and g are real-valued functions such that l is O(k nk+ ). f(x) is O(g(x» , then for every positive integer fk(x) 19. Give as good a big-O estimate as possible for each of is O(gk(x» . [Note that fk(x) = f(xl.] k, these functions. 34. Show that for all real numbers a and with a > I and a (n2 + 8)(n + I) b) (n log n + n2)(n3 + 2) > I, if f(x) is (log x), then f(x) isb O(log x). ) b o b a 192 3 / The Fundamentals: Algorithms,the Integers, and Matrices 3-26

35. Suppose that f(x) is o (g(x » where f and g are increas­ c) x2 is o(2X). ing and unbounded functions. Show that log If(x)1 is d) x2 + X + 1 is not o(x2). O(log Ig(x )1). 52. (Requires calculus) 36. Suppose that f(x) is O(g(x» . Does it follow that 2f(x) is . a) Show that if f(x) andg(x) are functionssuch that f(x) o (2g(x» ? is o(g(x» and c is a constant, then cf(x ) is o(g(x» , 37. Let fl (x) and hex) be functions fromthe set of real num­ where (cf)(x) = cf(x). bers to the set of positive real numbers. Show that if fl (x) b) Show that if fl(X), hex), and g(x) are functions and f2(X) are both 8(g(x »,where g(x) is a functionfrom such that fl (x) is o(g(x» and hex) is o(g(x» , the set of real numbers to the set of positive real numbers, then (fl + h)(x) is o(g(x» , where (fl + h)(x) = then fl (x) + hex) is 8(g(x» . Is this still true if fl (x) fl (x) + hex). and f2(X) can take negative values? 53. (Requires calculus) Represent pictorially that x log x is 38. Suppose that f(x), g(x), and h(x) are functionssuch that O(X2) by graphing x logx, x2, and x log x/x2. Explain f(x) is 8(g(x» and g(x) is 8(h(x» . Show that f(x) is how this picture shows that x log x is O(X2). 8(h(x» . 54. (Requires calculus) Express the relationship f(x) is 39. If fl (x) and hex) are functions from the set of posi­ o(g(x» using a picture. Show the graphs of f(x), g(x), tive integers to the set of positive real numbers and fl (x) and f(x)/g(x). and f2 (x) are both 8(g(x» , is (fl - f2)(X) also 8(g(x» ? *55. (Requires calculus) Suppose that f(x) is o(g(x» . Does it Either prove that it is or give a counterexample. follow that 2f(x) is o(2g(x» ? 40. Show that if fl (x) and f2(X) are functions from the *56. (Requires calculus) Suppose that f(x) is o(g(x» . Does it set of positive integers to the set of real numbers and fo llow that log If(x)1 is o(log Ig(x )I)? fl (x) is 8(gl (x» and hex) is 8(g (X»,then (fJ / )(X) is 2 2 57. (Requires calculus) The two parts ofthis exercise describe 8(glg (X». 2 the relationship between little-o and big-O notation. 41. Find functions f and g from the set of positive integers a) Show that if f(x) and g(x) are functions such that to the set of real numbers such that fen) is not O(g(n» f(x) is o(g(x» , then f(x) is O(g(x» . and g(n) is not O(f(n» simultaneously. b) Show that if f(x) and g(x) are functions such that 42. Express the relationship f(x) is Q(g(x» using a picture. f(x) is O(g(x» , then it does not necessarily fo llow Show the graphs of the functionsf(x ) and Cg(x), as well that f(x) is o(g(x» . as the constant k on the real axis. 58. (Requires calculus) Show that if f(x) is a polynomial of 43. Show that if fl (x) is 8(gl (x» , hex) is 8(g2(X», and degree n and g(x) is a polynomial of degree m where f2(X) i= 0 and g2 (x) i= 0 for all real numbers x > 0, then m > n, then f(x) is o(g(x» . (fJ /h )(x) is 8« gJ/g )(X». 2 n n I 59. (Requires calculus) Show that if fl (x) is O(g(x» and 44. x x - + ... ao, Show that if f(x) = an + an_l + alx + hex) is o(g(x» , then fl (x) + hex) is O(g(x» . where ao, ai, ... , an I , and an are real numbers and - 60. (Requires calculus) Let H be the nth an i= 0, then f(x) is 8(xn). n harmonic number 1 1 1 Big-O, big-Theta, and big-Omega notation can be extended H =I+ + +" '+ n 2" 3 ;; ' to functionsin more than one variable. For example, the state­ ment f(x , y) is O(g(x , y» means that there exist constants C, Show that Hn is o (log n). [Hint: First establish the kl , and k2 such that If(x, y)1 :::: Clg(x, y)1 whenever x > kl inequality and y > k2. n 1 n 1 L -: < -dx 45. Define the statement f(x , y) is 8(g(x , y» . X j=2 J I 46. Definethe statement f(x , y) is Q(g(x , y» . I by showing that the sum of the areas of the rectangles of 47. Show that (x2 + xy + X logy)3 is O(x6y3). height I/} with base from } - 1 to }, for } = 2, 3, ..., n, 5 3 48. Show that x5y3 + x4y4 + x3y is Q(x3y ). is less than the area under the curve y = 1/x from2 to n.] 49. Show that Lxy J is O(xy). *61. Show that n log n is O(log n!). 50. Show that fxYl is Q(xy). �62. Determine whether logn! is 8(n log n). Justify your The fo llowing problems deal with another type of asymptotic answer. notation, called Iittle-o notation. Because little-o notation is *63. Show that log n! is greater than (n log n)/4 for based on the concept of limits, a knowledge of calculus is n > 4. [Hint: Begin with the inequality n! > n(n - 1) needed for these problems. We say that f(x) is o(g(x» [read (n - 2) ··· fn/2l] f(x) is "little-oh" of g(x)], when Let f(x) and g(x) be functionsfrom the set ofreal numbers to f(x) lim = the set of real numbers. We say that the functions f and g are --->oo o. x g(x) asymptotic and write f(x) � g(x) iflimx--->oof(x )/g(x ) = 1. 51. (Requires calculus) Show that 64. (Requires calculus) For each of these pairs of functions, a) x2 is o(x3). b) x log x is O(X2). determine whether f and g are asymptotic. 3-27 3.3 Complexity of Algorithms 193

a) f(x) = x2 3x 7, g(x) = x2 10 65. For each ofthese pairs of functions, b) f(x) = x2 log+ x, +g(x ) = x3 + determine(Requires calculus)whether f and g are asymptotic. c) f(x) = X4 log(3x8 7), a) f(x) = log(X2 1), g(x) = log x g(x) = (x2 + 17x 3)2+ b) f(x) = 2x+3,g(x)+ = 2x+7 f(x) = (x3 + x2 +X 1)4, c) f(x) = 21' , g(x) = 2X2 d) g(x) = (x4 + x3 + x2+ X 1)3. f(x) = 2x2+x+ l , g(x) = 2x2+2x + + + + d)

3.3 Complexity of Algorithms

Introduction

When does an algorithm provide a satisfactory solution to a problem? First, it must always produce the correct answer. How this can be demonstrated will be discussed in Chapter 4. Second, it should be efficient. The efficiency of algorithms will be discussed in this section. How can the efficiencyof an algorithm be analyzed? One measure of efficiency is the time used by a computer to solve a problem using the algorithm, when input values are of a specified size. A second measure is the amount of computer memory required to implement the algorithm when input values are of a specifiedsize . Questions such as these involve the computational complexity ofthe algorithm. An analysis of the time required to solve a problem of a particular size involves the time complexity of the algorithm. An analysis of the computer memory required involves the space complexity of the algorithm. Considerations of the time and space complexity of an algorithm are essential when algorithms are implemented. It is obviously important to know whether an algorithm will produce an answer in a microsecond,a minute, or a billion years. Likewise, the required memory must be available to solve a problem, so that space complexity must be taken into account. Considerations of space complexity are tied in with the particular data structures used to implement the algorithm. Because data structures are not dealt with in detail in this book, space complexity will not be considered. We will restrict our attention to time complexity.

Time Complexity

The time complexity of an algorithm can be expressed in terms of the number of operations used by the algorithm when the input has a particular size. The operations used to measure time complexity can be the comparison of integers, the addition of integers, the multiplication of integers, the division of integers, or any other basic operation. Time complexity is described in terms of the number of operations required instead of actual computer time because of the difference in time needed for different computers to perform basic operations. Moreover, it is quite complicated to break all operations down to the basic bit operations that a computer uses. Furthermore, the fastest computers in existence can perform basic bit operations (for instance, adding, multiplying, comparing, or exchanging two bits) in 10-9 second (1 nanosecond), but personal computers may require 10-6 second (1 microsecond), which is 1000 times as long, to do the same operations. We illustrate how to analyze the time complexity of an algorithm by considering Algorithm 1 of Section 3. 1, which findsthe maximum of a finite set of integers.

EXAMPLE 1 Describe the time complexityof Algorithm 1 of Section 3.1 for finding the maximum element in a set.

Solution: The number of comparisons will be used as the measure of the time complexity of the algorithm, because comparisons are the basic operations used. 3-33 3.3 Complexity of Algorithms 199

required to solve this problem. But, if an algorithm requires 10 billion years to solve a problem, it would be unreasonable to use resources to implement this algorithm. One of the most interesting phenomena of modem technology is the tremendous increase in the speed and memory space of computers. Another important factor that decreases the time needed to solve problems on computers is parallel processing, which is the technique of performing sequences of operations simultaneously. Efficient algorithms, including most algorithms with polynomial time complexity, bene­ fit most from significanttechnology improvements. However, these technology improvements offer little help in overcoming the complexity of algorithms of exponential or factorial time complexity. Because of the increased speed of computation, increases in computer memory, and the use of algorithms that take advantage of parallel processing, many problems that were considered impossible to solve fiveyears ago are now routinely solved, and certainly fiveyears from now this statement will still be true.

Exercises

1. How many comparisons are used by the algorithm given procedure real in Exercise 16 of Section 3.1 to findthe smallest natural numbers)pol ynomial(c, ao, aI, ..., an: number in a sequence of natural numbers? 2. Write the algorithm that putsn the first four terms of a list power:= I of arbitrary length in increasing order. Show that this al­ y:for=a :=o 1 to gorithm has time complexity 0(1) in terms ofthe number begini n of comparisons used. * 3. Suppose that an element is known to be among the first power :=+ power* c y := y ai +power + ... + + four elements in a list of 32 elements. Would a linear end n n-1 search or a binary search locate this element more rapidly? where the {yfinal = a valuenc ofa n_lisC the value ofa thelc polynomialaol at x y 4. Determine the number of multiplications used to find k a) Evaluate= c. + x 1 at x by working through starting with x and successively squaring (to find X24 , each step ofthe3x2 algorithm+ showing= 2 the values assigned and so on). Is this a more efficient way tofind ' thanx2, by multiplying x by itself the appropriate numberx 2of times? at each assignment step. b) Exactly how many multiplications and additions are 5. Give a big-O estimate for the number ofcomparisons used used to evaluate a polynomial of degree at x ? by the algorithm that determines the number of 1 s in a bit (Do not count additions used to incrementn the =loop string by examining each bit of the string to determine c variable.) whether it is a 1 bit (see Exercise of Section 3.1). 8. There is a more efficientalgorithm (in terms of the num­ a) Show that this algorithm determines25 the number of *6. ber of multiplications and additions used) for evaluating 1 bits in the bit string S: polynomials than the conventional algorithm described in procedure (S: bit string) the previous exercise. It is called Horner's method. This 0 bit count pseudocode shows how to use this method to findthe value ' ... cwhileount :=S i= 0 of n + n-1 + + + at x begin anxprocedurean_lX alX ao = c.real + 1 numbers)Horner( c, ao, aI, a2, ... , an: cSount S := /\ c (Sount - 1) end := is the number of 1 s in S} fory:=a n 1 to {count i := * n+ ... Here S - I is the bit string obtained by changing the y := y n c an-ni -1 + + + rightmost 1 bit of S to a 0 and all the 0 bits to the right a) Evaluate{y = a nc ++ a xn_l +C 1 at x byal cworking aol through of this to 1 s. [Recall that S /\ (S - 1) is the bitwise each step of3xthe 2 algorithm showing= 2 the values assigned AND of S and S - 1.] at each assignment step. b) How many bitwise AND operations are needed to find b) Exactly how many multiplications and additions are the number of 1 bits in a string S? used by this algorithm to evaluate a polynomial of 7. The conventional algorithm for evaluating a polynomial degree at x ? (Do not count additions used to + + ... + + at x can be ex­ incrementn the loop= c variable.) presseda xn ain pseudocoden-1 by 9. n n_lX alx ao = c What is the largest n for which one can solve in one 200 3 / The Fundamentals: Algorithms,the Integers, and Matrices 3-34

second a problem using an algorithm that requires fen) 19. Analyze the worst-case time complexity of the algorithm bit operations, where each bit operation is carried out in you devised in Exercise 31 of Section 3.1 for findingthe 10-9 second, with these values for f(n)? first termof a sequence of integers equal to some previous a) log n b) n c) n log n term. d) n2 e) 2n 1) n! 20. Analyze the worst-case time complexity ofthe algorithm 10. How much time does an algorithm take to solve a problem you devised in Exercise 32 of Section 3.1 for finding all of size n if this algorithm uses 2n2 2n bit operations, terms of a sequence that are greater than the sum of all each requiring 10-9 second, with these+ values of n? previous terms. a) 10 b) 20 c) 50 d) 100 21. Analyze the worst-case time complexity of the algorithm 11. How much time does an algorithm using 250 bit operations you devised in Exercise 33 of Section 3.1 for findingthe need if each bit operation takes these amounts of time? firstterm of a sequence less than the immediately preced­ ing term. a) 10-6 second b) 10-9 second c) 10-12 second 12. Determine the least number of comparisons, or best-case 22. Determine the worst-case complexity in terms of com­ performance, parisons of the algorithm from Exercise 5 in Section 3.1 for determining all values that occur more than once in a required to find the maximum of a sequence of n in­ a) sorted list of integers. tegers, using Algorithm 1 of Section 3.1. b) used to locate an element in a list of n terms with a 23. Determine the worst-case complexity in terms of compar­ linear search. isons of the algorithm from Exercise 9 in Section 3.1 for determining whether a string is a palindrome. c) used to locate an element in a list of n terms using a binary search. 24. How many comparisons does the selection sort (see 13. Analyze the average-case performance ofthe linear search preamble to Exercise 41 in Section 3.1) use to sort n items? Use your answer to give a big-O estimate of the complex­ algorithm, if exactly half the time element x is not in the ity ofthe selection sort in terms of number of comparisons list and if x is in the list it is equally likely to be in any position. for the selection sort. 14. An algorithm is called optimalfor the solution of a prob­ 25. Find a big-O estimate for the worst-case complexity in lem with respect to a specified operation if there is no terms of number of comparisons used and the number of algorithm for solving this problem using fewer operations. terms swapped by the binary insertion sort described in the preamble to Exercise 47 in Section 3.1. a) Show that Algorithm 1 in Section 3.1 is an optimal al­ gorithm with respect to the number of comparisons of 26. Show that the greedy algorithm for making change for n integers. Comparisons used for bookkeeping cents using quarters, dimes, nickels, and pennies has O(n) in the loop(N areote: not of concernher e.) complexity measured in terms of comparisons needed. b) Is the linear search algorithm optimal with respect to 27. Describe how the number of comparisons used in the the number of comparisons of integers (not including worst case changes when these algorithms are used to comparisons used for bookkeeping in the loop)? search for an element of a list when the size of the list 15. Describe the worst-case time complexity, measured in doubles from n to 2n, where n is a positive integer. terms of comparisons, of the ternary search algorithm a) linear search described in Exercise 27 of Section 3.1. b) binary search 16. Describe the worst-case time complexity, measured in 28. Describe how the number of comparisons used in the terms of comparisons, of the search algorithm described worst case changes when the size of the list to be sorted in Exercise 28 of Section 3.1. doubles from n to 2n, where n is a positive integer when 17. Analyze the worst-case time complexity ofthe algorithm these sorting algorithms are used. you devised in Exercise 29 of Section 3.1 for locating a a) bubble sort b) insertion sort mode in a list of nondecreasing integers. c) selection sort (described in the preamble to Exercise 18. Analyze the worst-case time complexity of the algorithm 41 in Section 3.1) you devised in Exercise 30 of Section 3.1 for locating all d) binary insertion sort (described in the preamble to modes in a list of nondecreasing integers. Exercise 47 in Section 3.1)

3.4 The Integers and Division

Introduction

The part of mathematics involving the integers and their properties belongs to the branch of mathematics called number theory. This section is the beginning of a fo ur-section introduction 208 3 / The Fundamentals: Algorithms, the Integers, and Matrices 3-42

Now replace each ofthese numbers p by f(p) = (p + 3) mod 26. This gives

IS 7722 1 17 23 11 16 22 10 7 18 320 13. Translating this back to letters produces the encrypted message "PHHW BRX LQ WKH SDUN." <0lIl To recover the original message from a secret message encrypted by the Caesar cipher, the I function f- , the inverse of f, is used. Note that the function f-I sends an integer p from I {O, 1 , 2, ... , 25} to f- (p) = (p - 3) mod 26. In other words, to find the original message, each letter is shiftedback three letters in the alphabet, with the first threeletters sent to the last three letters of the alphabet. The process ofdetermining the original message from theencrypted message is called decryption. There are various ways to generalize the Caesar cipher. For example, instead of shifting each letter by 3, we can shifteach letter by k, so that

f(p) = (p + k) mod 26.

Such a cipher is called a shiftcipher . Note that decryption can be carried out using I f- (p) = (p - k) mod 26.

Obviously, Caesar's method and shiftciphers do not provide a high level of security. There are various ways to enhance this method. One approach that slightly enhances the security is to use a functionof the form

f(p) = (ap + b) mod 26,

where a and b are integers, chosen such that f is a bijection. (Such a mapping is called an affi ne transformation.) This provides a number of possible encryption systems. The use of one of these systems is illustrated in Example 10.

= EXAMPLE 10 What letter replaces the letter K when the function f(p) (7p + 3) mod 26 is used fo r en­ cryption?

Solution: First, note that 10 represents K. Then, using the encryption function specified, it = fo llows that f(lO) (7 ·10 + 3) mod 26 = 21. Because 21 represents V, K is replaced by V in the encrypted message. <0lIl Caesar's encryption method, and the generalization of this method, proceed by replacing unkS� each letter of the alphabet by another letter in the alphabet. Encryption methods of this kind are vulnerable to attacks based on the frequency of occurrence of letters in the message. More sophisticated encryption methods are based on replacing blocks of letters with other blocks of letters. There are a number of techniques based on modular arithmetic for encrypting blocks of letters. A discussion of these can be found in the suggested readings listed at the end of the book.

Exercises

1. Does 17 divide each of these numbers? 4. Show that part (iii ) of Theorem 1 is true. a) 68 b) 84 c) 357 d) 1001 5. Show that if a I and a, where a and are integers, then a or a b b I b 2. Show that ifa is an integer other than0, then = b = -b. a) 1 divides a. b) a divides O. 6. Show that if a, c, and are integers such that a I C and 3. Show that part (ii ) of Theorem 1 is true. bid, then ab I b,cd . d 3-43 3.4 The Integers and Division 209

7. Show that if a, b, and are integers with =1= 0, such that 23. Find counterexamples to each of these statements about a I then a I b. e e e be, congruences. 8. Prove or disprove that if a I where a, b, and are pos­ a) If ae = (mod m), where a, b, and m are integers itive integers, then a I b or abe, Ie . e with m =:':be 2, then a = b (mod m).e, 9. What are the quotient and remainder when b) If a = b (mod m) and = (mod m), where a, b, d, a) 19 is divided by 7? and m are integers withe dand positive and m =:': 2,e, then aC = bd (mod m). e d b) -111 is divided by II? 2 c) 789 is divided by 23? 24. Prove that if n is an odd positive integer, then n = d) 1001 is divided by 13? 1 (mod 8). 0 is divided by 19? 25. a, e) Show that if b, and m are integersk such that t) 3 is divided by 5? =:': 1, m =:': 2, and a =k, b (mod m), then a = bk(mod m) g) -1 is divided by 3? wheneverk is a positive integer. b) 4 is divided by I? 26. Which memoryk locations are assigned by the hashing 10. What are the quotient and remainder when function = mod 101 to the records of insurance a) 44 is divided by 8? company h(customersk) k with these Social Security numbers? b) 777 is divided by 21 ? a) 104578690 b) 432222187 c) -123 is divided by 19? c) 372201919 d) 501338753 d) -1 is divided by 23? 27. A parking lot has 31 visitor spaces, numbered from 0 to e) -2002 is divided by 87? 30. Visitors are assigned parking spaces using the hash­ t) 0 is divided by 17? ing function = mod 31, where is the number g) 1,234,567 is divided by 1001? formed fromh( thek) firstk three digits on a kvisitor's license b) -100 is divided by 1OI? plate. 11. Let m be a positive integer. Show that a = b (mod m) if a) Which spaces are assigned by the hashing functionto a mod m = b mod m. cars that have these first three digits on their license 12. Let m be a positive integer. Show that a mod m = plates? b mod m if a = b (mod m). 317, 918, 007, 100, 111, 310 13. Show that if n and are positive integers, then n / = L / J r 1 b) Describe a procedure visitors should fo llow to find a (n - 1) 1. k k free parking space, when the space they are assigned 14. Show thatk if+ a is an integer and d is a positive inte­ is occupied. ger greater than 1, then the quotient and remain­ 28. What sequence of pseudorandom numbers is gener­ der obtained when a is divided by d are La/dJ and ated using the linear congruential generator Xn+1 = a - dLa/dJ, respectively. (4xn 1) mod 7with seed xo = 3? 15. Find a formula for the integer with smallest absolute value 29. What +sequence of pseudorandom numbers is gener­ that is congruent to an integer a modulo m, where m is a ated using the pure multiplicative generator Xn+1 = positive integer. 3xn mod 11 with seed Xo = 2? 16. Evaluate these quantities. 30. Write an algorithm in pseudocode for generating a se­ a) -17 mod 2 b) 144 mod 7 quence ofpseudorandom numbers using a linear congru­ c) -101 mod 13 d) 199 mod 19 ential generator. 17. Evaluate these quantities. 31. Encrypt the message "DO NOT PA SS GO" by translating a) 13 mod 3 b) -97 mod 11 the letters into numbers, applying the encryption func­ c) 155 mod 19 d) -221 mod 23 tion given, and then translating the numbers back into 18. List fiveintegers that are congruent to 4 modulo 12. letters. 19. Decide whether each of these integers is congruent to a) f(p) = (p 3) mod 26 (the Caesar cipher) 5 modulo 17. b) f(p) = (p + 13) mod 26 f( ) a) 80 b) 103 c) p = (3p+ + 7) mod 26 c) -29 d) -122 32. Decrypt these messages encrypted using the Caesar cipher. 20. Show that if a = b (mod m) and = d (mod m), where

a, b, d, and m are integers withe m =:': 2, then a - = a) EOXH MHDQV b - de, (mod m). e b) WHVW WRGDB 21. Show that if n I where n and are positive integers c) HDW GLP VXP greater than 1, andm, if a = (modm where a and are All books are identifiedby an International Standard Book

integers, then a = (mod bn). m), b .•. b Number (ISBN), a lO-digit code XIX2 XIO, assigned by 22. Show that if a, b, e, and are integers such that m =:': 2, the publisher. (This system will change in 2007 when a new > 0, and a = b (mod m),m then ae = (mod 13-digit code will be introduced.) These 10 digits consist of e be me). 210 3/The Fundamentals: Algorithms, the Integers, and Matrices 3-44 blocks identifyingthe language, the publisher, the number as­ of the fifth edition of this book are 0-07-1 19881. What is signed to the book by its publishing company, and finally, a the check digit for that book? I-digit check digit that is either a digit or the letter X (used to 34. The ISBN of the fifthedition of represent 10). This check digit is selected so that L:�l == 0 is 0-32-Elementary123Q072, Nuwhere mber Q Th ise­ a (mod 11) and is used to detect errors in individual digitsix; and orydigit.and Find Its the Ap plicationsvalue of Q. transposition of digits. 35. Determine whether the check digit of the ISBN for this 33. The first nine digits of the ISBN of the European version textbook was computed correctly by the publisher.

3.5 Primes and Greatest Common Divisors

Introduction

In Section 3.4 we studied the concept of divisibility of integers. One important concept based on divisibility is that of a prime number. A prime is an integer greater than I that is divisible only by I and by itself. The study of prime numbers goes back to ancient times. Thousands of years ago it was known that there are infinitelymany primes. Great mathematicians of the last 400 years have studied primes and have proved many important results about them. However, many old conjectures about primes remain unsettled. Primes have become essential in modern cryptographic systems. An important theorem, the Fundamental Theorem of Arithmetic, asserts that every positive integer can be written uniquely as the product of prime numbers. The length oftime required to factor large integers into their prime factors plays an important role in cryptography. In this section we will also study the greatest common divisor of two integers, as well as the least common multiple of two integers. We will develop an algorithm for computing greatest common divisors in Section 3.6.

Primes

Every positive integer greater than I is divisible by at least two integers, because a positive integer is divisible by I and by itself. Positive integers that have exactly two differentpos itive integer factors are called primes.

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

Remark: The integer n is composite if and only if there exists an integer a such that a I n and 1 < a < n.

EXAMPLE 1 The integer 7 is prime because its only positive factors are 1 and 7, whereas the integer 9 is composite because it is divisible by 3. ....

The primes less than 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,37, 41,43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, and 97. In Section 7.6 we introduce a procedure, known as the sieve of Eratosthenes, that can be used to findall the primes not exceeding an integer n. The primes are the building blocks of positive integers, as the Fundamental Theorem of Arithmetic shows. The proof will be given in Section 4.2. 3-5/ 3.5 Primes and Greatest CommonDivisors 217

l DEFINITION 5 The east common multipleof the positive integers a andb is the smallest positive integer that is divisible by both a and b. The least commonmultiple of a andb is denoted by lcm(a, b).

The least common multiple exists because the set of integers divisible by both a and b is nonempty, and every nonempty set of positive integers has a least element (by the well-ordering property, which will be discussed in Section 4.2). Suppose that the prime factorizations of a and b are as before. Then the least common multiple of a and b is given by

= max(a\, btl max(a2, b2) ... max(an , bn ) I cm( a , b) PI P2 Pn where max(x , y) denotes the maximum ofthe two numbers x and y. This formula is valid because a common multiple of a and b has at least max(ai , bi) factors of Pi in its prime factorization, and the least common multiple has no other prime factors besides those in a and b.

EXAMPLE 15 What is the least common multiple of233572 and 2433?

Solution: We have

Theorem 5 gives the relationship between the greatest common divisor and least common multiple oftwo integers. It can be proved using the formulae we have derived for these quantities. The proof of this theorem is leftas an exercise for the reader.

THEOREM 5 Let a and b be positive integers. Then

ab = gcd(a, b) · lcm(a, b).

Exercises

1. Determine whether each of these integers is prime. *7. Show that log2 is an irrational number. Recall that an ir­ a) b) rational number3 is a real number x that cannot be written c) 21 d) 29 as the ratio of two integers. e) 71 1) 97 8. Prove that for every positive integer there are consecu­ 2. Determine111 whether each of these 14integers3 is prime. tive composite integers. Considern, the consecutiven integers starting with [Hin+ t: + n a) b) (n I)! 2] c) 19 d) 27 *9. Prove or disprove that there are three. consecutive odd e) 93 1) 101 positive integers that are primes, that is, odd primes ofthe form and + 3. Find10 the7 prime factorization of each113 of these integers. p, p p 10. Which positive+ 2, integers less4. than are relatively prime a) b) c) to 12 d) 88 e) 126 1) 729 11. Which12? positive integers less than are relatively prime 4. Find1001 the prime factorization1111 of each of these909,090 integers. to 30 a) b) c) 12. Determine30? whether the integers in each of these sets are d) 39 e) 81 1) 101 pairwise relatively prime. 5. Find14 the3 prime factorization289 of 899 a) b) *6. 1O!. c 21,34,55 d) 14,17,85 How many zeros are there at the end of 100!? ) 25,41,49,64 17, 18, 19,23 218 3/The Fundamentals: Algorithms,the Integers, andMatrices 3-52

13. Determine whether the integers in each of these sets are of a and b and the formulae for gcd(a, b) and lcm(a, b) pairwise relatively prime. in terms of these factorizations.] a) 11, 15, 19 b) 14, 15, 21 28. Find the smallest positive integer with exactly different c) 12, 17,31, 37 d) 7, 8, 9, 11 factors when n is n 14. We call a positive integer perfect if it equals the sum of a) 3. b) 4. c) 5. its positive divisors other than itself. d) 6. e) 10. a) Show that 6 and 28 are perfect. 29. Can you find a formula or rule for the nth term of a se­ b) Show that 2P-I (2P - 1) is a perfect number when quence related to the prime numbers or prime factoriza­ 2P - 1 is prime. tions so that the initial terms of the sequence have these 15. Show that if 2n - 1 is prime, then is prime. values? l ab = a a b b 2 Use the identity 2 - 1 (2 - 1) . (2n ( - ) + 2a( [Hin- ) +t: a) 0, 1, 1 , 0, 1,0, 1 , 0, 0, 0, 1,0, 1 , ... a ... + 2 + 1).] b) 1,2, 3,2,5, 2, 7,2,3,2,11 , 2,13,2, ... 16. Determine whether each of these integers is prime, veri­ c) 1 , 2, 2,3,2, 4, 2,4,3,4, 2,6, 2,4, .. . fYing some of Mersenne's claims. d) 1, 1 , 1 , 0, 1 , 1 , 1 , 0, 0, I , 1 , 0, 1 , 1 , ... a) 27 - I b) 29 - 1 e) 1,2,3,3,5, 5, 7,7, 7, 7, 11, II, 13, 13, ... c) 211 - I d) 213 - 1 1) 1,2,6, 30, 210,2310, 30030, 5105 10,9699690, The value of the Euler q,-function at the positive integer is 223092870, ... definedto be the number of positive integers less than or equaln 30. Can you find a formula or rule for the nth term of a se­ to that are relatively prime to n. ¢ is the Greek letter quence related to the prime numbers or prime factoriza­ phin.) (Note: tions so that the initial terms of the sequence have these 17. Find values? a) ¢(4). b) ¢(IO). c) ¢(13). a) 2, 2,3,5, 5, 7, 7,11 , 11, ll, ll, 13, 13, ... 18. Show that n is prime if and only if ¢(n) = n - I. b) 0, 1,2,2,3, 3,4,4,4,4,5,5,6,6, ... c) 1 , 0, 0,1,0, 1,0, I, 1,0, 1,0, I, ... 19. What is the value of ¢(pk) when P is prime and k is a d) 1, -1, 0, -1, 1,I, -1, 0, 0, 1, -1, 0, -I, I, 1 , ... positive integer? e) I , -I,I , 1, 1 , 1 , 0, 1, 1 , 1 , 0, I , 0, 1, 0, 0, ... 20. What are the greatest common divisors of these pairs of 1) 4, 9, 25,49, 121, 169, 289, 361, 529, 841,961, integers? 1369, ... 2 . 2 a) 2 33 . 55, 25 . 33 . 5 31. Prove that the product of any three consecutive integers b) 2·3 · 5 · 7· 11 . 13, 21 1 . 39 • 11 . 1714 is divisible by 6. 17, 1717 22.7, 53·13 c) d) 32. Show that if a, b, and m are integers such that m :::: 2 and e) 0, 5 1) 2·3·5·7, 2·3 . 5 ·7 a == b (mod m), then gcd(a, m) = gcd(b, m). 21. What are the greatest common divisors of these pairs of *33. Prove or disprove that - 79n + 1601 is prime when­ integers? ever is a positive integer.n2 . 11 . 5 . a) 37 ·53 73, 2 3 59 34. Proven or disprove that P P ...P + I is prime for every . . . 5 . I 2 n b) 11 · 13 17, 29 37 5 73 positive integer where P , P , ... , P are the smallest 1 1 I 2 n c) 233 , 23 7 prime numbers.n, n d) 41 · 43 · 53, 41 ·43·53 35. Adapt the proof in the text that there are infinitely many e) 3 13 . 5 17, 212 . 721 1) 1111, 0 primes to prove that there are infinitely many primes of the form 4k + 3, where k is a nonnegative integer. 22. What is the least common multiple of each pair in Exercise Suppose that there are only finitely many such primes[Hint: 20? ql, q2 , ···, qn ,andconsiderthenumber4qlq2 " ·qn - I.] 23. What is the least common multiple ofeach pair in Exercise *36. Prove that the set of positive rational numbers is countable 21? by settingup a functionthat assigns to a rational number 24. Find gcd(lOOO, 625) and lcm(1000, 625) and verifY that P / q with gcd(p, q) = 1 the base II number formed from gcd(1000, 625) · lcm(1000, 625) = 1000 · 625. the decimal representation of P fo llowed by the base II 25. Find gcd(92928, 123552) and lcm(92928, 123552), and digit A, which corresponds to the decimal number 10, verifY that gcd(92928, 123552) . lcm(92928, 123552) = followed by the decimal representation of q . 92928 . 123552. First findthe prime factorizations *37. Prove that the set of positive rational numbers is of92928 and 12355[Hint:2.] countable by showing that the function K is a one­ 26. lEthe product of two integers is 27385271 1 and their great­ to-one correspondence between the set of positive est common divisor is 23345, what is their least common rational numbers and the set of positive integers l 2 , l 2 , multiple? K( / . 2a 2a .. 2a 2b -1 2b -1 .. 2b -1 'f m ) = P P • P q q • q , I I 2 s l 2 t 27. Show that if a and b are positive integers, then ab = where gcd(mn , = I and the prime-power factorizations l 2 , l 2 , n) = a a a = b b ... b gcd(a, b) · lcm(a, b). [Hint:Use the prime factorizations o mf an d n are m PI P2 .••P s an d n ql q2 qt . 3-63 3.6 Integers and Algorithms 229

EXAMPLE 12 Find the greatest common divisor of 414 and 662 using the Euclidean algorithm.

Solution: Successive uses of the division algorithm give:

662 = 414 . 1 + 248 414 = 248 . 1 + 166 248 = 166 . 1 + 82 166 = 82 ·2 + 2 82 = 2·41.

Hence, gcd(414, 662) = 2, because 2 is the last nonzero remainder.

The Euclidean algorithm is expressed in pseudocode in Algorithm 6.

ALGORITHM 6 The Euclidean Algorithm.

procedure gcd(a , b: positive integers) x := a y:=b while y =1= 0 begin r := x mod y x := y y:=r end (gcd(a, b) is x}

In Algorithm 6, the initial values of x and y are a and b, respectively. At each stage of the procedure, x is replaced by y, and y is replaced by x mod y, which is the remainder when x is divided by y. This process is repeated as long as y =1= O. The algorithm terminates when y = 0, and the value of x at that point, the last nonzero remainder in the procedure, is the greatest common divisor of a and b. We will study the time complexity of the Euclidean algorithm in Section 4.3, where we will show that the number of divisions required to find the greatest common divisor of a and b, where a ?: b, is O(log b).

Exercises

1. Convert these integers from decimal notation to binary 4. Convert these integers from binary notation to decimal notation. notation. a) 231 b) 4532 c) 97644 a) 1 lOll b) 10 1011 0101 2. Convert these integers from decimal notation to binary c) lOll 111O d) 00 0001 llll notation. 5. ConvertII these integers fromIII hexadecimal II notation to bi­ a) 321 b) 1023 c) 100632 nary notation. 3. Convert these integers frombinary notation to decimal a) 80E b) 135AB notation. c) ABBA d) DEFACED a) 1 1 b) 10 0000 0001 6. Convert (BADFACED)16 fromits hexadecimal expansion c) 10III101 0101 d) 110 1001 0001 0000 to its binary expansion. 230 3 / The Fundamentals: Algorithms, the Integers, and Matrices 3-64

7. Convert (ABCDEF)16 from its hexadecimal expansion to 28. It can be shown that every integer can be uniquely repre­ its binary expansion. sented in the form 8. Convert each of these integers from binary notation to e 3k e _ 3k-1 e 3 eo, hexadecimal notation. k + k 1 + ... + l + a) 1111 0111 where ej = -I, O, or 1 for j = 0, 1,2, Expansions b) 1010 1010 1010 ofthis type are called balanced ternary... expansions. , k. Find c) 111 0111 0111 0111 the balanced ternaryexpansions of a) 5. b) 13. c) 37. d) 79. 9. Convert (1011 0111 IOIlh from its binary expansion to its hexadecimal expansion. 29. Show that a positive integer is divisible by 3 if and only if the sum of its decimal digits is divisible by 3. 10. Convert (1 1000 0110 0011h from its binary expansion to its hexadecimal expansion. 30. Show that a positive integer is divisible by 11 ifand only if the difference of the sum of its decimal digits in even­ 11. Show that the hexadecimal expansion of a positive integer numbered positions and the sum of its decimal digits in can be obtained from itsbinary expansion by grouping to­ odd-numbered positions is divisible by 11. gether blocks offour binary digits, adding initial digits if necessary, and translating each block of four binary digits 31. Show that a positive integer is divisible by 3 if and only into a single hexadecimal digit. if the difference of the sum of its binary digits in even­ numbered positions and the sum of its binary digits in 12. Show that the binary expansion of a positive integer can odd-numbered positions is divisible by 3. be obtained from its hexadecimal expansion by translat­ ing each hexadecimal digit into a block of four binary One's complement representations of integers are used to digits. simplifycomputer arithmetic. To represent positive and nega­ tive integers with absolute value less than 2n-l, a total ofn bits 13. Give a simple procedure for converting from the binary is used. The leftmostbit is used to represent the sign. A 0 bit expansion of an integer to its octal expansion. in this position is used for positive integers, and a 1 bit in this 14. Give a simple procedure for converting from the octal position is used for negative integers. For positive integers, expansion of an integer to its binary expansion. the remaining bits are identical to the binary expansion ofthe 15. Convert (7345321)8 to its binary expansion and (10 10 11 integer. For negative integers, the remaining bits are obtained 1011)2 to its octal expansion. by first finding the binary expansion of the absolute value of 16. Give a procedure for converting fromthe hexadecimal ex­ the integer, and then taking the complement of each of these pansion of an integer to its octal expansion using binary bits, where the complement of a I is a 0 and the complement notation as an intermediate step. ofa 0 is a 1. 17. Give a procedure for converting fromthe octal expansion 32. Find the one's complement representations, using bit of an integer to its hexadecimal expansion using binary strings oflength six, ofthe fo llowing integers. notation as an intermediate step. a) 22 b) 31 c) -7 d) -19 18. Convert (12345670)8 to its hexadecimal expansion and 33. What integer does each ofthe fo llowing one's complement (ABB093BABBA)16 to its octal expansion. representations of length five represent? 19. Use Algorithm 5 to find7644 mod 645. a) 11001 b) 01101 20. Use Algorithm 5 to find 11644 mod 645. c) 10001 d) 11111 21. Use Algorithm 5 to find3 2003 mod 99. 34. If m is a positive integer less than 2n-l, how is the one's 22. Use Algorithm 5 to find123 1001 mod 101. complement representation of -m obtained fr om the one's 23. Use the Euclidean algorithm to find complement ofm, when bit strings oflength n are used? 35. a) gcd(12, 18). b) gcd(111,201). How is the one's complement representation of the sum c) gcd(1001, 133 1). d) gcd(12345, 54321). of two integers obtained fromthe one's complement rep­ e) gcd(1000, 5040). f) gcd(9888, 6060). resentations of these integers? 36. How is the one's complement representation ofthe differ­ 24. Use the Euclidean algorithm to find ence of two integers obtained from the one's complement a) gcd(I, 5). b) gcd(100, 101). representations ofthese integers? c) gcd(123, 277). d) gcd(1529, 14039). 37. Show that the integer m with one's complement represen­ e) gcd(1529, 14038). f) gcd(l1 I 11, 111111). tation an-I an-2 ...a l ao can be found using the equation 25. How many divisions are required to findgcd(2 1 , 34) using ( n-1 ) n 2 m = -an_I 2 - 1) an_22 - al · 2 ao. the Euclidean algorithm? ( Two's complement representations+ of integers+ ...+ are also+ used 26. How many divisions are required to findgcd (34, 55) using to simplifycomputer arithmetic and are used more commonly the Euclidean algorithm? than one's complement representations. To represent an in­ 1 1 27. Show that every positive integer can be represented teger x with _2n- :::: x :::: 2n- - 1 for a specified positive uniquely as the sum of distinct powers of 2. [Hint: Con­ integer n, a total of n bits is used. The leftmost bit is used to sider binary expansions of integers.] represent the sign. A 0 bit in this position is used for positive 231 3-65 3.7 Applications of NumberTheory integers, and a 1 bit in this position is used for negative inte­ 46. Find the Cantor expansions of gers, just as in one's complement expansions. For a positive a) 2. b) integer, the remaining bits are identical to the binary expan­ c) 19. d) 7. sion of the integer. For a negative integer, the remaining bits e) 1000. t) 87.1,000,000. are the bits of the binary expansion of2n-1 - Ix I. Two 's com­ plement expansions of integers are often used by computers *4 7. Describe an algorithm that finds theCantor expansion of because addition and subtraction of integers can be performed an integer. easily using these expansions, where these integers can be *4 8. Describe an algorithm to add two integers from their either positive or negative. Cantor expansions. 38. Answer Exercise 32, but this time findthe two's comple­ 49. Add(10111)2 and (11010)2 by working through each step ment expansion using bit strings of length six. of the algorithm for addition given in the text. 39. Answer Exercise 33 if each expansion is a two's comple- 50. Multiply (11 1O)z and (101O)z by working through each ment expansion of length five. step of the algorithm for multiplication given in the 40. Answer Exercise 34 for two's complement expansions. text. 41. Answer Exercise 35 for two's complement expansions. 51. Describe an algorithm for finding the difference of two 42. Answer Exercise 36 for two's complement expansions. binary expansions. 43. Show that the integer m with two's complement represen­ 52. Estimate the number ofbit operations used to subtract two tation (an-l an-2 ...a l aO) can be found using the equation binary expansions. m = -a ' 2n-1 a _ 2n-2 al ·2 ao. n-I n 2 53. Devise an algorithm that, given the binary expansions of 44. Give a simple algorithm+ for fo+rming ... + the two's+ comple­ the integers a and b, determines whether a > b, a b, ment representation of an integer from its one's comple­ = or a < b. ment representation. 54. 45. Sometimes integers are encoded by using four-digit How many bit operations does the comparison algorithm binary expansions to represent each decimal digit. This fromExercise 53 use when the larger of a and b has n bits in its binary expansion? produces the binary coded decimal form of the in­ teger. For instance, is encoded in this way by 55. Estimate the complexity of Algorithm 1 for finding the 011110010001. How many791 bits are required to represent a base b expansion of an integer n in terms of the number number with n decimal digits using this type of encoding? of divisions used. A Cantor expansion is a sum ofthe form *56. Show that Algorithm 5 uses O((log mi 10g n) bit opera­ tions to findb n mod m. ann! + an l en - 1)! a 2! al l!, - + ... + 2 + 57. Show that Algorithm 4 uses O(q log a) bit operations, where ai is an integer with 0 ::: ai ::: i for i = 1 , 2, ..., n. assuming that a > d.

3.7 Applications of Number Theory

Introduction

Number theory has many applications, especially to computer science. In Section 3.4 we de­ scribed several of these applications, including hashing functions, the generation of pseudo­ random numbers, and shift ciphers. This section continues our introduction to number theory, developing some key results and presenting two important applications: a method for perform­ ing arithmetic with large integers and a recently invented type of cryptosystem, called a public key system. In such a cryptosystem, we do not have to keep encryption keys secret, because knowledge of an encryption key does not help someone decrypt messages in a realistic amount of time. Privately held decryption keys are used to decrypt messages. Before developing these applications, we will introduce some key results that play a central role in number theory and its applications. For example, we will show how to solve systems of linear congruences modulo pairwise relatively prime integers using the Chinese Remainder Theorem, and then show how to use this result as a basis for performing arithmetic with large integers. We will introduce Fermat's Little Theorem and the concept of a pseudoprime and will show how to use these concepts to develop a public key cryptosystem. 244 3 / The Fundamentals: Algorithms, the Integers, and Matrices 3-78

finding a factorization of n, or that does not also lead to the factorization of n. Factorization is believed to be a difficult problem, as opposed to finding large primes p and q, which can be done quickly. The most efficient factorization methods known (as of 2005) require billions of years to factor 400-digit integers. Consequently, when p and q are 200-digit primes, messages n = encrypted using pq as the modulus cannot be found in a reasonable time unless the primes p and q are known. Active research is under way to find new ways to efficiently factor integers. Integers that were thought, as recently as several years ago, to be far too large to be factored in a reasonable amount of time can now be factored routinely. Integers with more than 100 digits, as well as some with more than 150 digits, have been factored using team efforts. When new factorization techniques are found, it will be necessary to use larger primes to ensure secrecy of messages. Unfortunately, messages that were considered secure earlier can be saved and subsequently n = decrypted by unintended recipients when it becomes fe asible to factor the pq in the key used for RSA encryption. The RSA method is now widely used. However, the most commonly used cryptosystems are private key cryptosystems. The use of public key cryptography, via the RSA system, is growing. However, there are applications that use both private key and public key systems. For example, a public key cryptosystem, such as RSA, can be used to distribute private keys to pairs of individuals when they wish to communicate. These people then use a private key system fo r encryption and decryption of messages.

Exercises

1. Express the greatest common divisor ofeach ofthese pairs 14. a) Show that the positive integers less than II, except of integers as a linear combination of these integers. 1 and 10, can be split into pairs of integers such that a) 10, II b) 21, 44 c) 36, 48 each pair consists of integers that are inverses of each 34, 55 e) 117, 213 t) 0, 223 other modulo 11. d)g) 123, 2347 h) 3454, 4666 i) 9999, 11111 b) Use part (a) to show that 1O! == -1 (mod 11).

2. Express the greatest common divisor of each ofthese pairs 15. Show that if p is prime, the only solutions of x2 == of integers as a linear combination of these integers. 1 (mod p) are integers x such that x == 1 (mod p) and a) 9, 11 b) 33, 44 c) 35, 78 x == -1 (mod p). 21, 55 e) 101, 203 t) 124, 323 *16. a) Generalize the result in part (a) of Exercise 14; that g)d) 2002, 2339 h) 3457, 4669 i) 10001, 13422 is, show that if p is a prime, the positive integers less 3. Show that 15 is an inverse of7 modulo 26. thanp, except 1 and p - 1, can be split into (p - 3)/2 4. Show that 937 is an inverse of 13 modulo 2436. pairs of integers such that each pair consists of integers 5. Find an inverse of 4 modulo 9. that are inverses of each other. Use the result of Exercise 15.] [Hint: 6. Find an inverse of 2 modulo 17. b) From part (a) conclude that (p - I)! -1 (mod p) 7. Find an inverse of 19 modulo 141. == whenever p is prime. This result is known as Wilson's 8. Find an inverse of 144 modulo 233. Theorem. *9. Show that if a and m are relatively prime positive inte­ c) What can we conclude if n is a positive integer such gers, then the inverse of a modulo m is unique modulo that (n - I)! ¥= -1 (mod n)? m. Assume that there are two solutions b and e of *17. This exercise outlines a proof of Fermat's Little Theorem. the [Hcongruenceint: ax == 1 (mod m). Use Theorem 2 to show that b == e(mod m).] a) Suppose that a is not divisible by the prime p. Show . )a 10. Show that an inverse of a modulo m does not exist if that no two of the integers 1 a, 2 · a, ... , (p - l gcd(a, m) > 1. are congruent modulo p. b) Conclude from part (a) that the product of 11. Solve the congruence 4x 5 (mod 9). == 2, ..., p - I is congruent modulo p to the prod­ 12. Solve the congruence 2x == 7 (mod 17). uctI, of a, 2a, ..., (p - I)a. Use this to show that *13. Show that if m is a positive integer greater than 1 and ae == be (mod m), then a == b mod m/gcd(e, m). (p - I)! == aP-1 (p - I)! (mod pl. 3-79 3.7 Applications of Number Theory 245

c) Use Theorem 2 to show from part (b) that aP-1 == mainder Theorem to find 3302 mod 385. (Note that 1 (mod p) if P fa. Use Lemma 2 to show that 385 = 5 . 7 . 11.) P does not divide (p[H -int:1)1 and then use Theorem 2. 29. a) Use Fermat's Little Theorem to compute 52003 mod 7, Alternatively, use Wilson's Theorem.] 52003 mod 11, and 52003 mod 13. d) Use part (c) to show that a p == a (mod p) for all inte­ b) Use your results from part (a) and the Chinese Re­ gers a. mainder Theorem to find 52003 mod 1001. (Note that . . 18. Find all solutions to the system ofcongruen ces. 1001 = 7 11 13.) I3rLet n be a positive integer and let n - 1 = 2S , where s is a x == 2 (mod 3) nonnegative integer and is an odd positive integer.t We say x == 1 (mod 4) that n passes Miller's testt for the base b if either == 1 (mod x == 3 (mod 5) Y n) or b2it -1 (mod n) for some j with 0 :::: j :::: s - 1. It 19. Find all solutions to the system of congruences. == can be shown (see [Ro05]) that a composite integer n passes x == 1 (mod 2) Miller's test for fewer than n/4 bases b with 1 < b < n. x == 2 (mod 3) *30. Show that if n is prime and b is a positive integer with x == 3 (mod 5) n f b, then n passes Miller's test to the base b. x == 4 (mod 11) *20. Find all solutions, if any, to the system of congruences. 31. Show that 2047 passes Miller's test to the base 2, but that it is composite. A composite positive integer n that passes 6) x == 5 (mod Miller's test to the base b is called a strong pseudoprime x == 3 (mod 10) to the base b. 1t fo llows that 2047 is a strong pseudoprime x == 8 (mod 15) to the base 2. *21. Find all solutions, if any, to the system ofcongruences. 32. Show that 1729 is a Carmichael number. x == 7 (mod 9) 33. Show that 2821 is a Carmichael number. x == 4 (mod 12) *34. Show that if n = PI P2 ...P k. where PI , P2 , ... , Pk x == 1 6 (mod 21) are distinct primes that satisfy P j - 1 I n - 1 for j = 22. Use the Chinese Remainder Theorem to show that an 1,2, ... ,k, then n is a Carmichael number. integer a, with 0 :::: a < m mlm '" mn, where the = 2 35. a) Use Exercise 34 to show that every integer ofthe form integers ml, m ," " mn are pairwise relatively prime, 2 (6m 1)(12m 1)(18m 1), where m is a positive can be represented uniquely by the n-tuple (a m I, mod integer+ and 6m+ 1, 12m+ 1, and 18m 1 are all a m , ... , a mn) mod 2 mod . primes, is a Carmichael+ number.+ + *23. Let m I, m2 , ... ,mn be pairwise relatively prime integers b) Use part (a) to show that 172,947,529 is a Car­ greater than or equal to 2. Show that if a == b (mod mi l michael number. for 1 , 2, ... , n, then a b(mod m), where m = == = 36. Find the nonnegative integer a less than 28 represented by m 1 mi ...m n' (This result will be used in Exercise 24 to 2 each ofthese pairs, where each pair represents (a 4, prove the Chinese remainder theorem. Consequently, do mod a 7). not use the Chinese remainder theorem to prove it.) mod (0, 0) (1, 0) c) (1, 1) *24. Complete the proof of the Chinese Remainder Theorem a) b) (2, 1) (2, 2) f) (0, 3) by showing that the simultaneous solution of a system d) e) (2, 0) (3, 5) (3, 6) of linear congruences modulo pairwise relatively prime g) h) i) integers is unique modulo the product of these moduli. 37. Express each nonnegative integer a less than 15 using the Assume that x andy are two simultaneous solu­ pair (a mod 3, a mod 5). tions.[Hint: Show that mi I x - y for all j. Using Exercise 23, 38. Explain how to use the pairs found in Exercise 37 to add conclude that m = mlm2 ...mn I x - y.] 4 and 7. 25. Which integers leave a remainder of 1 when divided by 2 39. Solve the system of congruences that arises in Example 8. and also leave a remainder of 1 when divided by 3? *40. Show that whenever a and b are both positive integers, 26. Which integers are divisible by 5 but leave a remainder of then (2a - 1) mod (2b - 1) = 2a mod b - 1. 1 when divided by 3? **41. Use Exercise 40 to show that if a and b are pos­ b b 27. a) Show that 2340 == 1 (mod 11) by Fermat's LittleThe­ itive integers, then gcd(2a - 1 , 2 - 1) = 2gcd(a, ) - 1. orem and noting that 2340 = (210)34. [Hint: Show that the remainders obtained when the Eu­ b b) Show that 2340 == 1 (mod 31) using the fact that clidean algorithm is used to compute gcd(2a - 1 , 2 - 1) = 8 8 2340 (25)6 = 326 . are of the form 2r - 1, where r is a remainder arising c) Conclude from parts (a) and (b) that 2340 == when the Euclidean algorithm is used to findgcd(a , b).] 1 (mod 341). 42. Use Exercise 41 to show that the integers 235 - 1,234 - 1, 9 28. a) Use Fermat's Little Theorem to compute 3302 mod 5, 233 - 1, 231 - 1, 22 - 1, and 223 - 1 are pairwise rela­ 3302 mod 7, and 3302 mod 11. tively prime. b) Use your results from part (a) and the Chinese Re- 43. Show that if P is an odd prime, then every divisor of the 246 3 / The Fundamentals: Algorithms, the Integers, and Matrices 3-80

Mersenne number 2P is ofthe form 2kp + 1, where k either no solutions or exactly two incongruent solutions is a nonnegative integer.- I [Hint: Use Fermat's Little The­ modulo p. orem and Exercise 41.] 54. Show that if p is an odd prime, then there are exactly 13 44. Use Exercise 43 to determine whether M13 = 2 - 1 = (p - 1)/2 quadratic residues of p among the integers 23 8191 and M23 = 2 - 1 = 8,388,607 are prime. 1,2, ..., p - 1. *45. Show that we can easily factor n when we know that n If p is an odd prime and a is an integer not divisible by p, the is the product of two primes, p and q, and we know the value of(p - 1)(q - 1). Legendre symbol (�) is definedto be 1 if a is a quadratic 46. Encrypt the message ATTACK using the RSA system with residue of p and -1 otherwise. n 43 . 59 and e 13, translating each letter into inte­ = = 55. Show that if p is an odd prime and a and b are integers gers and grouping together pairs of integers, as done in with a b (mod p), then Example 11. == 47. What is the original message encrypted using the RSA system with n = 43 . 59 and e = 13 ifthe encrypted mes­ sage is 0667 1947 0671? (Note: Some computational aid is needed to do this in a realistic amount oftime.) 56. Prove that if p is an odd prime and a is a positive integer The extended Euclidean algorithm can be used to express not divisible by p, then gcd(a, b) as a linear combination with integer coefficients -I 2 of the integers a and b. We set So = 1, SI = 0, to = 0, and (�) == a(p )/ (mod p). tl = 1 and let sj = Sj-2 - qj_ISj_1 and tj = tj-2 - qj_l tj_1 for j = 2, 3, ... , n, where the qj are the quotients in the divi­ 57. Use Exercise 56 to show that if p is an odd prime and a sions used when the Euclidean algorithm findsgcd(a , b) (see and b are integers not divisible by p, then page 228). It can be shown (see [Ro05]) that gcd(a, b) = Sna + tn b. = 48. Use the extended Euclidean algorithm to express gcd(252, 356) as a linear combination of252 and 356. (�) (�)(%). 58. Show that if p is an odd prime, then -1 is a quadratic 49. Use the extended Euclidean algorithm to express residue of p if p == 1 (mod 4), and -1 is not a quadratic gcd(144, 89) as a linear combination of 144 and 89. residue of p if p == 3 (mod 4). [Hint: Use Exercise 56.] 50. Use the extended Euclidean algorithm to express 2 59. Find all solutions of the congruence x == 29 (mod 35). gcd(1001, 100001) as a linear combination of 1001 and [Hint: Find the solutions of this congruence modulo 5 100001. and modulo 7, and then use the Chinese Remainder 51. Describe the extended Euclidean algorithm using Theorem.] pseudocode. 2 60. Find all solutions ofthe congruence x == 16 (mod 105). If m is a positive integer, the integer a is a quadratic residue [Hint: 2 Find the solutions of this congruence modulo 3, of m if gcd(a, m) = 1 and the congruence x == a (mod m) modulo 5, and modulo 7, and then use the Chinese Re­ has a solution. In other words, a quadratic residue ofm is an mainder Theorem.] integer relatively prime to m that is a perfect square modulo *61. Explain why it would not be suitable to use p, where m. For example, 2 is a quadratic residue of 7 because gcd(2, p is a large prime, as the modulus for encryption in 7) 1 and 32 2 (mod 7) and 3 is a quadratic nonresidue of = == 2 the RSA cryptosystem. That is, explain how someone 7 because gcd(3, 7) = 1 and x == 3 (mod 7) has no solution. could, without excessive computation, find a private key 52. Which integers are quadratic residues of 11? from the corresponding public key if the modulus were 53. Show that if p is an odd prime and a is an integer not a large prime, rather than the product of two large 2 divisible by p, then the congruence x == a (mod p) has primes.

3.8 Matrices

Introduction

Matrices are used throughout discrete mathematics to express relationships between elements in sets. In subsequent chapters we will use matrices in a wide variety of models. For instance, matrices will be used in models of communications networks and transportation systems. Many algorithms will be developed that use these matrix models. This section reviews matrix arithmetic that will be used in these algorithms. 3-93 Supplementary Exercises 259

b) Express gcd(84, 119) as a linear combination of 84 20. a) What is the difference between a public key and a and 119. private key cryptosystem? Explain why using shift ciphers is a private key 16. a) What does it mean for a to be an inverse of a modulo b) m? system. How can you findan inverse of a modulo m when m c) Explain why the RSA cipher system is a public key b) system. is a positive integer and gcd(a, m) = I? Find an inverse of7 modulo 21. Definethe product oftwo matrices A and B. When is this c) 19. product defined? 17, a) How can an inverse of a modulo m be used to solve the 22. a) How many different ways are there to evaluate the congruence ax ==b (mod m) when gcd(a, m) = I? Solve the linear congruence 7x 3 (mod product AIA2A3A! by successively multiplying pairs b) == 1 19). of matrices, when this product is defined? State the Chinese Remainder Theorem. 18. a) b) Suppose that AI , A2, A3, and A!are 10 x 20, 20 x 5, b) Find the solutions to the system x == 1 (mod 4), 5 x 10, and lO x 5 matrices, respectively. How should x == 2 (mod 5), and x == 3 (mod 7). AIA A A! be computed to use the least number of n-1 2 3 19. Suppose that 2 == 1 (modn). Is n necessarily prime? multiplications of entries?

Supplementary Exercises

1. a) Describe an algorithm for locating the last occurrence paring them with the maximum and minimum of the of the largest number in a list of integers. pair of elements being examined. b) Estimate the number of comparisons used. b) Express the algorithm described in part (a) in pseudo­ code. 2. a) Describe an algorithm for findingthe firstand second largest elements in a list of integers. c) How many comparisons of elements of the sequence are carried out by this algorithm? (Do not count com- b) Estimate the number of comparisons used. o pari sons used to determine whether the end of the se­ 3. a) Give an algorithm to determine whether a bit string quence has been reached.) How does this compare to contains a pair of consecutive zeros. the number of comparisons used by the algorithm in b) How many comparisons does the algorithm use? Exercise 5? Suppose that a list contains integers that are in order 4. a) *7. Show that the worst-case complexity in terms of compar­ oflargest to smallest and an integer can appear repeat­ isons of an algorithm that finds the maximum and mini­ edly in this list. Devise an algorithm that locates all mum ofn elements is at least f3n/21 - 2. occurrences of an integer x in the list. 8. Devise an efficient algorithm for finding the second b) Estimate the number of comparisons used. largest element in a sequence of n elements and deter­ 5. a) Adapt Algorithm 1 in Section 3.1 to find the maxi­ mine the worst-case complexity of your algorithm. mum and the minimum of a sequence of n elements The shaker sort (or bidirectional bubble sort) successively by employing a temporary maximum and a temporary compares adjacent pairs of elements, exchanging them ifthey minimum that is updated as each successive element are out of order, and alternatelypassing through the list from is examined. the beginning to the end and then fromthe end to the beginning b) Describe the algorithm frompart (a) in pseudocode. until no exchanges are needed. c) How many comparisons of elements in the sequence are carried out by this algorithm? (Do not count com­ 9. Show the steps used by the shaker sort to sort the list 3, parisons used to determine whether the end of the se­ 5, 1,4,6,2. quence has been reached.) 10. Express the shaker sort in pseudocode. 2 6. a) Describe in detail the steps of an algorithm that finds 11. Show thatthe shaker sort has 0 (n ) complexity measured the maximum and minimum of a sequence of n el­ in terms of the number of comparisons it uses. ements by examining pairs of successive elements, 12. Explain why the shaker sort is efficient for sorting lists keeping track of a temporary maximum and a tempo­ that are already in close to the correct order. rary minimum. If n is odd, both the temporary maxi­ 2 3 13. Show that (n log n + n ) is O(n6). mum and temporary minimum should initially equal 3 3 14. Show that 8x + 12x + 100 log x is O(x ). the firstterm, and if n is even, the temporary minimum 2 3 . 3 and temporary maximum should be found by compar­ 15. Give a big-O estimate for (x + x(logx) ) (2X + x ). ing the initial two elements. The temporary maximum 16. Find a big-O estimate for J=1 JU + 1). n 2: and temporary minimum should be updated by com- *17. Show that n! is not O(2 ). 260 3 / The Fundamentals: Algorithms, the Integers, and Matrices 3-94

n 18. Show that n is not O(n I). 35. Find all solutions of the system of congruences x == 19. Find four numbers congruent to 5 modulo 17. 4 (mod 6) and x == 13 (mod IS). 20. Show that if a and d are positive integers, then there are *36. a) Show that the system of congruences x == al (mod m I) integers q and r such that a = d q + r where -d 12 < r ::::: and x == a2 (mod m2) has a solution if and only if d12. gcd(ml ,m2) I al -a2· 1. Show that if ae == be (mod m), then a == b (mod mid), b) Show that the solution in part (a) is unique modulo where d = gcd(m, e). lcm(ml, m2). �2. How many zeros are at the end of the binary expansion *37. Prove that if f(x) is a nonconstant polynomial with inte­ of IOOIO!? ger coefficients,then there is an integer y such that f(y) is 23. Use the Euclidean algorithm to findthe greatest common composite. [Hint: Assume that f(xo) = p is prime. Show divisor of 10,223 and 33,341. that p divides f(xo + kp) for all integers k. Obtain a con­ 24. How many divisions are required to find gcd(144, 233) tradiction ofthe fact that a polynomial of degree n, where using the Euclidean algorithm? n > 1, takes on each value at most n times.] 25. Find gcd(2n + I , 3n + 2), where n is a positive integer. * *38. Show that if a and b are positive irrational numbers such [Hint: Use the Euclidean algorithm.] that 1 I a + 1 I b = 1, then every positive integer can be 26. a) Show that if a and b are positive integers with uniquely expressed as either L ka J or L kb J for some posi­ a ::: b, then gcd(a, b) = a if a = b, gcd(a, b) = 2 tive integer k. gcd(aI2, b12) if a and b are even, gcd(a, b) = 39. Show that Goldbach's conjecture, which states that every gcd(aI2, b) if a is even and b is odd, and gcd(a, b) = even integer greater than 2 is the sum of two primes, is gcd(a - b, b) if both a and b are odd. equivalent to the statement that every integer greater than b) Explain how to use (a) to construct an algorithm for 5 is the sum ofthree primes. computing the greatest common divisor of two posi­ Prove that there are no solutions in integers x and y to 40. 2 2 tive integers that uses only comparisons, subtractions, the equation x - 5 y = 2. [Hint: Consider this equation and shifts of binary expansions, without using any modulo 5.] divisions. n 41. Find A if A is c) Find gcd(1202, 4848) using this algorithm. 27. Show that an integer is divisible by 9 if and only if the sum of its decimal digits is divisible by 9. 28. Prove that if n is a positive integer such that the sum of 42. Show that if A = cI, where e is a real number and I is the the divisors of n is n + I, then n is prime. n x n identity matrix, then AB = BA whenever B is an 29. Adapt the proofthat there are infinitelymany primes (The­ n x n matrix. orem 3 in Section 3.5) to show that are infinitelymany 43. Show that ifA is a 2 x 2 matrix such that AB = BA when­ primes ofthe form 6k + 5 where k is a positive integer. ever B is a 2 x 2 matrix, then A = cI, where e is a real 30. a) Explain why n div 7 equals the number of weeks in number and I is the 2 x 2 identity matrix. n days. An n x n matrix is called upper triangular if aij = 0 when­ b) Explain why n div 24 equals the number of days in ever i > j. n hours. 44. From the definitionof the matrix product, devise an algo­ A set of integers is called mutually relatively prime if the rithm for computing the product of two upper triangular greatest common divisor of these integers is 1. matrices that ignores those products in the computation 31. Determine whether these sets of integers are mutually rel­ that are automatically equal to zero. atively prime. 45. Give a pseudocode description of the algorithm in Exer­ a) 8, 10, 12 b) 12, 15, 25 cise 44 for multiplying two upper triangular matrices. c) 15, 21,28 d) 21,24, 28, 32 46. How many multiplications of entries are used by the al­ 32. Find a set of four mutually relatively prime integers such gorithm found in Exercise 44 for multiplying two n x n that no two of them are relatively prime. upper triangular matrices? 33. a) Suppose that messages are encrypted using the func­ 47. Show that if A and B are invertible matrices and AB exists, then (AB)-I B-1A -I tion f(p) = (ap + b) mod 26 such that gcd(a, 26) = = . 1. Determine a function that can be used to decrypt 48. What is the best order to form the product ABCD if messages. A, B, C, and D are matrices with dimensions 30x 10, b) The encrypted version of a message is LJMKG lOx 40, 40 x 50, and 50 x 30, respectively? Assume MGMXF QEXMW Ifit was encrypted using the func­ that the number of multiplications of entries used to mul­ tion f(p) = (7p + 10) mod 26, what was the original tiply a p x q matrixand a q x r matrix is pqr. message? 49. Let A be an n x n matrix and let 0 be the n x n matrix 34. Show that the system of congruences x == 2 (mod 6) and all of whose entries are zero. Show that the following are x == 3 (mod 9) has no solutions. true. 3-95 Computer Projects 261

A A a) <:) 0 = 0 <:) = 0 51. Devise an algorithm for guessing a number between 1 b) A A A n v 0 = 0 v = and 2 - 1 by successively guessing each bit in its binary A = A = expansion. c) /\ 0 0 /\ 0 o 50. Showthat ifthe denominations of coins are c , cl , ..., ck, 52. Determine the complexity, in terms of the number of where k is a positive integer and c is a positive integer, 1 guesses,n needed to determine a number between and c > 1, the greedy algorithm always produces change us­ 2 - 1 by successively guessing the bits in its binary ing the fewest coins possible. expansion.

Computer Projects

Write programs with these inputs and outputs.

1. Given a list of n integers, find thelargest integer in the 15. Given a positive integer and a positive integer b greater list. than 1, findthe base b expansion of this integer. 2. Given a list of n integers, find the first andlast occurrences 16. Given the positive integers a, b, and m > 1, find ab of the largest integer in the list. modm. 3. Given a list of n distinct integers, determine the position 17. Given a positive integer, findthe Cantor expansion of this of an integer in the list using a linear search. integer (see the preamble to Exercise 46 of Section 3.6). 4. Given an ordered list of n distinct integers, determine the 18. Given a positive integer n, a modulus m, multiplier a, position of an integer in the list using a binary search. increment c, and seed xo, with 0 .:s a < m, 0 .:s c < m, 5. Given a list of n integers, sort them using a bubble sort. and 0 .:s Xo < m, generate the sequence of n pseudo­ random numbers using the linear congruential generator 6. Given a list of n integers, sort them using an insertion sort. Xn+1 = (axn + c) mod m. 19. Given positive integers a and b, findintegers s and t such 7. Given an integer n, use the greedy algorithm to findthe change for n cents using quarters, dimes, nickels, and that sa + tb = gcd(a, b). pennies. 20. Given n linear congruences modulo pairwise relatively prime moduli, findthe simultaneous solution ofthese con­ 8. Given an ordered list of n integers and an integer x, find the number of comparisons used to determine the position gruences modulo the product of these moduli. of an integer in the list using a linear search and using a 21. Given an m x k matrix A and a k x n matrix B, find binary search. AB. A n 9. Given a list of integers, determine the number of compar­ 22. Given a square matrix and a positive integer n, findA . isons used by the bubble sort and by the insertion sort to 23. Given a square matrix, determine whether it is symmetric. sort this list. 24. Given an nl x n2 matrix A, an n2 x n3 matrix B, an 10. Given a positive integer, determine whether it is prime. n3 x n4 matriJi.C, and an n4 x n5 matrix D, all with inte­ 11. Given a message, encrypt this message using the Caesar ger entries, determine the most efficientorder to multiply cipher; and given a message encrypted using the Caesar these matrices (in terms of the number of multiplications cipher, decrypt this message. and additions of integers). 12. Given two positive integers, find their greatest common 25. Given twom x n Boolean matrices, findtheir meet and divisor using the Euclidean algorithm. join. 13. Given two positive integers, find their least common 26. Given an m x k Boolean matrix A and a k x n Boolean multiple. matrix B, findthe Boolean product of A and B. A *14. Given a positive integer, find the prime factorization of 27. Given a squaren Booleanmatrix and a positive integer this integer. n, findA [ ).

Computations and Explorations

Use a computational program or programs you have written to do these exercises. n 1. We know that nb is O(d ) when b and d are positive num­ 2. Compute the change for different values of n with d :::: 2. C k bers with n Give values ofthe constants and such coins of different denominations using the greedy al­ that nb .:s C d whenever x > k for each of these sets of gorithm and determine whether the smallest number values: b = 10, d = 2; b = 20, d = 3; b = 1000, d = 7. of coins was used. Can you find conditions so that the 4-17 4.1 Mathematical Induction 279

Note that it is sometimes difficultto locate the error in a faulty proofby mathematical induction, as Example 14 illustrates.

EXAMPLE 14 Find the error in this "proof" of the clearly false claim that every set of lines in the plane, no two of which are parallel, meet in a common point.

Let Pen) be the statement that every set ofn lines in the plane, no two of which are paral"Proolel,f": meet in a common point. We will attempt to prove that Pen) is true fo r all positive integers n ::: 2. BASIS STEP: The statement P(2) is true because any two lines in the plane that are not parallel meet in a common point (by the definition of parallel lines ). INDUCTIVE STEP: The inductive hypothesis is the statement that P(k) is true fo r the positive integer k, that is, it is the assumption that every set of k lines in the plane, no two of which are parallel, meet in a common point. To complete the inductive step, we must show that if P(k) is true, then P(k + 1) must also be true. That is, we must show that if every set of k lines in the plane, no two of which are parallel, meet in a common point, then every set of k + 1 lines in the plane, no two of which are parallel, meet in a common point. So, consider a set of k + 1 distinct lines in the plane. By the inductive hypothesis, the firstk of these lines meet in a common point PI . Moreover, by the inductive hypothesis, the last k of these lines meet in a common point P2 . We will show that PI and P2 must be the same point. If PI and P2 were different points, all lines containing both of them must be the same line because two points determine a line. This contradicts our assumption that all these lines are distinct. Thus, PI and P2 are the same point.

We conclude that the point PI = P2 lies on all k + 1 lines. We have shown that P(k + 1) is true assuming that P(k) is true. That is, we have shown that if we assume that every k, k ::: 2, distinct lines meet in a common point, then every k + 1 distinct lines meet in a common point. This completes the inductive step. We have completed the basis step and the inductive step, and supposedly we have a correct proof by mathematical induction.

Examining this supposed proof by mathematical induction it appears that everything Solution:is in order. However, there is an error, as there must be. The error is rather subtle. Carefully looking at the inductive step shows that this step requires that k ::: 3. We cannot show that P(2)

implies P(3). When k = 2, our goal is to show that every three distinct lines meet in a common point. The firsttwo lines must meet in a common point PI and the last two lines must meet in a common point P2. But in this case, PI and P2 do not have to be the same, because only the second line is common to both sets of lines. Here is where the inductive step fails. ....

Exercises

1. There are infinitely many stations on a train route. Sup­ a) What is the statement pel)? pose that the train stops at the first station and suppose b) Show that pel) is true, completing the basis step of that if the train stops at a station, then it stops at the next the proof. station. Show that the train stops at all stations. c) What is the inductive hypothesis? 2. Suppose that you know that a golfer plays the firsthole of d) What do you need to prove in the inductive step? a golf course with an infinitenumber of holes and that if e) Complete the inductive step. this golfer plays one hole, then the golfer goes on to play 1) Explain why these steps show that this formula is true the next hole. Prove that this golfer plays every hole on whenever n is a positive integer. the course. Let pen) be the statement that 13 + 23 + ... + n3 4. 2 = D se mathematical induction in Exercises 3-17 to prove sum­ (n(n + 1)/2) for the positive integer n. mation formulae. What is the statement pel)? 2 2 2 a) 3. Let pen) be the statement that 1 + 2 + ... + n = b) Show that pel) is true, completing the basis step of n(n + 1)(2n + 1)/6 for the positive integer n. the proof. 280 4 / Induction and Recursion 4-18

c) What is the inductive hypothesis? c) What is the inductive hypothesis? d) What do you need to prove in the inductive step? d) What do you need to prove in the inductive step? e) Complete the inductive step. e) Complete the inductive step. 1) Explain why these steps show that this formula is true 1) Explain why these steps show that this inequality is whenever n is a positive integer. true whenever n is an integer greater than 1. 2 2 2 2 5. Prove that 1 + 3 + 5 + ... + (2n + 1) = (n + I) 19. Let P(n) be the statement that (2n + 1)(2n + 3)/3 whenever n is a nonnegative integer. Prove that 1 1 . 1 1 6. 1·1!+2·2!+···+n ·n!=(n+I)!-1 1 + - + - + . . + -2 < 2 - whenever n is a positive integer. 4 9 n -,n 2 n n l 7. Prove that 3 + 3·5+ 3 . 5 + ...+ 3·5 =3(5 + - 1)/4 where n is an integer greater than 1. whenever n is a nonnegative integer. a) What is the statement P(2)? 8. Prove that 2 - 2 . 7 + 2 . 72 - ...+ 2(-7t = (1 - b) Show that P(2) is true, completing the basis step of (_7)n+1 )/4 whenever n is a nonnegative integer. the proof. c) What is the inductive hypothesis? 9. a) Find a formula for the sum of the firstn even positive integers. d) What do you need to prove in the inductive step? b) Prove the formula that you conjectured in part (a). e) Complete the inductive step. 1) Explain why these steps show that this inequality is 10. a) Find a formula for truewhenever n is aninteger greater than I . II I n - + - + ... + ----:--".,. 20. Prove that 3 < n! if n is an integer greater than 6. 1·2 2·3 n(n+I) n 2 21. Prove that 2 n if n is an integer greater than 4. by examining the values of this expression for small > 2 22. For which nonnegative integers n is n ::: n!? Prove your values ofn. answer. b) Prove the formula you conjectured in part (a). n 23. For which nonnegative integers n is 2n + 3 ::: 2 ? Prove 11. a) Find a formula for your answer. I I 1 1 -+- + - + ... + - 24. Prove that 1/(2n) ::: [1 ·3·5···· . (2n - 1)]/(2·4····· 2 4 8 2n 2n) whenever n is a positive integer. by examining the values of this expression for small n *25. Prove that if h > -1, then 1 + nh ::: (1 + h) for all non­ values ofn. negative integers n. This is called Bernoulli's inequality. b) Prove the formula you conjectured in part (a). *26. Suppose that a and b are real numbers with 0 < b < a. 12. Prove that Prove that if n is a positive integer, then an - bn ::: n I J nan-I (a - b). L ( --2 ) *27. Prove that for every positive integer n, J=O 1 1 1 whenever n is a nonnegative integer. 1 + - + - + ... + 2(.Jn+f - 1) 2 2 2 ... _ n-I 2 _ n-1 > . 13. Prove that 1 - 2 + 3 - + ( I) n = ( 1) .j2 y'3 -.jfi n(n + 1)/2 whenever n is a positive integer. 2 k 28. Prove that n - 7n + 12 is nonnegative whenever n is an 14. Prove that for every positive integer n, LZ = I k2 = integer with n 3 (n - I)2n+1 + 2. � . 15. Prove that for every positive integer n, In Exercises 29 and 30, Hn denotes the nth harmonic number. . *29. Prove that H2n ::: 1 + n whenever n is a nonnegative 1 ·2+ 2·3+ . . + n(n + 1) = n(n + I)(n + 2)/3. integer. 16. Prove that for every positive integer n, *30. Prove that 1 ·2·3 + 2·3·4 + . . . + n(n + 1)(n + 2) HI + H2 + . .. + Hn = (n + I)Hn - n. = n(n + I)(n + 2)(n + 3)/4. Use mathematical induction in Exercises 31-37 to prove di­ 4 2 17. Prove that LJ = l j = n(n + I)(2n + I)(3n +3n -I)/ visibility facts. 30 whenever n is a positive integer. 2 31. Prove that 2 divides n + n whenever n is a positive Use mathematical induction to prove the inequalities in integer. 3 Exercises 18-30. 32. Prove that 3 divides n + 2n whenever n is a positive n integer. 18. Let P(n) be the statement that n! < n , where n is an integer greater than 1 . 33. Prove that 5 divides n5 - n whenever n is a nonnegative What is the statement P(2)? integer. a) 3 b) Show that P(2) is true, completing the basis step of 34. Prove that 6 divides n - n whenever n is a nonnegative the proof. integer. 4-19 4.1 Mathematical Induction 281

2 '35. Prove that n - 1 is divisible by 8 whenever n is an odd Consider any k + 1 horses; number these as horses positive integer. 1,2,3, ...,k , k 1. Now the first k of these horses all n 1 2n-1 + '36. Prove that 21 divides 4 + + 5 whenever n is a posi­ must have the same color, and the last k of these must tive integer. also have the same color. Because the set of the first k horses and the set of the last k horses overlap, all k + 1 '37. Prove that if n is a positive integer, then 133 divides lln+1 + 122n-l must be the same color. This shows that P(k + 1) is true . and finishesthe proof by induction. Use mathematical induction in Exercises 38-46to prove re­ sults about sets. 48. What is wrong with this "proof" ? 38. Prove that if AI , A2, ..., An and BI , B2, ... , Bn are sets "Theorem " For every positive integer n, L:7= I i = 2 such that Aj � Bj for j = 1,2, ..., n, then (n + 1) /2. n n A B Basis Step: The formula is true for n = 1. U j � U j. j = 1 j = 1 2 Inductive Step: Suppose that L:7=1 i = (n + 1) /2. 39. Prove that if AI , A2, ..., An and BI , B2, ... , Bn are sets Then L:7:/ i = (L:7=1 i) + (n + 1). By the inductive 2 2 such that Aj � Bj for j = 1,2, ..., n, then hypothesis, 'L7:/ i = (n + 1) /2 + n + 1 = (n + n + n n �)/2 + n + 1 = (n2 + 3n + �)/2 = (n +�)2/2= Aj Bj. [en + 1) + 1]2/2, completing the inductive step. jn= 1 � jn= 1 49. What is wrong with this "proof" ? 40. Prove that if AI , A2, ..., An and B are sets, then "Theorem " For every positive integer n, if x and y are ... positive integers with max(x, y) = n, then x = y. (AI n A2 n nAn) U B = (AI U B) n (A2 U B) n ... n (An U B) . Basis Step: Suppose that n = 1. If max(x, y) = I and x 41. Prove that if A I, A2, ..., An and B are sets, then and y are positive integers, we have x = I and y = I . (AI U A2 U ... U An) B n .. Inductive Step: Let k be a positive integer. Assume that = (AI n B) U (A2 n B) U · U (An n B). whenever max(x, y) = k and x and y are positive inte­ 42. Prove that if AI , A2," " An and B are sets, then gers, then x = y. Now let max(x, y) = k + I, where x

.. · (AI - B) n (A2 - B) n n (An - B) and y are positive integers. Then max(x - I, Y - I) = k, .. · = (AI n A2 n n An) - B. so by the inductive hypothesis, x - I = Y -I. It follows 43. Prove that if AI , A2, ... , An are subsets ofa universal set that x = y, completing the inductive step. U, then 50. Use mathematical induction to show that given a set of n + I positive integers, none exceeding 2n, there is at n n least one integer in this set that divides another integer in U Ak = Ak· k= 1 k=n1 the set. *51. A knight on a chessboard can move one space horizon­ 44. Prove that if A I , A2, ... , An and B are sets, then tally (in either direction) and two spaces vertically (in (AI - B) U (A2 - B) U ... U (An - B) either direction) or two spaces horizontally (in either di­ = (AI U A2 U·.. U An) - B. rection) and one space vertically (in either direction). Suppose that we have an infinite chessboard, made up 45. Prove that a set with n elements has n(n - 1)/2 subsets containing exactly two elements whenever n is an integer of all squares (m , n) where m and n are nonnegative in­ greater than or equal to 2 tegers. Use mathematical induction to show that a knight . starting at (0, 0) can visit every square using a finite se­ *46. Prove that a set with n elements has n(n - I)(n - 2)/6 quence of moves. [Hini: Use induction on the variable subsets containing exactly three elements whenever n is s = m +n] an integer greater than or equal to 3. . Suppose that Exercises 47-49 present incorrect proofs using mathemati­ 52. cal induction. You will need to identify an error in reasoning in each exercise. A 47. What is wrong with this "proof" that all horses are the [� �l same color? where =and b are real numbers. Show that Let P(n) be the proposition that all the horses in a set of a

n horses are the same color. Basis Step: Clearly, P(I) is true. Inductive Step: Assume that P(k) is true, so that all the horses in any set of k horses are the same color. for every positive integer n. 282 4 / Induction and Recursion 4-20

53. (Requires calculus) Use mathematical induction to prove would know each other. A particular party may have no n n-I that the derivative of f(x) = x equals nx whenever celebrity. Yo ur assignment is to findthe celebrity, if one n is a positive integer. (For the inductive step, use the exists, at a party, by asking only one type of question­ product rule for derivatives.) asking a guest whether they know a second guest. Ev­ 54. Suppose that A and B are square matrices with the prop­ eryone must answer your questions truthfully.That is, if n n erty AB = BA. Show that AB = B A for every positive Alice and Bob are two people at the party, you can ask integer n. Alice whether she knows Bob; she must answer correctly. 55. Suppose that m is a positive integer. Use mathematical Use mathematical induction to show that if there are n people at the party, then you can findthe celebrity, ifthere induction to prove that if a and b are integers with a == b k k is one, with 3(n - 1) questions. [Hint: First ask a ques­ (mod m), then a == b (mod m) whenever k is a nonneg­ ative integer. tion to eliminate one person as a celebrity. Then use the inductive hypothesis to identifya potential celebrity. Fi­ 56. Use mathematical induction to show that -'(PI v P2 V nally, ask two more questions to determine whether that ... ) is equivalent to -'P -'P -'P when­ v Pn I /\ 2 /\ ... /\ n person is actually a celebrity.] ever Ph P2, ..., Pn are propositions. *57. Show that Suppose there are n people in a group, each aware of a scandal no one else in the group knows about. These people commu­ [( ] PI --+ P2) /\ (P2 --+ P3) /\ ... /\ (Pn-I --+ Pn) nicate by telephone; when two people in the group talk, they ] --+ [(PI /\ P2 /\ ... /\ Pn-I) --+ Pn share information about all scandals each knows about. For is a tautology whenever PI , P2, ..., Pn are propositions, example, on the first call, two people share information, so where n ::: 2. by the end of the call, each ofthese people knows about two *58. Show that n lines separate the plane into (n2 + n + 2)/2 scandals. The gossip problem asks for G(n), the minimum regions if no two of these lines are parallel and no three number of telephone calls that are needed for all n people to pass through a common point. learn about all the scandals. Exercises 65--67 deal with the gossip problem. **59. Let aI , a2, ..., an be positive real numbers. The arith­ metic mean of these numbers is definedby 65. Find G(l), G(2), G(3), and G(4). ... A = (al +az + +an)/n, 66. Use mathematical induction to prove that G(n) .:s 2n - 4 for n 4 [Hint: In the inductive step, have a new person and the geometric mean of these numbers is definedby ::: . l n call a particular person at the start and at the end.] G = (alaz ...a n) / . **67. Prove that G(n) = 2n - 4 for n ::: 4. Use mathematical induction to prove that A ::: G. 60. Use mathematical induction to prove Lemma 2 of Section *68. Show that it is possible to arrange the numbers I, 2, ..., n 3.6, which states that if P is a prime and P I ala ...a , in a row so that the average of any two of these numbers z n never appears between them. [Hint: Show that it suffices where ai is an integer for i = 1 ,2,3, ..., n, then pia i for some integer i to prove this fact when n is a power of 2. Then use math­ . ematical induction to prove the result when n is a power 61. Show that if n is a positive integer, then of2.] *69. Show that if II , /z, ..., In is a collection of open intervals on the real number line, n ::: 2, and every pair of these in­ tervals has a nonempty intersection, that is, Ii n Ij =1= 13 (Here the sum is over all nonempty subsets of the set of whenever 1 .:s i .:s n and 1 .:s j .:s n, then the intersection the n smallest positive integers.) of all these sets is nonempty, that is, II n /z n ... n In =1= *62. Use the well-ordering property to show that the following 13. (Recall that an open interval is the set of real numbers b form ofmathematical induction is a valid method to prove x with a < x < b, where a and are real numbers with b ) that P(n) is true for all positive integers n. a < . Basis Step: P(l) and P(2) are true. Sometimes we cannot use mathematical induction to prove Inductive Step: For each positive integer k, if P(k) and a result we believe to be true, but we can use mathematical P(k + 1) are both true, then P(k + 2) is true. induction to prove a stronger result. Because the inductive hy­ pothesis ofthe stronger result provides more to work with, this 63. Show that if A I , Az, ..., An are sets where n 2, and for ::: process is called inductive loading. We use inductive loading all pairs of integers i and j with 1 i < j n either A .:s .:s i in Exercise 70 is a subset of of Aj or Aj is a subset of Ai, then there is . 70. Suppose that we want to prove that an integer i, 1 .:s i .:s n such that Ai is a subset of Aj for all integers j with 1 .:s j .:s n. 1 3 2n - 1 I - . - ... --- -- *64. A guest at a party is a celebrity if this person is known 2 4 2n < by every other guest, but knows none ofthem. There is at v'3ii most one celebrity at a party, for if there were two, they for all positive integers n. 4-21 4.2 Strong Induction and Well-Ordering 283

a) Show that if we try to prove this inequality using math­ 74. Prove or disprove that al1 checkerboards of these shapes ematical induction, the basis step works, but the induc­ can be completely covered using right triominoes when­ tive step fails. ever n is a positive integer. b) Show that mathematical induction can be used to prove 3 x 2n 6 x 2n a) n n b) n n the stronger inequality 3 x 3 d) 6 x 6 c) n n n I 3 2n - I I *75. Show that a three-dimensional 2 x 2 x 2 checker­ -.-... -- < ---;:::== board with one 1 x 1 x 1 cube missing can be completely 2 4 2n ,J3ri+1 covered by 2 x 2 x 2 cubes with one 1 x 1 x 1 cube for al1 integers greater than 1, which, together with a removed. x verification for the case where n = I, establishes the *76. Show that an n n checkerboard with one square re­ weaker inequality we original1y tried to prove using moved can be completely covered using right triominoes mathematical induction. ifn > 5, n is odd, and 3 f n. 71. Let n be an even positive integer. Show that when n peo­ 77. Show that a 5 x 5 checkerboard with a comer square re­ ple stand in a yard at mutual1y distinct distances and each moved can be tiled using right triominoes. person throws a pie at their nearest neighbor, it is possible *78. Find a 5 x 5 checkerboard with a square removed that that everyone is hit by a pie. cannot be tiled using right triominoes. Prove that such a 72. Construct a tiling using right triominoes of the 4 x 4 tiling does not exist for this board. checkerboard with the square in the upper left comer 79. Use the principle of mathematical induction to show that removed. pen) is true for n = b, b + 1, b + 2, . .. , where b is an 73. Construct a tiling using right triominoes of the 8 x 8 integer, if PCb) is true and the conditional statement checkerboard with the square in the upper left comer P(k) ---+ P(k + 1) is true for al1 positive integers k with removed. k?:.b.

4.2 Strong Induction and Well-Ordering

Introduction

In Section 4.1 we introduced mathematical induction and we showed how to use it to prove a variety of theorems. In this section we will introduce another form of mathematical induction, called strong induction, which can often be used when we cannot easily prove a result using mathematical induction. The basis step of a proof by strong induction is the same as a proof of the same result using mathematical induction. That is, in a strong induction proof that is true fo r all positive integers the basis step shows that (I) is true. However, the inductivePe stepsn) in these two proof methodsn, are different. In a proof byP mathematical induction, the inductive step shows that if the inductive hypothesis is true, then + 1) is also true. In a proof by strong induction, the inductive step showsP( k)that if is P(truek for all positive integers not exceeding then + 1) is true. That is, for the inductivePU) hypothesis we assume that is true fo r j k, 1, 2, ...P(k PU) = The validity of both, k. mathematical induction and strong induction fo llow from the well­ ordering property in Appendix 1. In fact, mathematical induction, strong induction, and well­ ordering are all equivalent principles. That is, the validity of each can be proved from either of the other two. This means that a proof using one of these two principles can be rewritten as a proof using either of the other two principles. Just as it is sometimes the case that it is much easier to see how to prove a result using strong induction rather than mathematical induction, it is sometimes easier to use well-ordering than one ofthe two forms of mathematical induction. In this section we will give some examples of how the well-ordering property can be used to prove theorems.

Strong Induction

Before we illustrate how to use strong induction, we state this principle again. 4-29 4.2 Strong Induction and Well-Ordering 291

Extra� Let S be the set of nonnegative integers of the form a where is an integer. This Examples� Solutioset is nonemptyn: because can be made as large as desired (taking-d q, to beq a negative integer -dq S q a with large absolute value). By the well-ordering property, has a least element r = The integer is nonnegative. It is also the case that If it were not, then there would-dq be. r r < o a smaller nonnegative element in S, namely, a 1).d. To see this, suppose that r � Because a it fo llows that a -d(q1)o =+ (a - O. Conse­d. = r, = r � quently, there aredq integers+ and with -d(qo +The proof thatdq ) -dand are-d unique is leftas o q r :::: r < oq r an exercise for the reader. 0 d. ...

EXAMPLE 6 In a round-robin tournamentevery player plays every other player exactly once and each match has a winner and a loser. We say that the players PI , P2 , ... , Pm form .a if PI beats P2 , P2 beats P3 , ..., Pm -I beats Pm , and Pm beats PI . Use the well-ordering principlecycle to show that if there is a cycle oflength m (m � 3) among the players in a round-robin tournament, there must be a cycle of three of these players.

We assume that there is no cycle of three players. Because there is at least one cycle in theSoluti round-robinon: tournament, the set of all positive integers for which there is a cycle of length is nonempty. By the well-ordering property, this set of posn itive integers has a least element k, whichn by assumption must be greater than three. Consequently, there exists a cycle of players PI , P2 , P3 , ..., Pk and no shorter cycle exists. Because there is no cycle of three players, we know that k > 3. Consider the first three elements of this cycle, PI, P2, and P3 . There are two possible outcomes of the match between PI and P3 · If P3 beats PI, it fo llows that PI, P2, P3 is a cycle of length three, contradicting our assumption that there is no cycle of three players. Consequently, it must be the case that PI beats P3 . This means that we can omit P2 from the cycle PI , P2 , P3 , ..., Pk to obtain the cycle PI , P3 , P4 , ... , Pk of length k 1, contradicting the assumption that the smallest cycle has length k. We conclude that there must- be a cycle oflength three. ...

Exercises

1. Use strong induction to show that if you can n,m one mile parts ofthis exercise outline a strong induction proof that or two miles, and if you can always run two more miles P(n) is true for n � IS. once you have run a specifiednumber of miles, then you a) Show statements P(lS), P(19), P(20), and P(21) are can run any number of miles. true, completing the basis step of the proof. 2. Use strong induction to show that all dominoes fall in b) What is the inductive hypothesis of the proof? an infinitearrangement of dominoes if you know that the c) What do you need to prove in the inductive step? firstthree dominoes fall, and that when a domino falls, the Complete the inductive step for k 21. domino three farther down in the arrangement also falls. d) � e) Explain why these steps show that this statement is 3. Let P (n ) be the statement that a postage of n cents can be true whenever n IS. formed using just 3-cent stamps and 5-cent stamps. The � parts of this exercise outline a strong induction proof that 5. a) Determine which amounts of postage can be formed using just 4-cent and II-cent stamps. P(n) is true for n � S. b) Prove your answer to (a) using the principle of math­ a) Show that the statements P(S), P(9), and P(lO) are true, completing the basis step of the proof. ematical induction. Be sure to state explicitly your b) What is the inductive hypothesis of the proof? inductive hypothesis in the inductive step. What do you need to prove in the inductive step? c) Prove your answer to (a) using strong induction. How c) does the inductive hypothesis in this proof differ from d) Complete the inductive step for k � lO. Explain why these steps show that this statement is that in the inductive hypothesis for a proof using math­ e) ematical induction? true whenever n � S. 4. Let P (n) be the statement that a postage of n cents can be 6. a) Determine which amounts of postage can be formed formed using just 4-cent stamps and 7-cent stamps. The using just 3-cent and IO-cent stamps. 292 4 / Induction and Recursion 4-30

b) Prove your answer to (a) using the principle of math­ to show that this strategy works. For the first move, the ematical induction. Be sure to state explicitly your first playerchomps all cookies except those in the left and inductive hypothesis in the inductive step. top edges. On subsequent moves, after the second player c) Prove your answer to (a) using strong induction. How has chomped cookies on either the top or left edge, the does the inductive hypothesis in this proof differ from firstplayer chomps cookies in the same relative positions that in the inductive hypothesisfor a proof using math­ in the leftor top edge, respectively.] ematical induction? *16. Prove that the firstplayer has a winning strategy for the 7. Which amounts of money can be formed using just two­ game of Chomp, introduced in Example 12 in Section 1.7, dollar bills and five-dollarbills? Prove your answer using if the initial board is two squares wide, that is, a 2 x n strong induction. board. [Hint: Use strong induction. The firstmove of the firstplayer should be to chomp the cookie in the bottom S. Suppose that a store offers giftcertificates in denomina­ tions of25 dollars and 40 dollars. Determine the possible row at the far right.] total amounts you can form using these giftcertficates. 17. Use strong induction to show that if a simple polygon with Prove your answer using strong induction. at least four sides is triangulated,then at least two of the Use strong induction to prove that is irrational. [Hint: triangles in the triangulation have two sides that border *9. -J2 the exterior of the polygon. Let P (n ) be the statement that -J2 =1= /n b for any positive integer b.] *IS. Use strong induction to show that when a convex poly­ gon P with consecutive vertices V V V is trian­ 10. Assume that a chocolate bar consists ofn squares arranged I, 2, ..., n in a rectangular pattern. The bar, a smaller rectangular gulated into n - 2 triangles, the n - 2 triangles can be numbered 1, 2, , n - 2 so that is a vertex oftriangle piece of the bar, can be broken along a vertical or a hor­ ... Vi izontal line separating the squares. Assuming that only i for i = 1,2, ... , n - 2. one piece can be broken at a time, determine how many *19. Pick's theorem says that the area of a simple polygon breaks you must successively make to break the bar into P in the plane with vertices that are all lattice points n separate squares. Use strong induction to prove your (that is, points with integer coordinates) equals I(P) + answer. R(P)/2 - 1, where I(P) and R(P) are the number oflat­ tice points in the interior of P and on the boundary of P, 11. Consider this variation of the game of Nim. The game respectively. Use strong induction on the number ofver­ begins with n matches. Two players take turnsremoving tices of P to prove Pick's theorem. [Hint: For the basis matches, one, two, or three at a time. The player removing step, firstprove the theorem for rectangles, then for right the last match loses. Use strong induction to show that if triangles, and finally for all triangles by noting that the each player plays the best strategy possible, the firstplayer area of a triangle is the area of a larger rectangle contain­ wins if n = 4}, 4} + 2, or 4} + 3 for some nonnegative ing it with the areas of at most three triangles subtracted. integer} and the second player wins in the remaining case For the inductive step, take advantage of Lemma 1.] when n = 4} + 1 for some nonnegative integer }. **20. Suppose that P is a simple polygon with vertices 12. Use strong induction to show that every positive integer n V I , V2, , Vn listed so that consecutive vertices are con­ can be written as a sum of distinct powers of two, that is, nected by... an edge, and and are connected by an edge. 1 V I Vn as a sum of a subset of the integers 2° = 1, 2 = 2, 22 = 4, A vertex is called an ifthe line segment connecting and so on. [Hint: For the inductive step, separately con­ Vi ear the two vertices adjacent to V is an interior diagonal of the sider the case where k + 1 is even and where it is odd. i simple polygon. Two ears V and V are called nonover­ When it is even, note that (k + 1)/2 is an integer.] i i lapping if the interiors of the triangles with vertices Vi *13. A jigsaw puzzle is put together by successively joining and its two adjacent vertices and V and its two adjacent pieces that fittogether into blocks. A move is made each vertices do not intersect. Prove thati every simple polygon time a piece is added to a block, or when two blocks are with at least four vertices has at least two nonoverlapping joined. Use strong induction to prove that no matter how ears. the moves are carried out; exactly n - 1 moves are re­ 21. In the proofof Lemma 1 we mentioned that many incorrect quired to assemble a puzzle with n pieces. methods for findinga vertex p such that the line segment 14. Suppose you begin with a pile of n stones and split this bp is an interior diagonal of P have been published. This pile into n piles of one stone each by successively splitting exercise presents some of the incorrect ways p has been a pile of stones into two smaller piles. Each time you split chosen in these proofs. Show, by considering one of the a pile you multiply the number of stones in each of the two polygons drawn here, that for each of these choices of p, ' smaller piles you form, so that if these piles have r and s the line segment bp is not necessarily an interior diagonal stones in them, respectively, you compute r s. Show that of P. no matter how you split the piles, the sum of the products a) p is the vertex of P such that the angle Labp is computed at each step equals n(n - 1)/2. smallest. 15. Prove that the firstplayer has a winning strategy for the b) p is the vertex of P with the least x-coordinate (other game of Chomp, introduced in Example 12 in Section 1.7, than b). if the initial board is square. [Hint: Use strong induction c) p is the vertex of P that is closest to b. 4-3 / 4.2 Strong Induction and Well-Ordering 293

a a b) P(I) and P(2) are true; for all positive integers n, if P(n) and P(n + 1) are true, then P(n + 2) is true. c) P(I) is true; for all positive integers n, if P(n) is true, b p then P(2n) is true. b d) P( 1) is true; for all positive integers n, if P(n) is true, g then P(n + I) is true. 26. Suppose that P(n) is a propositional function. Determine s for which nonnegative integers n the statement P(n) must be true if h a) P(O) is true; for all nonnegative integers n, if P(n) is true, then P(n + 2) is true. c e b) P(O) is true; for all nonnegative integers n, if P(n) is true, then P(n + 3) is true. c) P(O) and P(l) are true; for all nonnegative integers n, Exercises 22 and 23 present examples that show inductive if P(n) and P(n + 1) are true, then P(n + 2) is true. loading can be used to prove results in computational geom­ d) P(O) is true; for all nonnegative integers n, if P(n) is etry. true, then P(n + 2) and P(n + 3) are true. Let P(n) be the statement that when nonintersecting di­ 27. Show that if the statement P(n) is true for infinitely *22. many positive integers n and P(n + 1) P(n) is true agonals are drawn inside a convex polygon with n sides, -+ at least two vertices of the polygon are not endpoints of for all positive integers n, then P(n) is true for all positive any of these diagonals. integers n. 28. Let b be a fixed integer and j a fixed positive integer. a) Show that when we attempt to prove P(n) for all inte­ Show that if P(b), P(b + 1), ..., P(b + j) are true and gers n with n 3 using strong induction, the inductive ::: [P(b) P(b + 1) A ··· P(k)] P(k + 1) is true for step does not go through. /\ /\ -+ every positive integer k ::: b+ j, then P(n) is true for all b) Show that we can prove that P (n) is true for all inte­ integers n with n b. gers n with n ::: 3 by proving by strong induction the ::: 29. What is wrong with this "proof" by strong induction? stronger assertion Q(n), for n ::: 4, where Q(n) states that whenever nonintersecting diagonals are drawn in­ "Theorem " For every nonnegative integer n, 5n = O. side a convex polygon with n sides, at least two non­ Basis Step: 5 · 0 O. adjacent vertices are not endpoints of any of these = diagonals. Inductive Step: Suppose that 5 j = 0 for all nonnegative integers j with 0 ::c j ::c k. Write k + 1 = i + j, where 23. Let E(n) be the statement that in a triangulation of a sim­ i and j are natural numbers less than k + 1. By the ple polygon with n sides, at least one of the triangles in inductive hypothesis, 5(k + 1) 5(i + j) 5i + 5j the triangulation has two sides bordering the exterior of = = = 0+0 = O. the polygon. n *30. Find the flaw with the following "proof" that a = 1 for a) Explain where a proof using strong induction that E(n) all nonnegative integers n, whenever a is a nonzero real is true for all integers n ::: 4 runs into difficulties. number. b) Show that we can prove that E(n) is true for all inte­ O Basis Step: aO = I is true by the definitionof a . gers n ::: 4 by proving by strong induction the stronger Inductive Step: 1 statement T(n) for all integers n ::: 4, which states that Assume that ai = for all nonnegative in every triangulation of a simple polygon, at least two integers j with j ::c k. Then note that of the triangles in the triangulation have two sides bor­ ak . ak 1 . 1 a k+ 1 1 . dering theexterior of the polygon. = ak-1 = -1 - = *24. A stable assignment, defined in the preamble to Exer­ *31. Show that strong induction is a valid method of proof cise 58 in Section 3.1, is called optimal fo r suitors if no by showing that it follows from the well-ordering stable assignment exists in which a suitor is paired with property. a suitee whom this suitor prefers to the person to whom 32. Find the flaw with the following "proof" that every this suitor is paired in this stable assignment. Use strong postage of three cents or more can be formed using just induction to show that the deferred acceptance algorithm three-cent and four-cent stamps. produces a stable assignment that is optimal for suitors. Basis Step: We can form postage of three cents with a 25. Suppose that P(n) is a propositional function.Determine for which positive integers n the statement P(n) must be single three-cent stamp and we can form postage of four true, and justifyyour answer, if cents using a single four-cent stamp. a) P(I) is true; for all positive integers n, if P(n) is true, Inductive Step: Assume that we can form postage of j then P(n + 2) is true. cents for all nonnegative integers j with j ::c k using just 294 4 / Induction and Recursion 4-32

three-cent and four-cent stamps. We can then form postage e) Conclude from(c) and (d) that the greatest common of k + 1 cents by replacing one three-cent stamp with a divisor of a and b exists. Finish the proofby showing four-cent stamp or by replacing two four-cent stamps by that this greatest common divisor is unique. three three-cent stamps. 37. Let a be an integer and d be a positive integer. Show Show that we can prove that k) is true for all pairs 33. pen, that the integers q and r with a = dq + r and 0 � r < d, of positive integers n and k if we show which were shown to exist in Example 5, are unique. P(l, + a) 1) is true and pen, k) ---+ [Pen I, k) /\ 38. Use mathematical induction to show that a rectangu­ Pen, k + 1)] is true for all positive integers n lar checkerboard with an even number of cells and two and k. squares missing, one white and one black, can be covered b) P(l, k) is true for all positive integers k, and by dominoes. P (n, k) P (n + I, k) is true for all positive integers ---+ **39. Can you use the well-ordering property to prove the state­ n and k. ment: "Every positive integer can be described using no c) pen, 1) is true for all positive integers n, and more than fifteen English words"? Assume the words P (n, k) ---+ P (n, k + 1) is true for all positive integers come froma particular dictionary of English. [Hint: Sup­ n and k. pose that there are positive integers that cannot be de­ .. scribed using no more than fifteenEnglish words. By well 34. Prove that L:J=l j(j + 1)(j + 2) · (j + k - 1) = n(n + l)(n + 2) · ..(n + k)/(k + 1) for all positive ordering, the smallest positive integer that cannot be de­ integers k and n. [Hint: Use a technique from scribed using no more than fift een English words would Exercise 33.] then exist.] Use the well-ordering principle to show that if x and *35. Show that if aI , a2, ..., an are n distinct real numbers, ex­ 40. y actly n - I multiplications are used to compute the prod­ are real numbers with x < y, then there is a rational uct of these n numbers no matter how parentheses are number r with x < r < y. [Hint: Use the Archimedean inserted into their product. [Hint: Use strong induction property, given in Appendix 1, to find a positive integer arid consider the last multiplication.] A with A > 1/(y - x). Then show that there is a ratio­ nal number r with denominator A between x and by *36. The well-ordering property can be used to show that there y is a unique greatest common divisor of two positive inte­ looking at the numbers Lx J + j / A, where j is a positive gers. Let a and b be positive integers, and let S be the set integer.] of positive integers ofthe form as + bt, where s and t are *41. Show that the well-ordering property can be proved when integers. the principle of mathematical induction is taken as an axlOm. a) Show that Sis nonempty. b) Use the well-ordering property to show that S has a *42. Show that the principle of mathematical induction and smallest element c. strong induction are equivalent; that is, each can be shown to be valid from the other. c) Show that if d is a common divisor of a and b, then d is a divisor of c. *43. Show that we can prove the well-ordering property when d) Show that c I a and c I b. [Hint: First, assume that we take either the principle of mathematical induction or c y a. Then a = qc + r, where 0 < r < c. Show that strong induction as an axiom instead of taking the well­ rES, contradicting the choice of c.] ordering property as an axiom.

4.3 Recursive Definitions and Structural Induction

Introduction

Sometimes it is difficult to define an object explicitly. However, it may be easy to define this object in terms of itself. This process is called recursion. For instance, the picture shown in Figure I is produced recursively. First, an original picture is given. Then a process of suc­ cessively superimposing centered smaller pictures on top of the previous pictures is carried out. We can use recursion to define sequences, functions, and sets. In previous discussions, we specifiedthe terms of a sequence using an explicit formula. For instance, the sequence of n powers of2 is given by an = 2 fo r n = 1,2, ....However, this sequence can also be defined 0, ao 1, by giving the first term of the sequence, namely, = and a rule for finding a term of the 308 4 / Induction and Recursion 4-46

0, giving us the desired equality. Now suppose that n > so am,n = am,n-l + n. Because (m , n - 1) is smallerthan (m , n), the inductive hypothesiste lls us that a ,n m + (n - 1)n/2, 2 m -l = so am,n = m + (n - 1)n/2 + n = m + (n - n + 2n)/2 = m + n(n + 1)/2. This finishes the inductive step.

As mentioned, we will justifythis proof technique in Section 8.6.

Exercises

0, 1. Find f(I), f(2), f(3), and f(4) if fen) is definedrecur­ c) f(O) = f(I) = 1, fen) = 2f(n + 1) for n � 2· sively by and for n 0, 0, f(O) = 1 = 1, 2, ... d) f(O) = f(I) = 1, fen) = 2f(n - 1) for n � 1 ifn is odd and n and a) fen + 1) = fen) + 2. e) f(O) = 2, f(n) = fen - 1) � 1 fen + 1) 3f(n). fen) = 2f(n - 2) ifn � 2 b) = n c) fen + 1) = 2!( ). Give a recursive definition of the sequence {a }, 2 7. n d) fen + 1) = f(n) + fen) + 1 . n = 1,2,3, ... if 2. Find f(I), f(2), f(3), f(4), and f(5) if fen) is defined an 6n. an 2n + 1. a) = n b) = recursively by f(O) = 3 and for n = 0, 1, 2, ... c) an = IO . d) an = 5. a) fen + 1) = -2f(n). 8. Give a recursive definition of the sequence {an}, b) fen + 1) = 3f(n) + 7. n = I,2,3, ... if c) fen + 1) = f(np - 2f(n) - 2. an 4n - 2. an 1 + (_I)n. fen + 1) 3!(n)/3. a) = b) = d) = c) an = n(n + 1). d) an = nZ. 3. Find f(2), f(3), f(4), and f(5) if f is definedrecursively by f(O) = -1, f(I) = 2 and for n = 1,2, ... 9. Let F be the function suchthat F(n) is the sum of the first n positive integers. Give a recursive definitionof F(n). a) fen + I) = fen) + 3f(n - 1). z 10. Give a recursive definitionof Sm (n), the sum ofthe integer b) fen + 1) = f(n) fen - 1). m and the nonnegative integer n. c fen + 1) = 3f(np - 4f(n - I)Z. ) Give a recursive definitionof Pm (n), the product of the d) fen + 1) = fen - I)/f(n). 11. integer and the nonnegative integer n. 4. Find f(2), f(3), f( 4), and f(5) if f is definedrecursively m by f(O) = f(I) = 1 and for n = 1,2, ... In Exercises 12-19 In is the nth Fibonacci number. Prove that is a pos­ fen + 1) fen) - fen - 1). 12. f + i + ... + f� = fn fn+ I when n a) = itive integer.f I b) fen + 1) = f(n)f(n - 1). z 3 Prove that n is a pos­ c) fen + 1) = f(n) + fen - 1) . 13. fl + /3 + ...+ fZn-1 = hn when itive integer. d) fen + 1) = f(n)/f(n - 1). _ n 5. Detennine whethereach of these proposed definitionsis *14. Show that fn+dn -I - f� = ( I) when n is a positive a valid recursive definition of a function f from the set integer. of nonnegative integers to the set of integers. If f is well *15. Show that fo fl + fdz + . .. + fZn-dz n = f�n when n defined, finda fonnulafor f(n) when n is a nonnegative is a positive integer. integer and prove that your fonnulais valid. *16. Show that fo - fl + h - ... - hn-I + hn = hn-I when n is a positive integer. a) f(O) = 0, fen) = 2f(n - 2) for n � 1 - 1 b) f(O) = 1, fen) = fen - 1) - 1 for n � 1 17. Detennine thenumber of divisions used by the Euclidean c) f(O) = 2, 1(1) = 3,f(n) = fen - 1) - lfor n � 2 algorithm to findthe greatest common divisor of the Fi­ d) f(O) = 1, f(I) = 2, fen) = 2f(n - 2) for n � 2 bonacci numbers fn and fn+ I , where n is a nonnegative e) f(O) = I,f(n) = 3f(n - l)ifn isodd and n � land integer. VerifYyour answer using mathematical induction. f(n) 9 n - 2) if n is even and n 2 = fe � 18. Let 6. Detennine whether each of these proposed definitionsis a valid recursive definition of a function f from the set = of nonnegative integers to the set of integers. If f is well A J defined,find a fonnulafor f(n) when n is a nonnegative Show that U � integer and prove that your fonnulais valid. f f(O) I, f(n) -fen - 1) for n 1 n a) = = � fn-I ] b) f(O) = 1, f(l) = 0, f(2) = 2, fen) = 2f(n - 3) for n�3 when n is a positive integer. 4-47 4.3 Recursive Definitionsand Structural Induction 309

19. By taking determinants ofboth sides ofthe equation in Ex­ in the plane and look for lines containing points in the ercise 18, prove the identity given in Exercise 14. (Recall set.] + + that the determinant of the matrix � � is ad - be.) a) S = {(a, b) I a E Z , bEZ , and a + b is odd} I I b) S={(a, b)laEZ+,bEZ+,and alb} *20. Give a recursive definitionofthe functionsmax and min so c) S = {(a, b) I a E Z+, b E Z+, and 31a + b) that max(al, a2, ...,a n) and min(al' a2, ...,a n) are the 29. Give a recursive definition of each of these sets of or­ maximum and minimum ofthe n numbers aI, a2, ..., an, dered pairs of positive integers. Use structural induction respectively. to prove that the recursive definitionyou found is correct. *2 1. Let aI, a2, ..., an, and bl, b2, ..., bn be real numbers. [Hint: To finda recursive definition,plot the points in the Use the recursive definitionsthat you gave in Exercise 20 set in the plane and look for patterns.] to prove these. a) S = {(a, b) la E Z+, bEZ +, and a + b is even} a) max( -aI, -a2, ... , -an) = - min(al' a2, ...,a n) b) S={(a,b)laEZ+,bEZ+,andaorbis odd} b) max(al + bl, a2 + b2, ..., an + bn) c) S = {(a,b) la E Z+,b E Z+,a +bisodd, and 3 I b) � max(al, a2, ..., an) + max(bl , b2, ..., bn) 30. Prove that in a bit string, the string 01 occurs at most one c) min(al + bl, a2 + b2, ... , an + bn) more time thanthe string 10. ::: min(al' a2, ...,a n) + min(bl' b2, ..., bn) 31. Definewell-f ormed formulae of sets, variables represent­ 22. Show that the set S definedby 1 E S and s + t E S when­ ing sets, and operators from {-, -}. ever s E S and t E S is the set of positive integers. U, n, 32. a) Give a recursive definition of the function ones(s), 23. Give a recursive definitionof the set of positive integers which counts the number of ones in a bit string s. that are multiples of 5. b) Use structural induction to prove that ones(st) = 24. Give a recursive definitionof ones(s) + ones(t). the set of odd positive integers. a) 33. a) Give a recursive definition ofthe functionm( s), which b) the set of positive integer powers of 3. equals the smallest digit in a nonempty string of dec­ c) the set of polynomials with integer coefficients. imal digits. 25. Give a recursive definitionof b) Use structural induction to prove that m(st) = a) the set of even integers. min(m(s), met»�. b) the set of positive integers congruent to 2 modulo 3. The reversal of a string is the string consisting ofthe symbols c) the set ofpositive integers not divisible by 5. of the string in reverse order. The reversal of the string w is 26. Let S be the subset of the set of ordered pairs of integers denoted by w R • definedrecursively by 34. Find the reversal ofthe fo llowing bit strings. Basis step: (0, 0) E S. a) 0101 b) 11011 c) 1000 1001 0111 Recursive step: If(a, b) E S, then (a + 2, b + 3) E Sand 35. Give a recursive definition of the reversal of a string. (a +3,b+2)ES. [Hint: First definethe reversal ofthe empty string. Then a) List the elements of S produced by the firstfive appli­ write a string w oflength n + 1 as xy, where x is a string cations of the recursive definition. oflength n, and express the reversal ofw in terms of xR b) Use strong induction on the number of applications and y.] of the recursive step of the definition to show that R 51a + b when (a, b) E S. *36. Use structural induction to prove that (WIW2) = wfwf . i c) Use structural induction to show that 51a + b when 37. Give a recursive definitionof W , where w is a string and (a, b) E S. i is a nonnegative integer. (Here Wi represents the con­ 27. Let S be the subset of the set of ordered pairs of integers catenation of i copies of the string w .) definedrecursively by *38. Give a recursive definitionof the set of bit strings that are Basis step: (0, 0) E S. palindromes. 39. Recursive step: If (a, b) E S, then (a, b + 1) E S, When does a string belong to the set A of bit strings de­ (a + 1, b + 1) E S, and (a + 2, b + 1) E S. finedrecursively by a) List the elements of S produced by the firstfo ur ap­ AEA plications of the recursive definition. Ox 1 E A if x E A, b) Use strong induction on the number of applications of the recursive step ofthe definitionto show that a � 2b where A is the empty string? whenever (a, b) E S. *40. Recursively define the set of bit strings that have more c) Use structural induction to show that a � 2b whenever zeros than ones. (a, b) E S. 41. Use Exercise 37 and mathematical induction to show that 28. i Give a recursive definitionof each ofthese sets ofordered l(w ) = i · l(w), where w is a string and i is a nonnegative pairs of positive integers. [Hint: Plot the points in the set integer. 310 4/ Induction andRecursion 4-48

*42. Show that (wR)i = (wi)R whenever w is a string and i is study of the complexity of certain algorithms involving set a nonnegative integer; that is, show that the ith power of unions. (There are several differentvariants of this function. the reversal of a string is the reversal of the i th power of All are called Ackermann's functionand have similar proper­ the string. ties even though their values do not always agree.) 43. Use structural induction to show that neT) :::: 2h(T) + I, 2n ifm = 0 where T is a fullbinary tree, neT) equals the number of o if m > 1 and n = 0 vertices ofT, and h(T) is theheight ofT. A(m, n) = 2 ifm :::: 1 and n = 1 The set ofleaves and the set ofinternal vertices ofa fullbinary 1 A(m - 1, A(m, n - 1» tree can be definedrecursively. ifm :::: land n :::: 2

Basis step: The root r is a leaf of the full binary tree with Exercises 48-55 involve this version ofAckermann's function. exactly one vertex r. This tree has no internalvertices. 48. Find these values of Ackermann's function. Recursive step: . The set ofleaves of the tree T = TI T2 is the a) A(I,O) b) A(O, 1) union of the set ofleaves ofT1 and the set ofleaves ofT2. The c) A(I, I) d) A(2, 2) internalvertices of T are the root r of T and the union of 49. Show that A(m, 2) = 4 whenever m :::: 1. the set of internalvertices of TI andthe set of internalvertices 2n ofT2. SO. Show that A(1, n) = whenever n :::: 1. 44. Use structural induction to show that leT), the number of 51. Find these values of Ackermann's function. leaves of a full binary tree T, is 1 more than i(T), the a) A(2, 3) *b) A(3, 3) number of internal vertices of T . *52. Find A(3, 4). 45. Use generalized induction as was done in Example 15 to **53. Prove thatA(m , n + 1) > A(m, n) whenever m and n are show that if am,n is definedrecursively by ao,O = 0 and nonnegative integers. am-l n + 1 if n = 0 and m > 0 *54. Prove that A(m + 1, n) :::: A(m, n) whenever m and n are am ' n = \ ' nonnegativeintegers. am,n-l + l'1f n > 0 , 55. Prove that A(i, j) :::: j whenever i and j are nonnegative then am,n = m + n for all (m, n) E N X N. integers. 46. Use generalized induction as was done in Example 15 to 56. Use mathematical induction to prove that a functionF de­ show thatif am,n is definedrecursively by al,l = 5 and finedby specifyingF( O) and a rule for obtaining F(n + 1) am-1 n +2 ifn =land m > 1 fromF( n) is well defined. am ' n = { ' 57. am,n-l + 2'1 f n > 1 , Use strong induction to prove that a function F defined by specifying F(O) and a rule for obtaining F(n + 1) + + then am,n = 2(m + n) + 1 for all (m, n) E Z X Z . from the values F(k) for k = 0, 1,2, ... , n is well *47. A partition of a positive integer n is a way to write n as defined. a sum of positive integers where the order of terms in the 58. Show that each of these proposed recursive definitionsof sum does not matter. For instance,7 = 3 + 2 + 1 + 1 is a functionon the set of positive integers does not produce a partition of7. Let Pm equal the number of different par­ a well-definedfunction. titions of m, and let Pm ,n be the number of different ways a) F(n) = 1 + F(ln/2J) for n :::: 1and F(I) = 1. to express m as the sum of positive integers not exceed­ b) F(n) = 1 + F(n - 3) for n :::: 2, F(I) = 2, and ing n. F(2) = 3. a) Show that Pm,m = Pm . c) F(n) = 1 + F(n/2) for n :::: 2, F(I) = I, and b) Show that the fo llowing recursive definition forP m,n F(2) = 2. is correct: d) F(n) = 1 + F(n/2) if n is even and n :::: 2, 1 ifm = 1 F(n) = 1 - F(n - 1) ifn is odd, and F(I) = 1. 1 ifn = 1 e) F(n) = 1 + F(n/2) if n is even and n :::: 2, F(n) = 1. Pm,n = Pm,m ifm < n F(3n - 1) ifn is odd and n :::: 3, and F(I) = 1 1 + Pm,m-l ifm = n > 1 59. Show that each of these proposed recursive definitionsof Pm,n-l + Pm-n,n ifm > n > 1. a functionon the set of positive integers does not produce a well-definedfunction. 6 c) Find the number of partitions of 5 and of using this + + recursive definition. a) F(n) = 1 F(l(n 1)/2J) for n :::: 1and F(I) = 1. b) F(n) = 1 + F(n - 2) for n :::: and2 F(I) = O. Consider an inductive definitionof a version of Ackermann's c) F(n) = 1 + F(n/3) for n :::: 3, F(I) = I, F(2) = 2, � fu nction. This functionwas named afterWilhelm Ackermann, and F(3) = 3...... a German mathematician who was a student ofthe great math­ d) F(n) = 1 + F(n/2) if n is even and n :::: 2, ematician David Hilbert. Ackermann's functionplays an im­ F(n) = 1 + F(n - 2) ifn is odd, andF(I) = 1. portant role in the theory of recursive functions and in the e) F(n) = 1 + F(F(n - 1» ifn :::: 2 and F(I) = 2. 4-49 4.4 Recursive Algorithms 311

Exercises 60-62 dealwith iterations ofthe logarithm function. Exercises 63-65 deal with values of iterated functions. Sup­ Let log n denote the logarithm of n to the base 2, as usual. The pose that f(n) is a function from the set of real numbers, or functionlog (k) n is definedrecursively by positive real numbers, or some other set of real numbers, to the set of real numbers such that f(n ) is monotonically increasing n ifk = 0 k l k l [that is, f(n) < f(m) when n < m) and f(n) < n for all n in k) _ log(log( - ) n) if log( - ) n is defined (k) I og ( n - the domain of f.] The functionf (n) is definedrecursively and positive by undefined otherwise. (k) n ifk = 0 I f (n) = (k-l) The iterated logarithm is the functionlog* n whose value at { f(f (n» ifk > O. (k) n is the smallest nonnegative integer k such that log n ::: I. Furthennore, let c be a positive real number. The iterated 60. Find each of these values: f; is the number of iterations of f required to reduce (2) function a) log 16 its argument to c or less, so f;(n) is the smallest nonnegative b) (3) k 10g 256 integer k such that f (n) c. (3) 65536 ::: c) log 2 63. - (4 265536 Let f(n) = n a, where a is a positive integer. Find a d) log ) 2 fonnulafor f(k)(n). What is the value of fo (n) when n is . 61. Find the value oflog* n for each of these values of n: a positive integer? (k) a) 2 b) 4 c) 8 d) 16 64. Let f(n) = nj2. Find a fonnulafor f (n). What is the e) 256 f) 65536 g) 22048 value of fj(n) when n is a positive integer?

4.4 Recursive Algorithms

Introduction

Sometimes we can reduce the solution to a problem with a particular set of input to the solution of the same problem with smaller input values. For instance, the problem of findingthe greatest

common divisor of two positive integers a and b, where b > a, can be reduced to finding the greatest common divisor of a pair of smaller integers, namely, b mod a and a, because

gcd(b mod a, a) = gcd(a, b). When such a reduction can be done, the solution to the original problem can be found with a sequence of reductions, until the problem has been reduced to some initial case for which the solution is known. For instance, for findingthe greatest common divisor,

the reduction continues until the smaller of the two numbers is zero, because gcd(a, 0) = a when a > o. We will see that algorithms that successively reduce a problem to the same problem with smaller input are used to solve a wide variety of problems.

DEFINITION 1 An algorithmis called if it solves a problem by reducing it to instance of the sameproblem with smallerrecurs input.ive an

unks� We will describe a variety of different recursive algorithms in this section.

EXAMPLE 1 Give a recursive algorithm for computing n !, where n is a nonnegative integer.

Extra� We can build a recursive algorithm that finds n!, where n is a nonnegative integer, Exampleslliiiiiiiit Solution: . based on the recursive definitionofn !, which specifiesthat n! = n (n - I)! when n is a positive integer, and that o! = To find n! for a particular integer, we use the recursive step n times, each time replacing a value1. of the factorial function with the value of the factorial function at 4-59 4.4 Recursive Algorithms 321

This analysis shows that the merge sort achieves the best possible big-O estimate for the number of comparisons needed by sorting algorithms, as stated in the fo llowing theorem.

THEOREM 1 The number of comparisons needed to merge sorta list with n elements is O(n log n).

We describe another efficientalgorithm, the quick sort, in the exercises. Exercises

1. Trace Algorithm 1 when it is given n = 5 as input. That 16. Prove that the recursive algorithm for findingthe sum of is, show all steps used by Algorithm 1 to find 5!, as is the first n positive integers you found in Exercise 8 is done in Example 1 to find4!. correct.

2. Trace Algorithm 1 when it is given n = 6 as input. That 17. Describe a recursive algorithm for multiplying two non­ is, show all steps used by Algorithm 1 to find 6!, as is negative integers x and y based on the fact that xy = done in Example 1 to find4!. 2(x . (y/2)) when y is even and xy = 2(x . Iy/2J ) + x 3. Trace Algorithm 3 when it is given m = 5, n = 11, and when y is odd, together with the initial condition xy = 0 b = 3 as input. That is, show all the steps Algorithm 3 when y = O. uses to find3 11 mod 5. 18. Prove that Algorithm 1 for computing n! when n is a non­ 4. Trace Algorithm 3 when it is given m = 7, n = 10, and negative integer is correct. 19. b = 2 as input. That is, show all the steps Algorithm 3 Prove that Algorithm 4 for computing gcd(a, b) when a O uses to find2 l mod 7. and b are positive integers with a < b is correct. 5. Trace Algorithm 4 when it findsgcd (8, 13). That is, show 20. Prove that the algorithm you devised in Exercise 11 is all the steps used by Algorithm 4 to findgcd (8, 13). correct. 6. Trace Algorithm4when itfinds gcd(12, 17). That is, show 21. Prove that the recursive algorithm that you found in Ex­ all the steps used by Algorithm 4 to findgcd( 12, 17). ercise 7 is correct. 7. Give a recursive algorithm for computing nx whenever n 22. Prove that the recursive algorithm that you found in Ex­ is a positive integer and x is an integer, usingjust addition. ercise lOis correct. 2 8. Give a recursive algorithm for findingthe sum of the first 23. Devise a recursive algorithm for computing n where n is a nonnegative integer using the fact that (n + n positive integers. 2 2 + + 1 9. 1) = n 2n . Then prove that this algorithm is Give a recursive algorithm for findingthe sum of the first correct. n odd positive integers. 24. Devise a recursive algorithm to find a2n , where a is a real 10. Give a recursive algorithm for findingthe maximum of a [Hint: finite set of integers, making use of the fact that the max­ number and n is a positive integer. Use the equality 2n+1 2n 2 imum of n integers is the larger of the last integer in the a = (a ) .] list and the maximum ofthe firstn - 1 integers in the list. 25. How does the number of multiplications used by the al­ 11. gorithm in Exercise 24 compare to the number of multi­ Give a recursive algorithm for findingthe minimum of a 2n finiteset of integers, making use of the fact that the mini­ plications used by Algorithm 2 to evaluate a ? *26. Use the algorithm in Exercise 24 to devise an algorithm for mum of n integers is the smaller of the last integer in the n list and the minimum of the the first n - 1 integersin the evaluating a when n is a nonnegative integer. [Hint: Use list. the binary expansion of n.] n *27. 12. Devise a recursive algorithm for findingx mod m when­ How does the number of multiplications used by the al­ ever n, x, and m are positive integers based on the fact gorithm in Exercise 26 compare to the number of multi­ n n 1 n that x mod m = (x - mod m . x mod m ) mod m. plications used by Algorithm 2 to evaluate a ? 28. 13. Give a recursive algorithm for findingn! mod m whenever How many additions are used by the recursive and itera­ n and m are positive integers. tive algorithms given in Algorithms 7 and 8, respectively, 14. Give a recursive algorithm for findinga mode of a list of to find the Fibonacci number h? integers. (A mode is an element in the list that occurs at 29. Devise a recursive algorithm to findthe nth term ofthe se­ least as oftenas every other element.) quence definedby ao = 1, al = 2, and an = an-I'a n-2, 15. Devise a recursive algorithm for computing the greatest for n = 2, 3,4, .... common divisor of two nonnegative integers a and b with 30. Devise an iterative algorithm to findthe nth term of the a < b using the fact that gcd(a, b) = gcd(a, b - a). sequence defined in Exercise 29. 322 4 / Induction andRecursion 4-60

31. Is the recursive or the iterative algorithm for finding the 46. How many comparisons are required to merge these pairs sequence in Exercise 29 more efficient? of lists using Algorithm 10? 32. Devise a recursive algorithm to find the nth term of a) 1,3, 5, 7, 9; 2, 4, 6, 8, 10 the sequence defined by ao = I, al = 2, a2 = 3, and b) 1, 2,3,4, 5; 6, 7, 8, 9, 10 an = an-I + an-2 + an-3, for n = 3,4, 5, ... . c) 1, 5,6,7,8; 2, 3, 4, 9, 10 33. Devise an iterative algorithm to findthe nth term of the 47. Show that for all positive integers m and n there are lists sequence defined in Exercise 32. with m elements and n elements, respectively, such that 34. Is the recursive or the iterative algorithm for findingthe Algorithm lOuses m + n - 1 comparisons to merge them sequence in Exercise 32 more efficient? into one sorted list. 35. Give iterative and recursive algorithms for findingthe nth *48. What is the least number of comparisons needed to merge term of the sequence definedby ao = I, al = 3, a2 = 5, anytwo lists in increasing order into one list in increasing . . order when the number of elements in the two lists are and an = an-I a�_2 a�_3. Which is more efficient? 36. Give a recursive algorithm to findthe number of partitions a) I,4? b) 2, 4? c) 3, 4? d) 4, 4? of a positive integer based on the recursive definitiongiven *49. Prove that the merge sort algorithm is correct. in Exercise 47 in Section 4.3. The quicksort is an efficientalgorithm. To sorta , a , ..., a , 37. l 2 n Give a recursive algorithm for findingthe reversal ofa bit this algorithm begins by taking the firstelement al and form- string. (See the definitionof the reversal of a bit string in � ing two sublists, the first containing those elements that are the preamble of Exercise 34 in Section 4.3.) i less than aI, in the order they arise, and the second contain­ 38. Give a recursive algorithm for finding the string W , the ing those elements greater than aI, in the order they arise. concatenation of i copies of w, when w is a bit string. Then al is put at the end of the first sublist. This proce­ 39. Prove that the recursive algorithm for findingthe reversal dure is repeated recursively for each sublist, until all sub­ of a bit string that you gave in Exercise 37 is correct. lists contain one item. The ordered list of n items is ob­ 40. Prove that the recursive algorithm for findingthe concate­ tained by combining the sublists of one item in the order they nation of i copies of a bit string that you gave in Exercise occur. 38 is correct. 50. Sort 3, 5, 7, 8, 1, 9,2,4,6 using the quick sort. *41. Give a recursive algorithm for tiling a 2n x 2n checker­ 51. Letal , a2, ..., an be a list ofn distinct real numbers. How board with one square missing using right triominoes. many comparisons are needed to form two sublists from 42. Give a recursive algorithm for triangulating a simple poly­ this list, the first containing elements less than al and the gon with n sides, using Lemma I in Section 4.2. second containing elements greater than al ? 43. Give a recursive algorithm for computing values of the 52. Describe the quick sort algorithm using pseudocode. Ackermann function. [Hint: See the preamble to Exer­ 53. What is the largest number of comparisons needed to or­ cise 48 in Section 4.3.] der a list of four elements using the quick sort algorithm? 44. Use a merge sort to sort 4, 3, 2, 5, 1, 8, 7, 6. Show all the 54. What is the least number of comparisons needed to order steps used by the algorithm. a list of four elements usingthe quick sort algorithm? 45. Use a merge sort to sort b, d, a,f,g, h, Z, p, 0, k. Show all 55. Determine the worst-case complexity of the quick sort the steps used by the algorithm. algorithm in terms of the number of comparisons used.

4.5 Program Correctness

Introduction

Suppose that we have designed an algorithm to solve a problem and have written a program to implement it. How can we be sure that the program always produces the correct answer? After all the bugs have been removed so that the syntax is correct, we can test the program with sample input. It is not correct if an incorrect result is produced for any sample input. But even if the program gives the correct answer for all sample input, it may not always produce the correct answer (unless all possible input has been tested). We need a proof to show that the program gives the correct output. alwaysProgram verification,the proof of correctness of programs, uses the rules of inference and proof techniques described in this chapter, including mathematical induction. Because an incor­ rect program can lead to disastrous results, a large amount of methodology has been constructed for verifyingprograms. Effortshave been devoted to automating program verificationso that it 4-67 Supplementary Exercises 329 recursive definitionof a set: a definitionof a set that specifies iteration: a procedure based on the repeated use of operations an initial set of elements in the set and a rule for obtaining in a loop other elements fromthose in the set program correctness: verification that a procedure always structural induction: a technique for proving results about produces the correct result recursively definedsets loop invariant: a property that remains true during every recursive algorithm: an algorithm that proceeds by reducing traversal of a loop a problem to the same problem with smaller input initial assertion: the statement specifYingthe properties ofthe merge sort: a sorting algorithm that sorts a list by splitting it input values of a program in two, sorting each of the two resulting lists, and merging final assertion: the statement specifYing the properties the the results into a sorted list output values should have if the program worked correctly

Review Questions

1. a) Can you use the principle of mathematical induction 8. a) Explain why a sequence an is well defined if it is de­ to finda formula for the sum of the firstn terms of a finedrecursively by specifYinga I and a2 and a rule for sequence? findinga n froma I , a2, ..., an-I for n = 3,4, 5, .... b) Can you use the principle of mathematical induction b) Find the value of an if al = 1, a2 = 2, and an = to determine whether a given formula for the sum of an-I +an-2 + ... +al , for n = 3,4, 5, .... the firstn terms of a sequence is correct? 9. Give two examples of how well-formed formulae are c) Find a formula for the sum of the first n even positive integers, and prove it using mathematical defined recursively for different sets of elements and induction. operators. n 10. a) Give a recursive definitionof the length of a string. 2. a) For which positive integers n is lln + 17 ::: 2 ? Use the recursive definition from part (a) and struc­ b) Prove the conjecture you made in part (a) using math­ b) ematical induction. tural induction to prove that l(xy) = l(x) + l(y). 11. 3. a) Which amounts of postage can be formed using only a) What is a recursive algorithm? 5-cent and 9-cent stamps? b) Describe a recursive algorithm for computing the sum b) Prove the conjecture you made using mathematical of n numbers in a sequence. induction. 12. Describe a recursive algorithm for computing the greatest c) Prove the conjecture you made using strong induction. common divisor of two positive integers. d) Find a proof of your conjecture differentfrom theones 13. a) Describe the merge sort algorithm. you gave in (b) and (c). b) Use the merge sort algorithm to put the list 4, 10, 1,5, 4. Give two different examples. of proofs that use strong 3, 8, 7, 2, 6, 9 in increasing order. induction. c) Give a big-O estimate for the number of comparisons 5. a) State the well-ordering property for the set of positive used by the merge sort. integers. 14. a) Does testing a computer program to see whether it b) Use this property to show that every positive integer produces the correct output for certain input values can be written as the product of primes. verifY that the program always produces the correct 6. a) Explain why a functionis well-definedif it is defined output? recursively by specifYingf( l) and a rule for finding b) Does showing that a computer program is partially f(n) from f(n - 1). correct with respect to an initial assertion and a final b) Provide a recursive definitionof the function f(n) = assertion verifYthat the program always produces the (n + I)!. correct output? If not, what else is needed? 7. Give a recursive definitionof the Fibonacci numbers. 15. What techniques can you use to show that a long com­ a) n-2 b) Show that fn > a whenever n ::: 3, where fn is puter program is partially correct with respect to an initial the nth term of the Fibonacci sequence and a = assertion and a finalassertion? (l + ./5)/2. 16. What is a loop invariant? How is a loop invariant used?

Supplementary Exercises

1. Use mathematical induction to show that + + 2. Use mathematical induction to show that 13 + 33 + 53 + �3 �9 2 2 + ... + - ... + (2n + 1)3 = (n + 1) (2n + 4n + 1) whenevern is 1..27 1..3" - 1 1..3" whenever n is a positive integer. a positive integer. 330 4 / Induction andRecursion 4-68

1 3. Use mathematical induction to show that 1 . 20 + 2 . 2 + *18. Determine which Fibonacci numbers are divisible by 3. 3 . 22 + ... + n ·2n-1 = (n - 1) · 2n + 1 whenevern isa Use a form of mathematical induction to prove your positive integer. conjecture. 4. Use mathematical induction to show that *19. Prove that lkln + lk+l/n+1 = In+k+1 for all nonnegative integers n, where k is a nonnegative integer and denotes 1 lin J; - + - + ... + = -- the ith Fibonacci number. 1 . 3 3 . 5 (2n - 1 )(2n + 1) 2n + 1 The sequence of Lucas numbers is definedby 10 = 2, h = 1, and I = I I + I for n = 2, 3,4, .... whenever n is a positive integer. n n- n-2 20. Show that I + I = I 1 whenever n is a positive in­ 5. Show that n n+2 n+ teger, where J; and Ii are the ith Fibonacci number and 1 lin ith Lucas number, respectively. - - ... + + + = -- 21. ... 1·4 4·7 (3n - 2)(3n +l) 3n +l Show that 15 + It + + I� = Inln+1 + 2 whenever n is a nonnegative integer and Ii is the ith Lucas whenever n is a positive integer. number. 6. n 2 Use mathematical induction to show that 2 > n + n *22. Use mathematical induction to show that the product of whenever n is an integer greater than 4. any n consecutive positive integers is divisible by n!. n 3 7. Use mathematical induction to show that 2 > n when­ [Hint: Use the identity m(m + 1)· ··(m +n - 1)/n! = ever n is an integer greater than 9. (m - l)m(m + 1)···(m + n - 2)/n! + m(m+ 1)···(m n 4 + n - 2)/(n - I)!.] 8. Find an integer N such that 2 > n whenever n is greater than N. Prove that your result is correct using mathemat­ 23. Use mathematical induction to show that (cos x + ical induction. i sin x)n =-cos nx + i sin nx whenever n is a positive in­ 9. Use mathematical induction to prove that a - b is a factor teger. (Here i is the square root of -1.) [Hint: Use of an - bn whenever n is a positive integer. the identities cos( a + b) = cos a cos b - sin a sin b and 10. Use mathematical induction to prove that 9 divides n3 + sin(a + b) = sin a cos b + cosa sin b.] (n + 1)3 + (n + 2)3 whenever n is a nonnegative integer. *24. Use mathematical induction to show that LJ = I cos j X = 11. Use mathematical induction to prove this formula for the cos[(n + l)x /2] sin(nx /2)/ sin(x /2) whenever n is a pos­ sum of the terms of an arithmetic progression. itive integer and sin(x /2) :/= O. 25. 2 j ... Use mathematical induction to prove that LJ=I j 2 = a + (a + d) + + (a + nd) = n22n+1 - n2n+2 + 3· n+1 - 6 (n + 1)(2a + nd)/2 2 for every positive inte­ ger n. 26. 12. Suppose that aj == bj (mod m) for j = 1,2, ..., n. Use (Requires calculus) Suppose that the sequence mathematical induction to prove that XI, X2, ••• , Xn , •.• is recursively defined by XI = 0 and n n Xn+l = "';xn + 6. a) L aj == L bj (mod m). Use mathematical induction to show that XI < X < j = j =1 a) 2 1 ...... n n < Xn < , that is, the sequence {xn } is monoton­ ically increasing. b) n aj == n bj (mod m). j = 1 j =1 b) Use mathematical induction to prove that Xn < 3 for 13. Show that if n is a positive integer, then n = 1,2, .... c) Show that limn-HXl Xn = 3. n k + 4 n(3n + 7) 27. Show if n is a positive integer with n :::: 2, then Lk= I = k(k + 1)(k + 2) 2(n + 1)(n + 2) 1 (n - 1)(3n + 2) 2 t __ = 14. n n + 6 < (n - 8n)/16? 2 For which positive integers is j= j - 1 4n(n + 1). Prove your answer using mathematical induction. 2

15. (Requires calculus) Suppose that I(x) = � and g(x) = 28. Use mathematical induction to prove Theorem 1 in Sec­ xeX• Use mathematical induction together with the tion 3.6, that is, show if b is a positive integer, b > 1, and product rule and the fact that I'(x) = � to prove that n is a positive integer, then n can be expressed uniquely g(n)(x) = (x + n)eX whenever n is a positive integer. in the form n = akbk + ak_Ibk-1 + ... + alb + ao. 16. (Requires calculus) Suppose that I(x) = � and g(x) = *29. A lattice point in the plane is a point (x , y) where both x eCx, where c is a constant. Use mathematical induction and y are integers. Use mathematical induction to show together with the chain rule and the fact that I' (x) = that at least n + 1 straight lines are needed to ensure n n � to prove that g( ) = c ecx whenever n is a positive that every lattice point (x , y) with x :::: 0, y :::: 0, and integer. x + y :'S n lies on one of these lines. 17. Determine which Fibonacci numbers are even, anduse a 30. (Requires calculus) Use mathematical induction and the form of mathematical induction to prove your conjecture. product rule to show that if n is a positive integer and 4-69 Supplementary Exercises 331

f,(x), fz(x), ..., fn(x), are all differentiable functions, o < p / q < I as the sum of distinct unit fractions.At each then step of the algorithm, we findthe smallest positive integer (f,(x)fz(x) · ··f n(x))' n such that 1/n can be added to the sum without exceed­ q. f,(x)fz(x) · ··f n(x) ing p / For example, to express 517 we first start the f (x) f (x) f (x) sum with 1/2. Because 5/7 - 1/2 3/14 we add 1/5 to { + � + ... + � the sum because 5 is the smallest positive= integer k such f,(x) (x) (x) · = fz fn that 1/k < 3/14. Because 3/14 - 1/5 1/70, the a1go­ 31. (Requires material in Section 3. 7) Suppose that B rithm terminates, showingthat 5j7 1/=2 + 1/5 + 1170. ' where and are n x n matrices and is Let T (p) be the statement that this= algorithm terminates MAM- , A B M = invertible. Show that Bk MAkM-' for all positive in­ for all rational numbers p / q with 0 < p / q < 1. We will tegers k. = prove that the algorithm always terminates by showing 32. Use mathematical induction to show that ifyou draw lines that T(p) holds for all positive integers p. in the plane you only need two colors to color the regions a) Show that the basis step T(l) holds. formed so that no two regions that have an edge in com­ b) Suppose that T(k) holds for positive integers k with mon have a common color. k < p. That is, assume that the algorithm terminates r, < 33. Show that n! can be represented as the sum of n of its for all rational numbers k / where 1 � k p. Show that if we start with p / q and the fraction 1/n is se­ distinct positive divisors whenever n � 3. [Hint: Use in­ ductive loading. First try to prove this result using mathe­ lected in the first step of the algorithm, then p / q matical induction. By examining where your proof fails, p' /q' + l/n, where p' np - q and q' nq. After= find a stronger statement that you can easily prove using considering the case where= p/q l/n, =use the in­ mathematical induction.] ductive hypothesis to show that the= greedy algorithm terminates when it begins with p' / q' and complete the *34. ••• Use mathematical induction to prove that ifx" X2, , Xn inductive step. are positive real numbers with n � 2, then The McCarthy 91 functioQ.(defined by John McCarthy, one of the founders of artificialintelligence) is definedusing the rule n -10 ifn > 100 M(n) { n n = M(M( + 11)) if � 100 for all positive integers n. 35. Use mathematical induction to prove that ifn people stand in a line, where n is a positive integer, and ifthe firstper­ 41. By successively using the definingrule for M(n), find M(102). M(101). son in the line is a woman and the last person in line is a a) b) c) M(99). man, then somewhere in the line there is a woman directly d) M(97). e) M(87). t) M(76). in frontof a man. *>il:t2. Show that the function M(n) is a well-defined function *36. Suppose that for every pair of cities in a country there is a fromthe set of positive integers to the set of positive inte­ direct one-way road connecting them in one direction or gers. [Hint:Prove that M(n) 91 for all positive integers the other. Use mathematical induction to show that there n with n � 101.] = is a city that can be reached from every other city either 43. Is this proofthat directly or via exactly one other city. -1 + -1 + ... + 1 - --3 37. Use mathematical induction to show that when n circles 1 ·2 2·3 (n - l)n - 2 - n divide the plane into regions, these regions can be col­ whenever n is a positive integer, correct? Justify your ored with two differentcolors such that no regions with a answer. common boundary are colored the same. Basis step: The result is true when n 1 because *38. Suppose that among a group of cars on a circular track 1 3 1 = there is enough fuel for one car to complete a lap. Use mathematical induction to show that there is a car in the 1·2 2 group that can complete a lap by obtaining gas fromother Inductive step: Assume that the result is true for n. Then cars as it travels around the track. -1 + - 1 + ... + 1 + 1 39. Show that if n is a positive integer, then 1·2 2·3 (n - 1)n ---n(n+1) 1 (2j - 1) 1/ k n(n + 1)/2. � _ .!.+ _ __ 'tt (j;; ) = = 2 n (.!.n n+1) 3 1 40. A unit or Egyptian fraction is a fraction of the form - --n + l/n, where n is a positive integer. In this exercise, we = 2 - 1 will use strong induction to show that a greedy algorithm Hence, the result is true for n + 1 if it is truefor n. This can be used to express every rational number p / q with completes the proof. 332 4 / Induction and Recursion 4-70

2. 44. Suppose that AI. A2, ..., An are a collection of sets. fen - 1) + 2n - 1 for n ::: Prove your result using Suppose that R2 = Al EB A2 and Rk = Rk-I EB Ak for mathematical induction. k = 3,4, ..., n. Use mathematical induction to prove that **52. Give a recursive definition of the set of bit strings that x E Rn if and only if x belongs to an odd number of the contain twice as many Os as 1 s. sets A I, A2, ... , An . (Recall that S EB T is the symmetric 53. Let S be the set of bit strings defined recursively by difference of the sets S and T defined in Section 2.2.) A E S and Ox E S, x I E S if XES, where A is the empty 2 2 *45. Show that n circles divide the plane into n - n + re­ string. gions if every two circles intersect in exactly two points Find all strings in S oflength not exceeding five. and no three circles contain a common point. a) b) Give an explicit description of the elements of S. *46. Show that n planes divide three-dimensional space into 3 54. Let S be the set of strings definedrecursively by abc E S, (n + 5n + 6)/6 regions ifany three of these planes have bac E S, and acb E S, where a, b, and c are fixedletters; a point in common and no four contain a common point. and for all XES, abcx E S; abxc E S, axbc E S, and *47. Use the well-ordering property to show that .../i is irra­ xabc E S, where x is a variable representing a string of tional. [Hint: Assume that .../iis rational. Show that the letters. set of positive integers of the form b.../i has a least el­ a) Find all elements of S oflength eight or less. ement a. Then show that a.../i- a is a smaller positive b) Show that every element of S has a length divisible by integer of this form.] three. 48. A set is well ordered if every nonempty subset of this The set B of all balanced strings of parentheses is defined set has a least element. Determine whether each of the recursively by A E B, where A is the empty string; (x) E B, following sets is well ordered. xy E B ifx, Y E B. a) the set of integers 55. Show that (00) is a balanced string of parentheses and b) the set of integers greater than -100 (0» is not a balanced string of parentheses. c) the set of positive rationals 56. Find all balanced strings of parentheses with exactly six d) the set of positive rationals with denominator less than symbols. 100 57. Find all balanced strings of parentheses with four or fewer 49. a) Show that if aI, a2, ..., an are positive integers, then symbols. gcd(al ' a2, ..., an-I, an) = gcd(al' a2, ..., an-2, 58. Use induction to show that if x is a balanced string of gcd(an_I ' an». parentheses, then the number of leftparenthe ses equals b) Use part (a), together with the Euclidean algorithm, to the number of right parentheses in x. develop a recursive algorithm for computing the great­ Definethe function on the set ofstrings ofparentheses by est common divisor of a set of n positive integers. N *50. Describe a recursive algorithm for writing the greatest N(A) = 0, N(O = 1, N O) = -I, common divisor of n positive integers as a linear combi­ N(uv) = N(u) + N(v), nation of these integers. where A is the empty string, and u and v are strings. It can be 51. Find an explicit formula for f(n) iff(l) = 1 and f(n) = shown that N is well defined. unkS� JOHN McCARTHY (BORN 1927) John McCarthy was born in Boston. He grew up in Boston and in Los Angeles. He studied mathematics as both an undergraduate and a graduate student, receiving his B.S. in 1948 from the California Institute of Technology and his Ph.D. in 1951 from Princeton. After graduating from Princeton, McCarthy held positions at Princeton, Stanford, Dartmouth, and M.1.T. He held a position at Stanford from 1962 until 1994, and is now an emeritusprof essor there. At Stanford, he was the director of the Artificial Intelligence Laboratory, held a named chair in the School of Engineering, and was a senior fe llow in the Hoover Institution. McCarthy was a pioneer in the study of artificial intelligence, a term he coined in 1955. He worked on problems related to the reasoning and information needs required for intelligent computer behavior. Mc­ Carthy was among the firstcomputer scientists to design time-sharing computer systems. He developed LISp, a programming language for computing using symbolic expressions. He played animportant role in using logic to verifythe correctness of computer programs. McCarthy has also worked on the social implications of computer technology. He is currently working on the problem of how people and computers make conjectures through assumptions that complications are absent from situations. McCarthy is an advocate of the sustainability of human progress and is an optimist about the futureof humanity. He has also begun writing science fiction stories. Some of his recent writing explores the possibility that the world is a computer program written by some higher force. Among the awards McCarthy has won are the Turing Award from the Association for Computing Machinery, the Research Excellence Award of the International Conferenceon Artificial Intelligence,the Kyoto Prize, and the National Medal of Science. 7-23 7.2 Solving Linear Recurrence Relations 471

is a particular solution. Hence, all solutions ofthe original recurrence relation an = an I + n are given by = = c 1)/2. Because = 1, we have 1 = = c -1 · 2/2 = ) p) n(n c 1, so acn = a�hIt fo+ llowsa� that+ = +n( n 1)/2. (Thisa isl the same formula agivenl in+ Table 2 in Section+ 2.4 andO. derived previously.)an + ....

Exercises

1. Determine which ofthese are linear homogeneous recur­ caught in a year is the average of the number caught in rence relations with constant coefficients. Also, findthe the two previous years. degree ofthose that are. a) Find a recurrence relation for {Ln }, where Ln is the a) an = 3an-1 + 4an-2 + 5an-3 number oflobsters caught in year n, under the assump­ b) an = 2nan_1 + an-2 c) an = an-l + an-4 tion for this model. d) e) b) an = an-l + 2 an = a�_l + an-2 Find Ln if 100,000 lobsters were caught in year 1 and t) an = an-2 g) an = an-l + n 300,000 were caught in year 2. 2. Determine which ofthese are linear homogeneous recur­ 9. A deposit of $100,000 is made to an investment fundat rence relations with constant coefficients. Also, findthe the beginning of a year. On the last day of each year two degree of those that are. dividends are awarded. The first dividend is 20% of the amount in the account during that year. The second divi­ a) an = 3an-2 b) an = 3 c) an a _l d) an an-l + 2an-3 dend is 45% ofthe amount in the account in the previous = � = year. e) an = an-l in a) t) an = an-l + an-2 + n + 3 Find a recurrence relation for {Pn }, where Pn is the g) an = 4an-2 + 5an-4 + 9an-7 amount in the account at the end ofn years if no money is ever withdrawn. 3. Solve these recurrence relations together with the initial conditions given. b) How much is in the account aftern years if no money has been withdrawn? a) an = 2an-1 for n ::: 1, ao = 3 *10. Prove Theorem 2. b) an = an-l for n ::: 1, ao = 2 c) Lucas numbers an = 5an-1 - 6an-2 for n ::: 2, ao = 1, al = 0 11. The satisfythe recurrence relation d) an = 4an-1 - 4an-2 for n ::: 2, ao = 6, al = 8 e) an = -4an-1 - 4an-2 for n ::: 2, ao = 0, al = 1 t) an = 4an-2 for n ::: 2, ao = 0, al = 4 g) an = an-2 14 for n ::: 2, ao = 1, al = 0 and the initial conditions Lo = 2 and Ll = 1. a) 4. Solve these recurrence relations together with the initial Show that Ln = fn-l + fn+l for n = 2, 3, ... , where conditions given. fn is the nth Fibonacci number. a) b) Find an explicit formula for the Lucas numbers. an = an-l + 6an-2 for n ::: 2, ao = 3, al = 6 b) Find the solution to a 2a + a - 2a for n an = 7an-1 - lOan-2 for n ::: 2, ao = 2, al = I 12. n = n-1 n-2 n-3 = c) 3,4, 5, ..., with ao 3, al 6, and a O. an = 6an-1 - 8an-2 for n ::: 2, ao = 4, al = 10 = = 2 = d) an = 2an-1 - an-2 for n ::: 2, ao = 4, al = 1 13. Find the solution to an = 7an-2 + 6an-3 with ao = 9, e) an = an-2 for n ::: 2, ao = 5, al = - 1 al = 10, and a2 = 32. t) a -6a - 9a - for n ::: 2, ao 3, al -3 n = n-1 n 2 = = 14. Find the solution to an = 5an-2 - 4an-4 with ao = 3, g) an 2 -4an + 5an for n ::: 0, ao 2, al 8 + = +1 = = al = 2, a2 = 6, and a3 = 8. How many different messages can be transmitted in n 5. 15. Find the solution to an = 2an-1 + 5an-2 - 6an-3 with microseconds using the two signals described in Exercise ao 7, al -4, and a2 8. 35 in Section 7.1? = = = *16. Prove Theorem 3. 6. How many different messages can be transmitted in n 17. Prove this identity relating the Fibonacci numbers and the microseconds using three different signals if one signal binomial coefficients: requires 1 microsecond for transmittal, the other two sig­ nals require 2 microseconds each for transmittal, and a fn l C(n, 0) + C(n - 1, 1) + ... + C(n - k, k), signal in a message is followed immediately by the next + = signal? where n is a positive integer and k = Ln 12J. [Hint: Let In how many ways can a 2 x n rectangular checkerboard 7. an = C(n, 0) + C(n - 1, 1) + ...+ C(n - k, k). Show be tiled using 1 x 2 and 2 x 2 pieces? that the sequence {an} satisfies the same recurrence re­ 8. A model for the number of lobsters caught per year is lation and initial conditions satisfiedby the sequence of based on the assumption that the number of lobsters Fibonacci numbers.] 472 7 / Advanced Counting Techniques 7-24

18. Solve the recurrence relation an = 6an-1 - 12an-2 + b) Find the solution of the recurrence relation in part (a) 8an-3 with ao = -5, al = 4, and a2 = 88. with initial condition al = 5. 19. Solve the recurrence relation an = -3an-1 - 3an-2 - 30. a) Find all solutions of the recurrence relation an = an-3 with ao = 5, al = -9, and a2 = 15. -5an_1 - 6an-2 + 42 . 4n. b) 20. Find the general form of the solutions of the recurrence Find the solution of this recurrence relation with relation an = 8an-2 - 16an_4. al = 56 and a2 = 278. 21. What is the general form of the solutions ofa linear homo­ 31. Find all solutions ofthe recurrence relation an = 5an-1 - geneous recurrence relation if its characteristic equation 6an-2 + 2n + 3n. [Hint: Look for a particular solution of has roots I, I, I, I, -2, -2, -2, 3, 3, -4? the form qn2n + Pin + P2, where q, PI, and P2 are con­ stants.] 22. What is the general form ofthe solutions of a linear homo­ geneous recurrence relation if its characteristic equation 32. Find the solution ofthe recurrence relation an = 2an-1 + has the roots -I, - I, -1,2,2,5, 5, 7? 3·2n. Find all solutions of the recurrence relation an = 4an_1 - 23. Consider the nonhomogeneous linear recurrence relation 33. n an = 3an-1 + 2n . 4an-2 + (n + 1)2 . n 1 a) Show that an = _2 + is a solution ofthis recurrence 34. Find all solutions of the recurrence relation an = 7an_1 - relation. 16an_2 + 12an-3 + n4n with ao = -2, al = 0, and b) Use Theorem 5 to findall solutions of this recurrence a2 = 5. relation. 35. Find the solution ofthe recurrence relation an = 4an-1 - e) Find the solution with ao = 1. 3an-2 + 2n + n + 3 with ao = I and al = 4. Consider the nonhomogeneous linear recurrence relation 24. n Let an be the sum of the first n perfect squares, that an = 2an_1 +2 . 36. 2 n is, an = I:Z= J k . Show that the sequence {an l satis­ a) Show that an = n2 is a solution of this recurrence fiesthe linear nonhomogeneous recurrence relation an = relation. an-I + n2 and the initial condition aJ = 1. Use Theorem b) Use Theorem 5 to findall solutions of this recurrence 6 to determine a formula for an by solving this recurrence relation. relation. Find the solution with ao = 2. e) Let an be the sum of the firstn triangular numbers, that a) Determine values of the constants and B such 37. 25. A is, an = I:�=I tk, where tk = k(k + 1 )/2. Show that {anl that an = An + B is a solution of recurrence relation satisfies the linear nonhomogeneous recurrence relation an = 2an-1 +n + 5. an = an-I + n(n + 1)/2 and the initial condition al = 1. b) Use Theorem 5 to findall solutions ofthis recurrence Use Theorem 6 to determine a formula for an by solving relation. this recurrence relation. e) Find the solution of this recurrence relation with ao = 4. 38. a) Find the characteristic roots of the linear ho­ mogeneous recurrence relation an = 2an-1 - 2an-2. 26. What is the general form of the particular solution (Note: These are complex numbers.) guaranteed to exist by Theorem 6 of the linear nonho­ mogeneous recurrence relation a = 6a - 12a + b) Find the solution of the recurrence relation in part (a) n n-1 n-2 with ao = I and aJ = 2. 8an-3 + F(n) if a) Find the characteristic roots ofthe linear homogeneous a) F(n) = n2? b) F(n) = 2n? *39. recurrence relation a = a (Note: These include F(n) = n2n? d) F(n) = (-2t? n n-4. e) complex numbers.) e) F(n) = n22n? t) F(n) = n3( _2)n? b) Find the solution of the recurrence relation in part (a) g) F(n) = 3? with ao = I, al = 0, a2 = -I, and a3 = 1. 27. What is the general form ofthe particular solution guaran­ teed to exist by Theorem 6 of the linear nonhomogeneous *40. Solve the simultaneous recurrence relations recurrence relation an = 8an-2 - 16an-4 + F(n) if a = 3a -J + 2b J 3 n n n- a) F(n) = n ? b) F(n) = (-2t? b = a + 2b _ F(n) = n2n? d) F(n) = n24n? n n-I n 1 e) 2 4 n e) F(n) = (n - 2)( -2t? t) F(n) = n 2 ? with ao = I and bo = 2. g) F(n) =2? a) Use the formula found in Example 4 for in, the nth a) Find all solutions of the recurrence relation an = *41. 28. 2 Fibonacci number, to show that in is the integer closest 2an_1 +2n . to b) Find the solution of the recurrence relation in part (a) with initial condition al = 4. 29. a) Find all solutions of the recurrence relation an = 2an_1 +3n. 7-25 7.2 Solving Linear Recurrence Relations 473

b) Determine for which n fn is greater than end of each year her salary will be double her salary of the previous year, with an extra increment of $10,000 for n 1 1 vIs each year she has been with the company. _ + ( 2 ) a) Construct a recurrence relation for her salary for her vis nth year of employment. b) Solve this recurrence relation to findher salary for her and for which n fn is less than nth year of employment. Some linear recurrence relations that do not have constant co­ efficients can be systematically solved. This is the case for recurrence relations of the form f(n )an g(n )an-l + h(n). Exercises 48-50 illustrate this. = *48. a) Show that the recurrence relation 42. Show that if an = an-l + an-2, ao = s and al = t, where s and t are constants, then an = Sfn-l + tfn for all posi­ tive integers n. f(n)an = g(n)an-l + h(n), 43. Express the solution of the linear nonhomogenous re­ currence relation an = an-l + an-2 + I for n :::: 2 where for n :::: 1, and with ao = C, can be reduced to a re­ ao = 0 and al = I in terms of the Fibonacci numbers. currence relation of the form [Hint: Let bn = an + 1 and apply Exercise 42 to the se­ quence bn.] bn = bn-1 + Q(n)h(n), *44. (Linear algebra required) Let An be the n x n matrix with 2s on its main diagonal, 1 s in all positions next to a where bn g(n I)Q(n l)an, with diagonal element, and Os everywhere else. Find a recur­ = + + rence relation for dn, the determinant of An. Solve this Q(n) (f(1)f(2) ···f(n - 1»j(g(l)g(2) · ··g(n ». recurrence relation to finda formula for dn• = 45. Suppose that each pair of a genetically engineered species of rabbits lefton an island produces two new pairs ofrab­ b) Use part (a) to solve the original recurrence relation bits at the age of I month and six new pairs of rabbits at to obtain the age of2 months and every month afterward. None of C + ,£7- 1 Q(i)h(i) the rabbits ever die or leave the island. a . n g(n I)Q(n 1) a) Find a recurrence relation for the number of pairs of = + + rabbits on the island n months after onenewborn pair is lefton the island. *49. Use Exercise 48 to solve the recurrence relation (n + 1) b) By solving the recurrence relation in (a) determine the an = (n + 3)an-l + n, for n :::: 1, with ao = 1. number of pairs of rabbits on the island n months after 50. It can be shown that the average number ofcomparisons one pair is lefton the island. made by the quick sort algorithm (described in preamble 46. Suppose that there are two goats on an island initially. to Exercise 50 in Section 4.4), when sorting n elements The number of goats on the island doubles every year by in random order, satisfiesthe recurrence relation natural reproduction, and some goats are either added or removed each year. 2 n-l a) C n 1 L C Construct a recurrence relation for the number of goats n = + + n k on the island at the start ofthe nth year, assuming that - k=O during each year an extra 100 goats are put on the island. for n 1,2, ..., with initial condition Co O. Solve the recurrence relation frompart (a) to findthe = b) a) Show that {Cn} also satisfiesthe recurrence= relation number of goats on the island at the start of the nth nC (n I)C 2n for 1,2, .... year. n = + n-1 + n = c) Construct a recurrence relation for the number of goats b) Use Exercise 48 to solve the recurrence relation in part on the island at the start of the nth year, assuming (a) to findan explicit formula for Cn. that n goats are removed during the nth year for each **51. Prove Theorem 4. n 3. **52. Prove Theorem 6. :::: 2 d) Solve the recurrence relation in part (c) for the number 53. Solve the recurrence relation T(n) nT (nj2) with ini­ of goats on the island at the start of the nth year. k tial condition T (1) = 6. [Hint: Let n= = 2 and then make 47. A new employee at an exciting new software company the substitution ak log T (2k) to obtain a linear nonho­ starts with a salary of$50,000 and is promised that at the mogeneous recurrence= relation.] 482 7 / Advanced Counting Techniques 7-34

where f(2) = exceeds the number of comparisons needed to solve the closest-pair problem for n points. By1, the Master Theorem (Theorem 2), it fo llows that fen) is O(n log n). The two sorts of points by their x coordinates and by their y coordinates each can be done using 0 (n log n) comparisons, by using the merge sort, and the sorted subsets ofthese coordinates at each ofthe O(log n) steps of the algorithm can be done using O(n) comparisons each. Thus, we find that the closest-pair problem can be solved using O(n log n) comparisons. ....

Exercises

1. How many comparisons are needed for a binary search in particular officeare the elements of a sequence. A person a set of 64 elements? wins the election ifthis person receives a majority of the 2. How many comparisons are needed to locate the max­ votes. imum and minimum elements in a sequence with 128 a) Devise a divide-and-conquer algorithm that deter­ elements using the algorithm in Example 2? mines whether a candidate received a majority and, 3. Multiply (11 1O)z and (101O)z using the fast multiplication ifso, determine who this candidate is. [Hint: Assume algorithm. that n is even and split the sequence of votes into Express the fast multiplication algorithm in pseudocode. two sequences, each with n /2 elements. Note that a 4. candidate could not have received a majority of votes 5. Determine a value for the constant C in Example 4 and without receiving a majority of votes in at least one of use it to estimate the number of bit operations needed to the two halves.] multiply two 64-bit integers using the fast multiplication b) Use the Master Theorem to estimate the number of algorithm. comparisons needed by the algorithm you devised in 6. How many operations are needed to mUltiply two 32 x 32 part (a). matrices using the algorithm referred to in Example 4? 18. Suppose that each person in a group of n people votes 7. Suppose that fen) = f(n/3) + 1 when n is divisible by for exactly two people from a slate of candidates to fill 3, and f(l) = 1. Find two positions on a committee. The top two finishersboth a) f(3). b) f(27). c) f(729). win positions as long as each receives more than n /2 8. Suppose that fen) = 2f(n/2) + 3 when n is even, and votes. f(1) 5. Find = a) Devise a divide-and-conquer algorithm that deter­ a) f(2). b) f(8). c) f(64). d) f(1024). 2 mines whether the two candidates who received the 9. Suppose that fen) = f(n/5) + 3n when n is divisible most votes each received at least n /2 votes and, if so, by 5, and f(l) = 4. Find determine who these two candidates are. a) f(5). b) f(125). c) f(3125). b) Use the Master Theorem to estimate the number of k 10. Find f(n) when n = 2 , where f satisfiesthe recurrence comparisons needed by the algorithm you devised in relation fen) = f(n/2) + 1 with f(1) = 1. part (a). 11. Estimate the size of f in Exercise 10 if f is an increasing 19. a) Set up a divide-and-conquer recurrence relation for n function. the number of multiplications required to compute x , where is a real number and n is a positive integer, 12. Find f(n) when n = 3k, where f satisfiesthe recurrence x relation fen) 2f(n/3) 4 with f(l) 1. using the recursive algorithm from Exercise 26 in Sec­ = + = tion 4.4. 13. Estimate the size of f in Exercise 12 if f is an increasing function. b) Use the recurrence relation you found in part (a) to construct a big-O estimate for the number of mul­ Suppose that there are n 2k teams in an elimination 14. = tiplications used to compute n using the recursive tournament, where there are n /2 games in the firstround, x -1 algorithm. with the n /2 = 2k winners playing in the second round, and so on. Develop a recurrence relation for the number 20. a) Set up a divide-and-conquer recurrence relation for:the of rounds in the tournament. number of modular multiplications required to com­ n pute a mod m, where a, m, and n are positive inte­ 15. How many rounds are in the elimination tournamentde­ gers, using the recursive algorithms from Example 3 scribed in Exercise 14 when there are 32 teams? in Section 4.4. 16. Solve the recurrence relation for the number of rounds in b) Use the recurrence relation you found in part (a) to the tournamentdescribed in Exercise 14. construct a big-O estimate for the number of modular n 17. Suppose that the votes of n people for different candi­ multiplications used to compute a mod m using the dates (where there can be more than two candidates) for a recursive algorithm. 7-35 7.3 Divide-and-Conquer Algorithmsand Recurrence Relations 483

21. Suppose that the functionf satisfiesthe recurrence rela­ *26. Use pseudocode to describe the recursive algorithm for tion fen) = 2f(Jn) + 1 whenever n is a perfect square solving the closest-pair problem as described in Exam­ greater than 1 and f(2) = 1. ple 12. a) Find f(16). 27. Construct a variation of the algorithm described in Ex­ b) Find a big-O estimate for f(n). [Hint: Make the sub­ ample 12 along with justifications of the steps used by stitution m = log n.] the algorithm to findthe smallest distance between two points if the distance between two points is definedto be 22. Suppose that the function f satisfies the recurrence re­ x d« x; , Y;), (Xj, Yj» = max(l ; - xj l, Iy; - Yj l)· lation fen) = 2f(Jn)+ log n whenever n is a perfect square greater than 1 and f(2) 1. *28. Suppose someone picks a number x froma set of n num­ = bers. A second person tries to guessthe number by succes­ a) Find f(16). sively selecting subsets of the n numbers and asking the b) Find a big-O estimate for fen). [Hint: Make the sub­ firstperson whether x is in each set. The firstperson an­ stitution m = log n.] swers either "yes" or "no." When the firstperson answers * This exercise deals with the problem offindingthe largest *23. � each query truthfully, we can findx using log n queries sum of consecutive terms of a sequence of n real numbers. by successively splitting the sets used in each query in When all terms are positive, the sum of all terms provides half. Ularn's problem, proposed by Stanislaw Ulam in the answer, but the situation is more complicated when 1976, asks for the number of queries required to findx, some terms are negative. For example, the maximum supposing that the first person is allowed to lie exactly sum of consecutive terms of the sequence -2, 3, - 1, 6, once. -7, 4 is 3 + (-1) + 6 = 8. (This exercise is based on a) Show that by asking each question twice, given a num­ [Be86].) ber x and a set with n elements, and asking one more a) Use pseudocode to describe an algorithm that solves question when we findthe lie, Ulam's problem can be this problem by findingthe sums of consecutive terms solved using 2 log n + 1 queries. starting with the first term, the sums of consecutive b) Show that by dividing the initial set of n elements into terms starting with the second term, and so on, keep­ four parts, each with n /4 elements, 114 ofthe elements ing track of the maximum sum found so far as the can be eliminated using two queries. [Hint: Use two algorithm proceeds. queries, where each of the queries asks whether the b) Determine the computational complexity of the al­ element is in the union of two of the subsets with n/4 gorithm in part (a) in terms of the number of sums elements and where one ofthe subsets of n /4 elements computed and the number of comparisons made. is used in both queries.] c) Devise a divide-and-conquer algorithm to solve this c) Show from part (b) that if fen) equals the number problem. [Hint: Assume that there are an even num­ of queries used to solve Ularn's problem using the ber ofterms in the sequence and split the sequence into method from part (b) and n is divisible by 4, then two halves. Explain how to handle the case when the fen) = f(3n/4) + 2. maximum sum of consecutive terms includes terms in d) Solve the recurrence relation in part (c) for f(n). both halves.] e) Is the naive way to solve Ulam's problem by asking d) Use the algorithm from part (c) to findthe maxi­ each question twice or the divide-and-conquer method mum sum of consecutive terms of each of the se­ based on part (b) more efficient? The most efficient quences: -2, 4,-1,3, 5,-6, 1,2;4, 1 ,-3, 7,-1,-5, way to solve Ularn'sproblem has been determined by 3, -2; and -1, 6, 3, -4, -5, 8, -1, 7. A. Pelc [Pe87]. e) Find a recurrence relation for the number of sums In Exercises 29-33, assume that f is an increasing func­ and comparisons used by the divide-and-conquer al­ tion satisfyingthe recurrence relation f(n) = af(n/b) + end, gorithm from part (c). where a ::: 1, b is an integer greater than 1, and e and d 1) Usethe Master Theorem to estimate the computational are positive real numbers. These exercises supply a proof of complexity of the divide-and-conquer algorithm. How Theorem 2. does it compare in terms of computational complexity *29. Show that if a bd and n is a power of b, then f(n) with the algorithm frompart (a)? = = f(l)nd + end 10gb n. d 24. Apply the algorithm described in Example 12 for finding 30. Use Exercise 29 to show that if a = b , then f(n) is the closest pair of points, using the Euclidean distance be­ O(nd logn). tween points, to findthe closest pair of the points (1, 3), Show that if a =1= bd and n is a power of b, then fen) *31. ] = (1, 7), (2, 4), (2, 9), (3, 1), (3, 5), (4, 3), and (4, 7). C]nd + C2n ogb a, where C] = bde/(bd - a) and C2 = f(l) bde/(a - bd). 25. Apply the algorithm described in Example 12 for finding + the closest pair of points, using the Euclidean distance be­ 32. Use Exercise 31 to show that if a < bd, then fen) is tween points, to findthe closest pair of the points (1, 2), O(nd). (1,6), (2, 4), (2, 8), (3, 1), (3, 6), (3, 10), (4, 3), (5, 1), Use Exercise 31 to show that if a>b d, then fen) is 33. ] (5, 5), (5, 9), (6, 7), (7, 1), (7, 4), (7, 9), and (8, 6). o (n ogb a). 484 7 / Advanced Counting Techniques 7-36

Find f(n) when n 4k, where f satisfiesthe recurrence Find f(n) when n 2k, where f satisfiesthe recurrence 34. = 36. = 2 relation f(n) = 5f(n/4) + 6n, with f(l) = 1. relation f(n) = 8f(n/2) + n with f(l) = 1. 35. Estimate the size of f in Exercise 34 if f is an increasing 37. Estimate the size of f in Exercise 36 if f is an increasing function. function.

7.4 Generating Functions

Introduction

Generating functions are used to represent sequences efficiently by coding the terms of a se­ unkS� quence as coefficients of powers of a variable in a fo rmal power series. Generating fu nctions can be used to solve many types of counting xproblems, such as the number of ways to select or distribute objects of different kinds, subject to a variety of constraints, and the number of ways to make change fo r a dollar using coins of different denominations. Generating functions can be used to solve recurrence relations by translating a recurrence relation fo r the terms of a sequence into an equation involving a generating function. This equation can then be solved to find a closed fo rm fo r the generating function. From this closed fo rm, the coefficientsof the power series for the generating function can be found, solving the original recurrence relation. Generating functions can also be used to prove combinatorial identities by taking advantage of relatively simple relationships between functionsthat can be translated into identities involving the terms of sequences. Generating functions are a helpfultool fo r studying many properties of sequences besides those described in this section, such as their use fo r establishing asymptotic fo rmulae for the terms of a sequence. We begin with the definitionof the generating functionfo r a sequence.

DEFINITION 1 The generatingfunction for the sequence ao, al," " ako ... of real numbers is the infinite series

k 00 k ... = alx akx = L akx . G(x) ao + + ... + + k = O

Remark: The generating functionfo r {ak} given in Definition is sometimes called the ordinary generating function of to distinguish it from other types1 of generating functions fo r this sequence. {ak}

EXAMPLE 1 The generating functions fo r the sequences with = = k + and = are I:r:'= I:r:'= o(k + and I:r:' respecti{a } vely.a 3, a 1, a 2k .... Exam=� 0 3xk, 1 )xk, = 0 2k xk, k k k k We can define generatingfunctions fo r finitesequences of real numbers by extending a finite sequence into an infinite sequence by setting = 0, = 0, and so on. The generating function of this infinite sequence {a } is a polynomial of degree because no ao, ai, ..., an n an+l an+2 n terms of the fo rm G(x)with j occur, that is, ajxj > n 7-67 Supplementary Exercises 515

b) Suppose the sets AI , A2, A3, and A4 each contain 25 b) How many ways are there to assign seven jobs to three elements, the intersection ofany two ofthese sets con­ employees so that each employee is assigned at least tains 5 elements, the intersection of any three of these one job? sets contains 2 elements, and 1 element is in all four 14. Explain how the inclusion-exclusion principle can be of the sets. How many elements arein the union of the used to count the number of primes not exceeding the four sets? positive integer n. 11. a) State the principle of inclusion-exclusion. b) Outline a proof of this principle. 15. a) Definea derangement. 12. Explain how the principle of inclusion-exclusion can be b) Why is counting the number of ways a hatcheck per­ used to count the number of onto functionsfrom a set son can returnhats to n people, so that no one receives

with m elements to a set with n elements. the correct hat, the same as counting the number of de­ rangements of n objects? 13. a) How can you count the number of ways to assign m jobs to n employees so that each employee is assigned c) Explain how to count the number of derangements of at least one job? n objects.

Supplementary Exercises

1. A group of 10 people begin a chain letter, with each per­ a) Set up a recurrence relation for the number of bacteria son sending the letter to four other people. Each ofthese present aftern hours. people sends the letter to four additional people. b) What is the solution of this recurrence relation? a) Find a recurrence relation for the number of letters c) When will the colony contain more than 1 million sent at the nth stage of this chain letter, if no person bacteria? ever receives more than one letter. 5. Messages are sent over a communications channel using b) What are the initial conditions for the recurrence re­ two differentsignal s. One signal requires 2 microseconds lation in part (a)? for transmittal, and the other signal requires 3 microsec­ c) How many letters are sent at the nth stage of the chain onds for transmittal. Each signal of a message is followed letter? immediately by the next signal. 2. A nuclear reactor has created 18 grams of a particular ra­ a) Find a recurrence relation for the number of different dioactive isotope. Every hour 1 % of a radioactive isotope signals that can be sent in n microseconds. decays. b) What are the initial conditions of the recurrence rela­ a) Set up a recurrence relation for the amount of this tion in part (a)? isotope leftafter n hours. c) How many different messages can be sent in 12 mi­ b) What are the initial conditions for the recurrence re­ croseconds? lation in part (a)? 6. A small post office has only 4-cent stamps, 6-cent c) Solve this recurrence relation. stamps, and 10-cent stamps. Find a recurrence relation 3. Every hour the U.S. government prints 10,000 more $1 for the number of ways to form postage of n cents with bills, 4000 more $5 bills, 3000 more $10 bills, 2500 more these stamps if the order that the stamps are used mat­ $20 bills, 1000 more $50 bills, and the same number of ters. What are the initial conditions for this recurrence $100 bills as it did the previous hour. In the initial hour relation? 1000 of each bill were produced. 7. How many ways are there to form these postages using a) Set up a recurrence relation for the amount of money the rules described in Exercise 6? produced in the nth hour. a) 12 cents b) 14 cents b) What are the initial conditions for the recurrence re­ c) 18 cents d) 22 cents lation in part (a)? 8. Find the solutions of the simultaneous system of congru­ c) Solve the recurrence relation for the amount of money ences produced in the nth hour. d) Set up a recurrence relation for the total amount of an = an-I + bn-I money produced in the firstn hours. bn = an-I - bn-I e) Solve the recurrence relation for the total amount of money produced in the firstn hours. with ao = 1 and bo = 2. 4. Suppose that every hour there are two new bacteria in a 9. Solve the recurrence relation an = a�_dan-2 if ao = 1 colony for each bacterium that was present the previous and al = 2. [Hint: Take logarithms of both sides to ob­ hour, and that all bacteria 2 hours old die. The colony tain a recurrence relation for the sequence log an, n = starts with 100 new bacteria. 0, 1,2, .... ] 516 7/ Advanced Counting Techniques 7-68

*10. Solvethe recurrencerelationan = a�_la�_2 ifao = 2 and 24. Suppose that 14 students receive an A on the first exam al = 2. (See the hint for Exercise 9.) in a discrete mathematics class, and 18 receive an A on the second exam. If 22 students received an A on either 11. Find the solution of the recurrence relation an = 3an-1 - 3a -2 + an-3 + 1 if ao 2, al 4, and a2 8. the first exam or the second exam, how many students n = = = received an A on both exams? 12. Find the solution ofthe recurrence relation an = 3an-1 - 3an-2 + an-3 if ao 2, al = 2, and a2 = 4. 25. There are 323 farms in Monmouth County that have at = least one of horses, cows, and sheep. If 224 have horses, *13. Suppose that in Example 4 of Section 7. l a pair of rabbits leaves the island after reproducing twice. Find a recur­ 85 have cows, 57 have sheep, and 18 farms have all three rence relation for the number of rabbits on the island in types of animals, how many farms have exactly two of the middle of the nth month. these three types of animals? 14. Find the solution to the recurrence relation f(n) 26. Queries to a database of student records at a college pro­ = duced the fo llowing data: There are 2175 students at the 3f(nI5) + 2n\ when n is divisible by 5, for n = 5k, where k is a positive integer and f(l) 1 . college, 1675 of these are not freshmen, 1074 students = have taken a course in calculus, 444 students have taken 15. Estimate the size of f in Exercise 14 if f is an increasing function. a course in discrete mathematics, 607 students are not freshmen andhave taken calculus, 350 students have taken 16. Find a recurrence relation that describes the number of comparisons used by the following algorithm: Find the calculus and discrete mathematics, 201 students are not largest and second largest elements of a sequence of n freshmen and have taken discrete mathematics, and 143 numbers recursively by splitting the sequence into two students are not freshmen and have taken both calculus subsequences with an equal number of terms, or where and discrete mathematics. Can all the responses to the there is one more term in one subsequence than in the queries be correct? other, at each stage. Stop when subsequences with two 27. Students in the school of mathematics at a university ma­ terms are reached. jor in one or more of the following four areas: applied mathematics (AM), pure mathematics (PM), operations 17. Estimate the number of comparisons used by the algo- rithm described in Exercise 16. research (OR), and computer science (CS). How many students are in this school if(includingjoint majors) there Let {an } be a sequence of real numbers. The fo rward dif­ are 23 students majoring in AM; 17 students majoring in ferences of this sequence are defined recursively as fol­ PM; 44 in OR; 63 in CS; 5 in AM and PM; 8 in AM and lows: The firstfo rward difference is flan an l - an ; the l = + CS; 4 in AM and OR; 6 in PM and CS; 5 in PM and OR; (k + l)st fo rward difference flk+ an is obtained from flkan k+l k k 14 in OR and CS; 2 in PM, OR, and CS; 2 in AM, OR, by fl an = fl an+l - fl an. and CS; 1 in PM, AM, and OR; 1 in PM, AM, and CS; 18. Find flan, where and 1 in all four fields. a) c) 2 + an = 3. b) an = 4n + 7. an = n + n 1. How many terms are needed when the inclusion­ 3 28. 19. Let an = 3n + n + 2. Find flkan, where k equals exclusion principle is used to express the number of ele­ a) 2. b) 3. c) 4. ments in the union of seven sets if no more than fiveof *20. Suppose that an = P(n), where P is a polynomial of these sets have a common element? degree d. Prove that fld+la 0 for all nonnegative in­ n = 29. How many solutions in positive integers are there to the tegers n. equation Xl + X2 + X3 = 20 with 2 < Xl < 6, 6 < Xz < 21. Let {an } and Ibn } be sequences of real numbers. Show 10, and 0 < X3 < 5? that 30. How many positive integers less than 1,000,000 are + a) fl(anbn) = an+l(flbn) bn(flan). divisible by 2, 3, or 5? not divisible by 7, 11, or 13? Show that if F(x) and G(x) are the generating functions b) 22. c) divisible by 3 but not by 7? for the sequences {ad and {bk}, respectively, and c and d are real numbers, then (cF + dG )(x) is the generating 31. How many positive integers less than 200 are functionfor {cak + dbd. a) second or higher powers of integers? 23. (Requires calculus) This exercise shows how generating b) either second or higher powers of integers or functions can be used to solve the recurrence relation primes? + c) (n l)an+1 = an + (lin!) for n ::: 0 with initial condi­ not divisible by the square of an integer greater than tion ao = 1. I? a) Let G(x) be the generating function for {an}. Show d) not divisible by the cube of an integer greater than I? that G'(x) = G(x) + eX and G(O) = 1. - e) not divisible by three or more primes? b) Show frompart (a) that (e x G(x» ' = 1, and conclude that G(x) = xex + eX . *32. How many ways are there to assign six differentjo bs to c) Use part (b) to finda closed form for an . three different employees if the hardest job is assigned 7-69 Computations and Explorations 517

to the most experienced employee and the easiest job is 34. How many bit strings of length six do not contain four assigned to the least experienced employee? consecutive Is? 33. What is the probability that exactly one person is given back the correct hat by a hatcheck person who gives n 35. What is the probability that a bit string oflength six con­ people their hats back at random? tains at least four Is?

Computer Projects

Write programs with these input and output.

1. Given a positive integer n, list all the moves re­ initial conditions ao = Co and al = C l, determine the quired in the Tower of Hanoi puzzle to move n disks unique solution. from one peg to another according to the rules of the 7. Givenarecurrencerelationofthe form /(n) = al(n/b) + puzzle. c, where a is a real number, b is a positive integer, and C k 2. Given a positive integer n and an integer k with I :s k :s n, is a real number, and a positive integer k, findI(b ) using list all the moves used by the Frame-Stewart algorithm iteration. (described in the preamble to Exercise 54 of Section 7. 1) 8. Given the number of elements in the intersection of three to move n disks from one peg to another using four pegs sets, the number of elements in each pairwise intersection according to the rules of the puzzle. of these sets, and the number of elements in each set, find 3. Given a positive integer n, list all the bit sequences of the number of elements in their union. length n that do not have a pair of consecutive Os. 9. Given a positive integer n, produce the formula for the 4. Given a positive integer n, write out all ways to parenthe­ number of elements in the union of n sets. size the product of n + I variables. 10. Given positive integers m and n, findthe number of onto j. Given a recurrence relation an = Clan-l + C2an-2, where functions from a set with m elements to a set with n Cl and C2 are real numbers, initial conditions ao = Co and elements. a 1 = C l , and a positive integer k, finda k using iteration. 11. Given a positive integer n, list all the derangements of the 6. Given a recurrence relationan = Clan-l + C2an-2 and set {I, 2, 3, ..., n}.

Computations and Explorations

Use a computational program or programs you have written to do these exercises.

1. Find the exact value of 11 00, 15 00, and 11000, where In is ing 16, 64, 256, and 1024 using the fast multiplication the nth Fibonacci number. described in Section 7.3 and the standard algorithm for 2. Find the smallest Fibonacci number greater than multiplying integers (Algorithm 3 in Section 3.6). 1,000,000, greater than 1,000,000,000, and greater than 8. Compute the number of operations required to multiply 1,000,000,000,000. two n x n matrices for various integers n including 4, 16, 3. Find as many prime Fibonacci numbers as you can. It is 64, and 128 using the fast matrix multiplication described unknown whether there are infinitelymany of these. in Section 7.3 and the standard algorithm for multiplying 4. Write out all the moves required to solve the Tower of matrices (Algorithm 1 in Section 3.8). Hanoi puzzle with 10 disks. 9. Use the sieve of Eratosthenes to find all the primes not 5. Write out all the moves required to use the Frame-Stewart exceeding 1000. algorithm to move 20 disks from one peg to another peg 10. Find the number of primes not exceeding 10,000 using using four pegs according to the rules ofthe Reve's pUzzle. the method described in Section 7.6 to findthe number of 6. Verifythe Frame conjecture for solving the Reve's puzzle primes not exceeding 100. for n disks for as many integers n as possible by show­ ing that the puzzle cannot be solved using fewer moves 11. List all the derangements of {I, 2, 3,4, 5, 6, 7, 8}. than are made by the Frame-Stewart algorithm with the 12. Compute the probability that a permutation of n objects optimal choice of k. is a derangement for all positive integers not exceeding 7. Compute the number of operations required to multiply 20 and determine how quickly these probabilities ap­ two integers with n bits for various integers n includ- proach the number e.