The Taxman Game. Robert K. Moniot September 5, 2003

Transcription

1 The Taxman Game Robert K. Moniot September 5, Introduction Want to know how to beat the taxman? Legally, that is? Read on, and we will explore this cute little mathematical game. The taxman game is a golden oldie computer game. It has been used as an exercise in introductory programming classes and in books on recreational computing since time immemorial. (Well, at least since the 1970s. I haven t been able to determine who invented it or when.) Here s how to play: you start with a pot consisting of all the positive integers up to some chosen limit, N. You take one, and the taxman gets all the others that divide it evenly. The number you picked is added to your score, and the numbers the taxman got are added to his score. This process is repeated until there s nothing left in the pot. What makes the game interesting is that the taxman has to get something on every turn, so you can t pick a number that has no divisors left. And when none of the numbers remaining in the pot have any divisors left, the taxman gets them all! The game is pretty easy to program on a computer, and if programming is one of your skills, you might want to pause now to do so and play a few games, just to get the hang of it. Right now we ll demo a couple of example games with N = 10 to show you how it goes. First, a game where we try to win by always taking the largest number in the pot. Hi! I m the taxman! Let s start. How many numbers in the pot? Pick one of the numbers: 10 I take: Pick one of the numbers: 9 I take: Pick one of the numbers: 8 I take: 4 I take: 6 7 All gone. My score = 28, your score = 27 I win. 1

2 As you can see, it s not so easy to beat the taxman. You do have an advantage because each number you take is always bigger than any of the numbers the taxman takes, and so it s not hard to get out in front of the taxman and stay there while play continues. But slowly and silently the unpickable numbers with no divisors left are accumulating, and suddenly the game ends with the taxman s score getting a big boost. If you haven t accumulated enough of a cushion, he ll jump ahead and beat you. In this game, the numbers 6 and 7 ended up with no factors left, so the taxman got them, boosting his score from 15 to 28. Still, if you ponder your moves well, it is possible to beat the taxman. This time we ll choose more carefully. Hi! I m the taxman! Let s start. How many numbers in the pot? Pick one of the numbers: 7 I take: Pick one of the numbers: 9 I take: Pick one of the numbers: 6 I take: Pick one of the numbers: 8 I take: Pick one of the numbers: 10 I take: 5 All gone. My score = 15, your score = 40 You win. In this game, we limited the taxman s take to just one number on each move, and no numbers were left unpickable at the end. It s not always possible to trounce the taxman this badly, but it is generally possible to win. Below, we will explore some strategies for making good picks. We will also see the results of a lengthy computer search for the best possible sequences of picks for pots of up to 49 numbers. 2 Some basics of strategy First of all, it is worthwhile noticing that since each of the N numbers originally in the pot ends up in either the player s score or the taxman s score, the sum of the two scores must equal the sum of the numbers from 1 to N. Therefore the bigger the taxman s score, the smaller your score, and vice-versa. This kind of game is called a zero-sum 2

3 game, because the sum of the two players final scores, minus a constant that is known at the start of play, is equal to zero. The taxman always gets the number 1 and all but at most one of the primes that are initially in the pot. This is because the number 1 can never be picked, and the only turn on which the player can pick a prime number is the first turn, when the number 1 is still in the pot. This gives the taxman a built-in advantage. There is no simple formula for this advantage, but a crude estimate can be calculated by using the approximation for the density of primes near x of 1/ ln x. We will skip the details of the calculation, but the result is that for a pot of size N, the taxman is guaranteed to get roughly 1/ ln N of the pot just from the leftover primes. This fraction slowly decreases as N increases: it is about 30% for N = 30, and about 15% for N = On the other hand, we can ask what is the absolute best that the player can do in any game? Since the taxman must get something on every turn, the best you can hope for is to take just as many numbers as the taxman does. This can only be done for even N. In that case, the best score is gotten if you take all largest numbers and leave the smallest ones for the taxman. (We were in fact able to do this in the second example game played above, with N = 10.) There is a well-known formula giving the sum of the numbers from 1 to N as N(N + 1)/2. So for even N, if the player takes all the numbers from N through N, leaving the taxman the numbers 1 through N 2, a little algebra shows that the taxman s score will be N(N + 2)/8. Since the pot is N(N + 1)/2, the taxman s take as a fraction of the pot is 1 (N+2) 4 (N+1). This fraction is always a bit bigger than 1/4, and gets closer to 1/4 or 25% as N gets larger. This means that the very best the player can ever do is to get a bit less than 75% of the pot. 2.1 Optimal strategy An optimal strategy for this game would be a strategy that always gives the player the largest possible score for a given N (and gives the taxman the least). What can we say about the optimal strategy? The best choice for the first pick is not hard to see: simply take the largest prime number in the pot. We can prove that this is always the best pick as follows. First, this pick limits the taxman s take to 1, which the taxman will take on the first move for any pick. Further, removing this prime, which we will call p max, and 1 from the pot does not prevent any sequence of nonprime picks that was originally possible. That is, if the player could have played by picking the sequence of numbers n 1, n 2,..., n k where k is the total number of picks and n 1 is not prime, then the alternative sequence p max, n 1, n 2,... n k is also possible and gives the player a higher score. Picking p max does not remove any divisors of n 1, n 2,..., n k other than 1, because p max cannot divide any other number in the pot. Its smallest multiple, 2p max, cannot be in the pot, because there is a theorem from number theory that says that there is always a prime between p max and 2p max, which would contradict the choice of p max as the largest prime in the pot. Since none of the picks in the original sequence is prime, they must therefore all have other divisors in the pot that will still allow them to be picked. On the other hand, if n 1 is prime and n 1 < p max, then by a similar argument replacing n 1 by p max does not require any change in the rest of the sequence, and increases the player s score by p max n 1. From this result, it follows that if N itself is prime, then the optimal sequence of picks for a pot of size N is the same as for a pot of size N 1, except that on the first 3

4 turn we should pick N instead of the next-smaller prime. 3 A heuristic strategy Beyond the first pick, there don t seem to be any simple rules for the optimal strategy. In situations like this, we can often be happy to settle for a strategy that may not be optimal but will generally give a high score. Such a strategy, which is not proved to be the best but is sufficiently successful in practice, is called a heuristic. Heuristics can be based on various principles. For the taxman game, a greedy principle yields a very good heuristic that is easy to apply. A greedy principle is one that says to make the move that maximizes the short-term gain. For some problems, a greedy strategy can turn out to be optimal, but in many cases it falls short because of the long-term consequences. (The same is true of life in general, isn t it?) In the first example game we saw a naive greedy heuristic that failed even to win the game: always picking the largest available number in the pot. This is a bad heuristic because it ignores the taxman s take. The game is zero-sum, so the true measure of gain is not your take alone, but the difference between your take (the value of the number you pick) and the taxman s take (the sum of the remaining divisors of the number). Therefore, a better greedy heuristic for the taxman game is on each turn to pick the number that maximizes the difference between your pick and the taxman s take for that pick. This heuristic is easy to calculate (at least, easy for a computer) and generally gives a good score. Trials of the greedy method show that it sometimes overlooks freebies, which are picks that can be taken in between greedy picks without altering the remaining sequence. The freebies are numbers for which the taxman s take is a proper subset of his take for the greedy pick on the same turn. That is, if you picked the freebie, the taxman would take some, but not all, of the divisors of the greedy pick, so the greedy pick could still be taken afterward. Thus the greedy pick would either give the freebie to the taxman or make it unpickable subsequently, whereas picking the freebie before the greedy pick does not prevent the greedy pick. (The freebie also must not be a divisor of any other number in the greedy sequence, lest it make that number unpickable.) For example, when N = 15, after first picking 13, the largest prime, the greedy pick is 15. The remaining divisors of 15 are 3 and 5. This means that we can pick 9, giving the taxman 3, and still pick 15, giving the taxman 5. This makes 9 a freebie. Therefore the greedy method can be improved by scanning for freebies before each greedy pick, and inserting them into the sequence. We call this the improved greedy heuristic, and it is the best strategy that we have found so far. But is it optimal? Alas, only sometimes. The experiments described later show that the improved greedy heuristic often falls short of the optimal score. 4 Search for optimal plays Since there does not seem to be any simple strategy that guarantees an optimal score, we programmed a computer to search for the optimal play for any given N. In essence, the program tries all possible choices for the first pick. Then, based on what is left in the pot, it tries all permissible choices for the second pick, and so on, continuing each sequence of picks till the game is ended. Having played all possible games for that N, it 4

5 prints out a sequence of picks that gives player the best score. (Here, I say a sequence rather than the sequence because there can be different sequences that achieve the same score.) This type of approach is called a brute-force search because it simply tries all possibilities. Usually, when trying to find an optimal solution to a problem, you should apply as much mathematical analysis as possible first, in order to find a strategy that homes in directly on the solution. A brute-force search usually requires a lot of computing power, so that it may be impractical for large problems. Our search was organized as a tree search, in which it is thought of as exploring the branches of a tree. The trunk of the tree is the starting point, when all the numbers from 1 to N are in the pot. Then each of the main branches of the tree represents one of the possible choices for the first pick. Having chosen one of these main branches, further branches represent the available choices at each step. After traversing a number of branches, the search eventually reaches a leaf, where a game ends. The search then backs up to the last branch point and picks another branch to follow. When all the branches from that point have been explored, it backs up further and tries another branch closer to the trunk. This process is repeated until all the branches have been explored and every leaf has been visited. 4.1 Time complexity As N gets bigger, the brute-force search takes an ever-longer time to explore the whole tree of possibilities and find the optimal score. Let s call the number of possible pick sequences S(N). This number grows rapidly as N increases. The rate of growth of S(N) is called the time complexity of the brute-force search method, since it determines how many possibilities the program has to examine in order to arrive at the answer. The term complexity in computer science is used to describe the level of difficulty of solving a given class of problems. A linear rate of growth, where the complexity is proportional to N, means the problem is easy and can be readily solved even for large values of N. Unfortunately, our S(N) grows very rapidly, limiting the maximum value of N for which a solution can be found in a reasonable time. We can easily see that S(N) grows less rapidly than N! because, although we have almost N possible choices for the first pick, after the first pick there are fewer than S(N 1) sequences for the rest of the game. Still, this line of thinking suggests that S(N) probably grows more quickly than an exponential function such as 2 N, since the number of sequences usually more than doubles as N is increased by 1. We used our search program to count S(N) for pot sizes up to N = 35. The result is shown in Figure 1 (filled circles). Notice that the vertical scale of the graph is logarithmic. On this kind of graph, an exponential curve would be a straight line. The upward curvature of the trend in the figure shows that S(N) grows somewhat faster than exponentially. 4.2 Improving the brute-force search The straightforward tree search is too lengthy to solve for the optimal play for pots bigger than about N = 40. So the program was improved to prune the tree. This pruning is done by keeping track of the unpickable numbers, those whose divisors are already taken. These numbers already belong to the taxman, although they are still in the pot. By adding these numbers to the taxman s score, the program can tell when it 5

6 S(N) without pruning with pruning N Figure 1: Number S(N) of pick sequences as a function of initial pot size N, with and without pruning. is impossible for the player to do better than the best score that has already been found, and abandon that branch of the tree. The program uses the score from the improved greedy heuristic as an initial target, and replaces this by any higher score that is found later on. The improvement with pruning is shown by the open circles in Figure 1. The reduction in the number of sequences examined is by a factor of nearly 1000 for N = 35. Still, even with pruning, the number of possibilities that need to be checked follows a trend that increases faster than exponentially with N. To speed the search still further, the program uses the proven optimal strategy for the first pick: instead of trying all numbers from 2 to N, the program starts by picking the largest prime in the pot. This prunes away all the first branches of the tree except one. The program also skips the search entirely whenever N is prime. As shown earlier, the optimal play for prime N is easily found from the optimal play for N 1. 5 Results of the search The search for the optimal sequence of picks was carried out for all pots up to N = 49 before I reached the limit of my computing resources. (Most of the search was done over a period of about 5 years on a Pentium 90 running the Linux operating system, with the final cases being done on a 1.8 GHz Pentium Linux system in a few additional months. For the case N = 49, the search needed to examine more than possible sequences.) The results are given in the Appendix. One interesting question that we can look at using these results is: how often can we achieve the absolute best score, in which we take all the numbers in the upper half of 6

7 the pot? The search shows that this is possible only for the cases N = 2, 4, 6, and 10. For N = 8, and for all N above 10, there are at least two primes in the upper half of the pot, so the taxman always gets at least one of them. For N = 10, the player s score is 40/55 73% of the pot. This is close to the upper limit of 75% derived in section 2. It is probably the best score, as a fraction of the pot, that can be achieved for any pot size. Another question is: how well does the improved greedy heuristic do as compared to the optimal play? The results show that the heuristic fails to yield the optimal play for about half the cases solved. Still, it always does a good job. Since the improved greedy heuristic can be calculated quickly, I explored how well it does out to N = In this range, it always makes the optimal first pick and it always wins (except for the case N = 3 in which the optimal score is a tie). On average, it gives the player 60% of the pot. If you look closely at the experimental results you can see an interesting feature: the optimal second pick is almost always the largest square of a prime. The only cases where this pick is not optimal are N = 8 and N = 20. (Some of the sequences could be rearranged to pick it later than second and still get the same score.) 6 Open questions There are many questions about the taxman game that I haven t been able to answer. Here are a few. Does the greedy heuristic always make the optimal first pick, namely the largest prime? It seems likely that it does, since the largest prime is usually one of the biggest numbers in the pot, and so the difference between it and 1 (the taxman s take for that pick), is going to be hard to beat. I have tested it out to N = 500, 000 without finding any cases where it fails to make the correct first pick. Still, I don t have a downright proof. Is there a simple winning strategy for the game? This would be a strategy that is guaranteed to win every time. The improved greedy heuristic is a good candidate, and tests show that it wins all games up to N = 1000, but I have not proved that it always wins. Is the choice of the largest squared prime as the second pick optimal for all N above 20? If not, how can we identify the values of N for which it is not? We saw how the optimal play for prime N can be based on the optimal play for N 1. Are there any other cases for which the optimal play for one pot size can be used to find the optimal play for another? The experiments show that very often the optimal sequences for successive N are quite similar, but there isn t an obvious systematic way to relate them. If you can answer any of these questions, be sure to let me know. Perhaps you can also think of some interesting questions of your own! In any case, I hope you have had fun exploring with me how to beat the taxman! 7

8 7 Appendix Listing of pick sequences found by brute-force search program. The table lists the improved greedy sequence first, with the freebies in boldface, followed by the optimal sequence, if different, on the next line. N Score Sequence

Induction Margaret M. Fleck 10 October 011 These notes cover mathematical induction and recursive definition 1 Introduction to induction At the start of the term, we saw the following formula for computing

SEC. 4.1 TAYLOR SERIES AND CALCULATION OF FUNCTIONS 187 Taylor Series 4.1 Taylor Series and Calculation of Functions Limit processes are the basis of calculus. For example, the derivative f f (x + h) f

Near Optimal Solutions Many important optimization problems are lacking efficient solutions. NP-Complete problems unlikely to have polynomial time solutions. Good heuristics important for such problems.

CHAPTER 2 Mathematics of Cryptography Part I: Modular Arithmetic, Congruence, and Matrices Objectives This chapter is intended to prepare the reader for the next few chapters in cryptography. The chapter

The General Approach Notes on Factoring MA 26 Kurt Bryan Suppose I hand you n, a 2 digit integer and tell you that n is composite, with smallest prime factor around 5 digits. Finding a nontrivial factor

Induction Problems Tom Davis tomrdavis@earthlin.net http://www.geometer.org/mathcircles November 7, 2005 All of the following problems should be proved by mathematical induction. The problems are not necessarily

Problem 3 If A is divided by B the result is 2/3. If B is divided by C the result is 4/7. What is the result if A is divided by C? Suggested Questions to ask students about Problem 3 The key to this question

Chapter 3 Sequences In this chapter, we discuss sequences. We say what it means for a sequence to converge, and define the limit of a convergent sequence. We begin with some preliminary results about the

Chapter 11 Number Theory Number theory is one of the oldest branches of mathematics. For many years people who studied number theory delighted in its pure nature because there were few practical applications

An Innocent Investigation D. Joyce, Clark University January 2006 The beginning. Have you ever wondered why every number is either even or odd? I don t mean to ask if you ever wondered whether every number

Politics and the political process affect everyone in some way. In local, state or national elections, registered voters make decisions about who will represent them and make choices about various ballot

COLORED GRAPHS AND THEIR PROPERTIES BEN STEVENS 1. Introduction This paper is concerned with the upper bound on the chromatic number for graphs of maximum vertex degree under three different sets of coloring

6.825 Techniques in Artificial Intelligence Decision Making under Uncertainty How to make one decision in the face of uncertainty Lecture 19 1 In the next two lectures, we ll look at the question of how

Chapter Pythagorean Triples The Pythagorean Theorem, that beloved formula of all high school geometry students, says that the sum of the squares of the sides of a right triangle equals the square of the

Key Terms in This Session Session 6 Number Theory Previously Introduced counting numbers factor factor tree prime number New in This Session composite number greatest common factor least common multiple

Lecture 3 Mathematical Induction Induction is a fundamental reasoning process in which general conclusion is based on particular cases It contrasts with deduction, the reasoning process in which conclusion

Fractions and Decimals Tom Davis tomrdavis@earthlink.net http://www.geometer.org/mathcircles December 1, 2005 1 Introduction If you divide 1 by 81, you will find that 1/81 =.012345679012345679... The first

ORDERS OF ELEMENTS IN A GROUP KEITH CONRAD 1. Introduction Let G be a group and g G. We say g has finite order if g n = e for some positive integer n. For example, 1 and i have finite order in C, since

8 Primes and Modular Arithmetic 8.1 Primes and Factors Over two millennia ago already, people all over the world were considering the properties of numbers. One of the simplest concepts is prime numbers.

Math Games with Cards and Dice presented at NAGC November, 2013 Ready, Set, Go! Math Games for Serious Minds Rande McCreight Lincoln Public Schools Lincoln, Nebraska Math Games with Cards Close to 20 -

MATH 13150: Freshman Seminar Unit 8 1. Prime numbers 1.1. Primes. A number bigger than 1 is called prime if its only divisors are 1 and itself. For example, 3 is prime because the only numbers dividing

Taylor Polynomials and Taylor Series Math 26 In many problems in science and engineering we have a function f(x) which is too complicated to answer the questions we d like to ask. In this chapter, we will

Course notes on Number Theory In Number Theory, we make the decision to work entirely with whole numbers. There are many reasons for this besides just mathematical interest, not the least of which is that

1 Tests for Divisibility, Theorems for Divisibility, the Prime Factor Test Definition: Prime numbers are numbers with only two factors, one and itself. For example: 2, 3, and 5. Definition: Composite numbers

Chapter 4 Trees 4.1 Basics A tree is a connected graph with no cycles. A forest is a collection of trees. A vertex of degree one, particularly in a tree, is called a leaf. Trees arise in a variety of applications.

CHAPTER 3 Numbers and Numeral Systems Numbers play an important role in almost all areas of mathematics, not least in calculus. Virtually all calculus books contain a thorough description of the natural,

. Knowledge of another language fosters greater awareness of cultural diversity among the peoples of the world. Individuals who have foreign language skills can appreciate more readily other peoples values

MODULAR ARITHMETIC 1 Working With Integers The usual arithmetic operations of addition, subtraction and multiplication can be performed on integers, and the result is always another integer Division, on

Key Terms in This Session Session 7 Fractions and Decimals Previously Introduced prime number rational numbers New in This Session period repeating decimal terminating decimal Introduction In this session,

10.3 Geometric and Harmonic Series Contemporary Calculus 1 10.3 GEOMETRIC AND HARMONIC SERIES This section uses ideas from Section 10.2 about series and their convergence to investigate some special types

Linear Programming Linear programming refers to problems stated as maximization or minimization of a linear function subject to constraints that are linear equalities and inequalities. Although the study

.. The game starts with a stack of n coins. In each move, you divide one stack into two nonempty stacks. + + + + + + + + + + If the new stacks have height a and b, then you score ab points for the move.

When Does a Number Equal the Sum of the Squares or Cubes of its Digits? An Exposition and a Call for a More elegant Proof 1 Introduction We will look at theorems of the following form: by William Gasarch

Today s Topics Primes & Greatest Common Divisors Prime representations Important theorems about primality Greatest Common Divisors Least Common Multiples Euclid s algorithm Once and for all, what are prime

Sum of Cubes Jo was supposed to be studying for her grade 12 physics test, but her soul was wandering. Show me something fun, she said. Well I wasn t sure just what she had in mind, but it happened that

Review of Fundamental Mathematics As explained in the Preface and in Chapter 1 of your textbook, managerial economics applies microeconomic theory to business decision making. The decision-making tools

PROBLEM STATEMENT In a triangle with a right angle, there are legs and the hypotenuse of a triangle. The hypotenuse of a triangle is the side of a right triangle that is opposite the 90 angle. The legs

Math 120 Final Exam Practice Problems, Form: A Name: While every attempt was made to be complete in the types of problems given below, we make no guarantees about the completeness of the problems. Specifically,

1 Error in Euler s Method Experience with Euler s 1 method raises some interesting questions about numerical approximations for the solutions of differential equations. 1. What determines the amount of

The verbal answers to all of the following questions should be memorized before completion of pre-algebra. Answers that are not memorized will hinder your ability to succeed in algebra 1. Number Basics

Chapter 1 DEGREE OF A CURVE Road Map The idea of degree is a fundamental concept, which will take us several chapters to explore in depth. We begin by explaining what an algebraic curve is, and offer two

CS188 Spring 2011 Section 3: Game Trees 1 Warm-Up: Column-Row You have a 3x3 matrix of values like the one below. In a somewhat boring game, player A first selects a row, and then player B selects a column.

Math 132 Population Growth: the World S. R. Lubkin Application If you think growth in Raleigh is a problem, think a little bigger. The population of the world has been growing spectacularly fast in the

ECON 459 Game Theory Lecture Notes Auctions Luca Anderlini Spring 2015 These notes have been used before. If you can still spot any errors or have any suggestions for improvement, please let me know. 1

CLASS 3, GIVEN ON 9/27/2010, FOR MATH 25, FALL 2010 1. Greatest common divisor Suppose a, b are two integers. If another integer d satisfies d a, d b, we call d a common divisor of a, b. Notice that as

Project and Production Management Prof. Arun Kanda Department of Mechanical Engineering Indian Institute of Technology, Delhi Lecture - 15 Limited Resource Allocation Today we are going to be talking about

3.3 Real Zeros of Polynomials 69 3.3 Real Zeros of Polynomials In Section 3., we found that we can use synthetic division to determine if a given real number is a zero of a polynomial function. This section

Lecture 1: Elementary Number Theory The integers are the simplest and most fundamental objects in discrete mathematics. All calculations by computers are based on the arithmetical operations with integers

CSE373: Data Structures and Algorithms Lecture 2: Proof by Induction Linda Shapiro Winter 2015 Background on Induction Type of mathematical proof Typically used to establish a given statement for all natural

Chapter 3 Solving for Voltage and Current Nodal Analysis If you know Ohm s Law, you can solve for all the voltages and currents in simple resistor circuits, like the one shown below. In this chapter, we

Real Numbers and Monotone Sequences. Introduction. Real numbers. Mathematical analysis depends on the properties of the set R of real numbers, so we should begin by saying something about it. There are

Basic Notions on Graphs Planar Graphs and Vertex Colourings Presented by Joe Ryan School of Electrical Engineering and Computer Science University of Newcastle, Australia Planar graphs Graphs may be drawn