Onto The Nimbers

Alright, in principle there is where I should post the solution to the puzzle from last time, but first I want to talk about some game theory first, just to give a reason why I found this puzzle sort of neat in spite of the fact that it is quite simple.

Let us consider a two player game, played with piles of coins. On your turn you choose a pile of coins and remove some nonzero number of coins from that pile. The players take turns doing this, and eventually piles run out of coins. If it is your turn and there are no coins left, you lose.

If you know some game theory, you will recognize this game as Nim and you might as well stop reading this post right now, since I'm not going to say anything you don't already know. Anyway, if you haven't seen this game, I encourage you to try it out, its a fun and simple game until you have seen the complete solution, you can probably find some programs online that will play with you (and beat you, because the solution is simple to program).

Before I go through the solution, I want to set up some notation. *x will denote a pile of x coins, and *x+*y will denote a game with two piles of coins with sizes x and y. {a,b,c} will denote a game where the legal move is for a player to move to any of positions a, b, or c, so we can say that *4={*0, *1, *2, *3}, and *2={*0, *1}, of course *0={ }. Lets begin by trying to solve a few simple cases of the game.

The one heap game is trivial, you simply remove all the coins in the heap with your first move and the game is over. The two heap game is fairly simple, suppose that we have a position like *7+*4, then we simply reduce the *7 by three coins to *4. With two heaps of the same number of coins, we simply have to mirror everything our opponent does to win, if he reduces one of the 4 heaps to x, we reduce the other 4 heap to x, eventually our opponent will remove a piles final coin, and we sweep the other pile away to win.

The larger games are harder, but we can solve a few cases right away. For example, if it is our turn from the position *8+*6+*6, we should simply remove the heap of size 8, and mirror our opponent in the two heap game. Or is we are in a position *9+*8+*6+*6, we can simply reduce 9 to 8 and play two mirror games. Actually, we can see that if ever we are in some position with *a+*b+*c+*x+*x, we can ignore the last two heaps, our opponent can find no winning move in those two heaps (we just respond in the other one) and we can find no winning move in those two heaps (our opponent will do the same to us). So, from an optimal strategy point of view, adding *x+*x to any game does not change anything. We can also see that adding *0 to any game does not change anything, so we can see that there is some sense in which
*x+*x=*0

Naturally, in the game with only *0, the active player loses, and in the game with only *x+*x, the active player loses.

Now we are in a position to analyze a slightly more complicated position, *2+*1. What are the legal moves from this position? We can reach any of *2+*0, *1+*1, or *0+*1, which are *2, *0, and *1, respectively, so we can see that *2+*1={*2, *0, *1}, which is *3
*2+*1=*3

Which means that in any game with *a+*b+*c+*2+*1, you can treat this as the same as *a+*b+*c+*3. In particular *3+*2+*1 is a losing position for the first player, as it is the same as *3+*3. It also means that a game like *a+*b+*c+*3+*2+*1 is just the same as the game *a+*b+*c+*3+*3, which we have seen is the same as the game *a+*b+*c.
*3+*2+*1=*0

Actually, one might try to derive this last equation from *2+*1=*3 by adding *3 to both sides and using *3+*3=*0. Sounds reasonable enough, and if it weren't valid then our choosing to use "+" to denote these games would be questionable indeed. Actually, this means that we should also be able to add *1 to *2+*1=*3 to arrive at *2=*3+*1, is this equation valid? Well, from the position *3+*1 we can reach any of *3+*0, *2+*1, *1+*1, *0+*1, so we can see that *3+*1={*3, *3, *0, *1}={*3, *0, *1}. But we know that *2={*0, *1}, so are these things really the same?

Suppose we have a winning strategy in the game G+*2 (where G is any collection of heaps, so G represents something like *a+*b+*c), I claim we can use that as a winning strategy in G+*3+*1. If your winning strategy ever tells you to move in G, then do so. If your winning strategy is to move in *2, it must be to *1 or *0, both of which are accessible in *3+*1, so you can follow your strategy. Our only concern is that our opponent makes use of the move from *3+*1 to *3, but we can immediately revert that to *2 (since *2 is a legal move from *3), and then we can actually continue to follow our strategy.

The conclusion of this is that if you have analyzed a game and found is looks like {*0, *1, *2, *5, *9} or so, you can treat this game as *3. It can access any game that *3 can, and if you opponent moves to one of the larger numbers you can immediately revert it to *3. 3 is said to be the Minimal EXcluded number from the set {0,1,2,5,9}, denoted 3=mex{0,1,2,5,9}.
{*a, *b, *c, ...} = *n, where
n=mex{a,b,c,...}

This gives us {*0, *1, *3}=*2 which validates the equation *3+*1=*2. We can confirm that *3+*2=*1 through a similar method.

This actually gives us a complete solution for the N-pile game, as long as the piles are not larger than size 3, any two piles *x and *y can be replaced by one pile, using one of *x+*x=*0, *1+*2=*3, *1+*3=*2, *2+*3=*1. In the end, there is one pile, and you simply must reduce it to zero, if the one pile is already *0, you are in a losing position and must hope for a mistake.

Next, what do we do if we are in a position with a pile of size 4? Well, lets see what *1+*4 gives us. We can move to any of *0+*4, *1+*3, *1+*2, *1+*1, *1+*0, which means *4+*1={*4, *2, *3, *0, *1}, which is clearly *5. We can find *1+*5 as well, we can move to any of*0+*5, *1+*4, *1+*3, *1+*2, *1+*1, *1+*0, which means *1+*5={*5, *5, *2, *3, *0, *1} which by the mex rule is the same as *4. The mex rule gives us our general answer, actually:
*a+*b=*c, where
*c is the smallest number not of the form *a'+*b or *a+*b'
for any a' less than a and b' less than b

This is enough for us to fill in an entire addition table for nim if we want (these numbers are often called nimbers, just to be whimsical). A partial nimber addition table is
0 1 2 3 4 5 6 7
1 0 3 2 5 4 7 6
2 3 0 1 6 7 4 5
3 2 1 0 7 6 5 4
4 5 6 7 0 1 2 3
5 4 7 6 1 0 3 2
6 7 4 5 2 3 0 1
7 6 5 4 3 2 1 0

If you extend this, you'll see it has some nice structure in terms of blocks of powers of two. Actually, you can find a general rule, which I will not prove here:
*2a+*2b=*(2a+2b)
unless a=b

So, nimber addition for different powers of two is the same as number addition. Of course if a=b then the nim sum is zero. One can also see from the nim addition table that nim addition is associative and commutative, meaning that the nimbers form an abelian group under nim addition with identity *0 and every element is its own inverse. We can use this to do some more complicated nim addition, for example *22+*7+*25 = (*16+*8)+(*4+*2+*1)+(*16+*8+*1) = *16+*16+*8+*8+*4+*2+*1+*1 = *0+*0+*6+*0 = *6. One can extend the nimbers to allow for a multiplication and make it a full blown field (sometimes called On2), but lets not bother with that.

One might find this addition easier to do in binary, in fact, some people describe nim addition as "convert to binary and add without carrying". This means that Patrick's addition rule from last time is actually just nim addition.

At this point is it fairly natural to solve the puzzle from last time. We can see that *a+*b+*c+...=*x+*y+*z+... if and only if *a+*b+*c+...+*x+*y+*z+...=*0, so we have an immediate check to see if Sean has any split that does not make his brother cry. If he has such a split, any split will work, so he simply gives his brother the smallest candy to keep the most for himself.

Alright, so any time you are in a situation in nim, simply check the nim sum, if it is not zero then there is some move that will make it zero. If it is already zero then you have lost and must either resign or hope for a mistake.

Nim is actually very important in game theory because any two player, perfect information, finite, impartial game with the normal ending condition can be reduced to nim. To clarify those technical terms, finite means that there is no infinite sequence of legal moves anywhere in the game, the normal ending condition is that if you have no legal moves you lose. Finally impartial means that in any position of the game, both players have the same set of legal moves available to them (contrast to something like chess where one player plays white and the other black, chess is not impartial).

As an example of another impartial game, consider the game of Cram. This game is played on a chessboard and on a players turn they place a 2x1 domino on the board to cover two squares. If it is your turn and you have no room to place a domino, you lose. One can reduce this game to nim by considering what moves are available from any given position. For example, if there is only a 1x1 square, this is the same as *0 (no legal moves). A 2x1 or 3x1 box has exactly one move, to *0, so those positions are *1. A 4x1 box or an L-shape can move either to *0 or to *1, so it is *2. You can just sort of explore the entire game like this if you feel like it.

Anyway, I think thats all I got for this. I really find the entire theory fascinating for some bizzare reason, and that puzzle reminded me that I wanted to make a post working out the solution. If you find this sort of game theory interesting, I recommend you look up the series of books by Gardener, Winning Ways For Your Mathematical Plays.

Splitting Candy

Alright, new puzzle time. This is another one that Bart sent me from CodeJam:
Two brothers, Sean and Patrick, are splitting a bag of candy. The candies come in different sizes, each represented by a positive integer value. Sean, as the older brother, will split the candy into two nonempty piles, and give one to Patrick, keeping the other for himself. Patrick will then examine the two piles and decide if he thinks the split is fair. If he decides the split isn't fair, he will start crying.
Unfortunately, Patrick is very young and bad at adding. He can almost add numbers in binary, but he always forgets to carry the 1. So if he wanted to add 13 (=1101) and 6 (=0110), he will get 1011, which is 11. Some other examples are 3+5=6, 4+10=14, 7+4=3.
Sean is good at adding, and wants to take as much candy as possible while not causing his brother to cry. Given a list of numbers the represent the candy values, is it possible to easily determine if Sean can split the candies in such a way that will not make his brother cry? What is the maximum value of candy that Sean's pile can be?

As an example, if the candies are value 3,4,7, we know that Sean can split them (7, 4) vs (3) and keep 11 total for himself, Patrick will perceive this as fair because he thinks 7+4=3. As another example, if the candies are valued 2,3,4, then there is no splitting that Sean can do that will keep Patrick happy.
The puzzle is sort of neat, and gets into some math that I really like that has a lot of importance in combinatorial game theory. Basically I'm just posting the puzzle so that I can make a game theory post while making it look like a puzzle post.

Flipping Vases

Alright, back to blogging before I stop blogging for another month or something. Time for the solution to the vases in a line puzzle from last time.

First of all, suppose we have a solution for N vases, then by removing vases M+1 through N, we will have a solution for M vases for any M less than N. Thus, we only need to find solutions in a few particular cases of N, as long as those cases get arbitrarily large it will be good enough (for example, we are going to wind up having a solution that works for N being a power of 2, since any number can be expressed a less than some power of two, you can simply grab that larger solution and remove the extra vases).

For future considerations, it will be slightly easier to subtract 1 from the heights of all the vases, so they are numbered 0 through N-1, rather than 1 through N.

We must arrange things so that no number is between two numbers it is the average of, so x cannot appear between x+a and x-a for any a. Note that the average of two even numbers is even and the average of two odd numbers is odd, and the average of an even number and an odd number is not an integer. This suggests that we place all the even numbers on one side, and all the odd numbers on the other. That way there is no concern of the "left side" and the "right side" having anything dangerous in between them, we can simply treat them independently.

Within the even ones, we can simply half them and solve the N/2 case. With the odd ones, we can subtract 1 from them and then half them and use the N/2 solution. Thus we can see that if we can solve N/2, we can solve N. This is why we will get solutions for powers of two, we simply must start by solving the case N=2 (this is trivial, the solution is (0 1)).

So, if we wanted to solve N=4, we have 0 1 2 3 to place in order, begin with the evens on the left and odds on the right and we get (0 2 1 3) as our solution. For N=8 we being by sorting them to (0 2 4 6 1 3 5 7) next treat 0 2 4 6 as 0 1 2 3 to get (0 4 2 6) and treat 1 3 5 7 as 0 1 2 3 to get (1 5 3 7), so the solution is (0 4 2 6 1 5 3 7).

There is another way to generate this solution, begin by writing out the numbers 0 through N-1 in binary
000
001
010
011
100
101
110
111
Next, reverse the digits
000
100
010
110
001
101
011
111
Next, read them off as they are to get (0 4 2 6 1 5 3 7), which is the same solution.

That first algorithm to get the solution is clean enough, and I was pretty happy to consider it the solution, but the second solution involving binary numbers in reverse is really cool (and I did not come up with it, I saw it on xkcd). Its a bit harder to prove that it always works though, but its not so counterintuitive that its impossible to believe. Naturally, if you want the solution for N that is not a power of two, just solve a larger N that is a power of two and ignore the extra numbers, so for N=5 we have (0 4 2 1 3), which after adding 1 becomes (1 5 3 2 4) which works as a solution.

Vases In A Line

Blogging is for chumps, I'm on extended vacation (also known as semi-unemployed). Anyway, time for a puzzle that I found on the forums at xkcd:
There is a museum with N vases they wish to arrange on a display. The vases are such that the first one has a height of 1cm, the second one has height of 2cm, and so on such that the Nth one has a height of N centimeters. The people at the museum wish to arrange the vases in a linear display in a special way, such that if we have three vases with heights a,b,c, with b being the average of a and c, b does not appear between a and c.

The puzzle is to come up with an algorithm that will generate an arrangement of numbers 1 through N such that no number appears between two numbers it is the average of.
So, for example if N=5, we could not have 3 appear anywhere between 2 and 4, or anywhere between 1 and 5, 2 could not appear between 1 and 3, and 4 could not appear between 3 and 5. An acceptable answer there would be (1 5 3 2 4).

Arrangements and Derangements

So, it seems that finishing my thesis has slowed down my blogging. Blogging was done as a way to slack from work, and now I don't really do much work anymore, leaving me with nothing to slack from. Basically, I need to start looking for a job so I can slack off with maximum efficiency.

Alright, time to look at the solution to the sorting puzzle from last time. I guess the solution was given in the comments, which means that I actually have something of a readership I guess (which is a good thing?), anyway, I have no problems with people posting solutions in the comments, but I'm going to do my own solution as usual.

First, lets try to solve it in some simple cases. We already know that the sequence (2 1) takes two moves, how about the sequence (3 1 2). There seems to be no reason to hold any of them in place, and a randomization has an equal chance of going to any of (1 2 3) (1 3 2) (2 1 3) (2 3 1) (3 1 2) (3 2 1). The number of steps those are away from the sorted form is 0, 2, 2, k, k, 2, respectively, (knowing that, say, (1 3 2) is two steps away on average) where k is the number of steps (3 1 2) is away. This allows us to say that
k=1/6(0+1)+3/6(2+1)+2/6(k+1)

which can be solved to get k=3. So this solves the N=3 case completely.

For N=4 we have a bit more of a complicated situation, it is clear that anything such as (_ _ _ 4) reduces to the N=3 case, but we have two separate things to solve, one is things of the form (2 3 4 1) where we have everything out of place, and the other is of the form (2 1 4 3) where we have two separate groups. We already know that it is possible to solve (2 1 4 3) in two steps by doing the first two separate from the second two, but it might be better somehow to just ignore that they are two separate cycles and randomize the whole thing. If it is better, then (2 3 4 1) will be less than 4 steps away on average. If it is more than 4 on average, then we should treat cycles separately, and if it is exactly 4 then it does not matter if we isolate cycles.

First, we will treat cycles separately, and let g(n) be the number of moves it takes on average to sort an n-cycle with each element out of place to the right by one (or left if you like). We have seen g(2)=2, g(3)=3 and trivially g(0)=0, g(1) isn't really a thing since you can't have a cycle of 1 with each one being out of place. Anyway, from a 4-cycle we have 24 places we can go, 1 of them is (1 2 3 4), the solution, 4C2 of them are 2-cycles (like (2 1 3 4)), 4C2/2 of then are two 2-cycles (like (2 1 4 3)), 8 of them are 3-cycles (you can see this by locking one of them down and there are two ways the remaining three are out of place), and the remaining 6 of them are 4-cycles. We can then see that
g(4)=1/24(0+1)+6/24(g(2)+1)+3/24(g(2)+g(2)+1)+8/24(g(3)+1)+6/24(g(4)+1)

which we can solve to get g(4)=4. If we chose the other strategy of just treating two 2-cycles as a 4-cycle, we would replace the term g(2)+g(2) with g(4) instead, and still get the solution g(4)=4, showing that the strategies are the same.

Given that, we might as well only lock down ones that are in the correct place, and not worry about the smaller cycles. Let f(n) be the number of steps it takes to get from a completely unsorted list of n elements to a sorted one ("completely unsorted" means none are in the correct place). Mathematicans have a name for a completely unsorted list, they call it a derangement, and the standard notation for the number of derangements of n elements is !n. A few early ones are !0=1, !1=0, !2=1, !3=2, !4=9. Our earlier strategy for finding f(4) can be expressed as
f(4)=!0/4!(f(0)+1)+!1/4!(f(1)+1)+!2/4!(f(2)+1)+!3/4!(f(3)+1)+!4/4!(f(4)+1)

that is, f(4) is the chance you get k elements in the wrong place, times f(k)+1, summed over k. The chance that exactly k elements are in the wrong place is !k/4!. f(1) isn't really defined, but !1 is zero anyway, I just included that term for completeness.

From this we can deduce the general form
f(N)=Σ !k/N!(f(k)+1)

summed k from 0 to N. If we hypothesize that f(k)=k for all k less than N, we can then proceed to prove f(N)=N, though it is rather tedious to do it this way.

The more intuitive way to get f(N)=N was given in the comments, throwing N elements into the air, each one has a 1/N chance of landing in the right spot, and you threw N of them, so on average one of them will land in the right spot. Thus it takes N steps.

Sorting Numbers

Alright, new puzzle time, Bart showed me this one from the Code Jam this year:
You have a list of the integers 1 through N in some random order, and you are going to attempt to sort them. The only operation you have is to randomize any subset of these integers. Specifically, you may select any subset of the integers and lock them in place, and then the other ones get rearranged randomly. You may perform this operation as many times as you like, but want to do it as few times as possible.

Given the initial list of integers, what is the optimal average number of steps it will take to get the list in order?

The original puzzle had a bit more coding bubble-wrap around it, but not too much. It also had a nicer explanation of why the algorithm is so weird, but whatever.

As a few specific examples, if the initial list is (2 1 3), you can hold the 3 in place and randomize the other two until they fall in place, this will take on average 2 steps. If the initial list is (2 1 4 3) you can hold the first two in place until the second two land in order and then hold the second two in place until the first two land in order. This will take 4 steps on average. Given the initial list, is there an easy way to determine how many steps it will take on average? (Hint: there is, or I wouldn't consider this puzzle interesting).

Cards and Coins

Alright, time for the solution to the score cards puzzle I put up last time. The comments contained the essentials of the solution, but I still want to type anyway.

First, suppose we had a target number N and a solution to use bases a,b,c,d... That is to say, the product abcd... is at least N with the sum a+b+c+d+... minimized. Suppose the proper solution had a 7 as one of the bases, it is just as good to replace the 7 with a 5 and 2, it makes the sum no larger and in fact makes the product bigger, we can similarly replace 6 with 4 and 2 and even 4 with 2 and 2. In all cases where a is larger than 4 it is better to replace a with a-2 and 2 (this is sort of obvious, but if you wanted to prove it note that 2(a-2) > a can be rearranged into a > 4). Anyway, so a proper solution has no need to contain numbers greater than 3, it should only have 2's and 3's.

Next, note that if a proper solution ever had three 2's, we might as well replace them with two 3's, since the product is larger and the sum the same. So any proper solution need not contain more than two 2's. Thus, we find the algorithm to find the optimal base for a target N. Select k such that 3k is at least as large as N and 3k-1 is smaller than N. Compare 3k-12 and 3k-222 to N. Whichever of those things is smallest while being larger than N is the expression of the solution.

It is somewhat surprising that base 3 is the optimal solution, most people (myself included) would intuitively think that base 2 is better, but that is just not the case. In some cases base 2 does work fine, our solution does give an optimal solution, but it is not the only one, for example if the target number is 60 then this solution gives us to use 34, however using 26 works just as well. As the numbers get sufficiently large however, base 3 will always win out.

One can "generalize" this problem somewhat by assuming the numbers do not have to be integers. So, we have to select numbers a,b,c,... to minimize their sum and have their product be N. There is nothing to be gained by overshooting N if we are using reals rather than integers, so we might as well hit it exact. Whats more, if two numbers a and b appear in the solution we would do just as well to replace them with a/x and bx with a smaller sum. How do we minimize the sum of a and b while keeping the product equal? This is just minimizing the perimeter of a rectangle, make it a square, set a=b. So we actually should only use a single number a. That is, we want p copies of a such that the sum a+a+a+... is minimized while the product equals N. That is, minimize pa with N=ap. Minimizing a(Ln(N)/Ln(a)) over a gives us a=e. Giving us that the optimal base is base e. This is sort of why 3 was best, it is the closest integer to e, with 2 also sort of being part of it (I'm not sure how to interpret that, but it feels right).

This also can be taken as a commentary on currency, the optimal base for a currency is base 3 (base e would be better, but then your coins are really weird and making change is hard). Base 3 currency is optimal in the sense of needing the fewest total number of coins to represent a variety of different values. Of course, I doubt we will see a switch to base 3 coinage anytime soon, a single coin of value 3 could probably happen (and probably has, but I know of no real world examples), but coins of value 27 and 81 would be pretty hard to get used to for people who are used to thinking in base 10.