Matches are a common object to play Nim with, but the pros advise beginners to keep them unlit. |
Simple Positions
If all three piles have at least one chip in them, there are more avenues of attack. Positions in which at least two of the piles are equal, ex. (1,1,1), (1,2,1), (7,3,3), are N-positions, since it is easy to move to a position with two equal piles and an empty third, which are established P-positions. One step more complicated is the (3,2,1) position. This must be a P-position, as every move will result in either a position with three piles with two equal piles or a position of two piles with unequal amounts. Both of these are established N-positions. We could continue on and find more N and P positions, and we would eventually find a pattern. Luckily, someone's done the work for us, but it requires the definition of a nim-sum.
Nim-Sum
Simply put, the nim-sum of two natural numbers is their addition in base 2 without carry. Those who are familiar with binary numbers may already have an idea of how this works. Every number has a unique base 2 representation which is formed by a string of 1's and 0's in digit places that are powers of 2. The first digit has value of 2 power 0 (1), the second has value 2 power 1 (2), the third has value 2 power 2 (4), the forth has value 2 power 3 (8), etc. Whereas in base 10 we know the value of 345 by computing (3*100 + 4*10 + 5*1), in base 2 we can find the value of 101 by computing (1*4 + 0*2 + 1*1) = 5 base 10. Further, 101101 in base 2 computes to (1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 1*1) = 45 base 10. So, to find the nim-sum of two numbers, we just need to add the base 2 versions of them without carrying digits over. For example, nim-sum(22, 51):
$\ \ \ \ \ \ 10110 \\Since addition modulo 2 is associative and commutative, so too is nim-sum associative and commutative. So, it doesn't matter what order we add the numbers up. Important to realize is that every number is its own negative. That is, the nim-sum of x and x is 0. So, the cancellation law holds with nim-sums.
\ \ \ \ 110011 \\
= 100101$
Suppose nim-sum(x, y) = nim-sum(x, z).Now that we understand nim-sums, we can understand Bouton's 1902 Theorem.
Then nim-sum(x, x, y) = nim-sum(x, x, z).
Since numbers are their own negative, nim-sum(0, y) = nim-sum(0, z)
Thus, y = z.
The Theorem of the Game of Nim
The theorem states that a position, (X1, X2, X3), in Nim is a P-position if and only if the nim-sum of its components is zero. That is, nim-sum(X1, X2, X3) = 0. For example, take the position (13, 12, 8). Converting these numbers to base 2 and then adding them yields:
$13 = 1101 \\1001 $\neq$ 0, so the theorem says that this is an N-position. There must be a move to a P-position, such that when the nim-sum is found again, it comes out to zero. There are more than one move that results in a P-position. One of them is to take away 9 from the pile of 13, leaving 4 in that pile.
12 = 1100 \\
\ \ 8 = 1000 \\
\ \ \ \ = 1001$
$\ \ 4 = 0100 \\This yields a nim-sum of 0, and so should be a P-position. Of note is that the theorem works for games of Nim with more than three piles. Indeed, any number of piles more than one pile will work. This is only good if we can prove that Bouton's Theorem is true, so let's do that.
12 = 1100 \\
\ \ 8 = 1000 \\
\ \ \ \ = 0000$
Found exclusively within pudding, delicious proof can only be eaten with the spoon of knowledge. |
The Proof of the Theorem
Let A refer to the set of Nim positions with nim-sum zero, and let B refer to the set of positions of positive, non-zero nim-sums. We check the three standard conditions.
- All terminal positions are in A. Indeed, they are. The only terminal position is the one with no chips, and nim-sum(0, 0, 0, ...) = 0.
- From each position in B, there is a move to a position A. This is always possible following these steps. Construct the nim-sum as a column addition and look at the leftmost column with an odd number of 1's. Change any of the numbers that have a 1 in that column to a number such that there are an even number of 1's in each column. Since the 1 in the most significant digit is changed to a 0, this step makes the number smaller. Thus, this is a legal move to a position in A.
- Every move from a position in A is to a position in B. If (X1, X2, ...) is in A, then nim-sum(X1, X2, ...) = 0. If X1 is changed to X1' < X1, then it is impossible that nim-sum(X1', X2, ...) = 0 because the cancellation law would imply that X1 = X1'. So, nim-sum(X1', X2, ...) $\neq$ 0, and so (X1', X2, ...) is in B.
Final Thoughts
Feel free to use this game to your advantage, perhaps to set up a game of Nim with your friends so that you always end up winning. In case they wise up to your antics, tune in to a future blog post where someone will talk about variants of the game of Nim to keep your friends on their toes.
Pointing out grammatical errors to me is appreciated!
Solid post, Bayard. It was concise and easy to follow, which is always a big plus. I feel I have a firm grasp of nim-sums and the game of nim and this is the first I have heard of both! After doing a little extra reading up on the game of nim, I noticed that players can either play this as a normal game or a misere game. When played as a misere game, the player to remove the last chips obviously loses, so this changes the basic strategy a bit. For example, as Bayard posted a scenario with (1,2,1) chips in each heap, this is an N position. In misere play, this is still an N position, however the optimal play changes from removing all of the middle pile (2), to taking 1 chip from that pile leaving a total of (1,1,1). As we can see, an odd number of heaps each with 1 chip is a P position, rather than an N position in normal play. Below is the wiki link for a little more misere nim strategy, just scroll to the Mathematical Theory section:
ReplyDeletehttps://en.wikipedia.org/wiki/Nim#Mathematical_theory
Intriguing! Perhaps we'll hear more about misere games in a future post/presentation... ;)
DeleteNice work! We played the game of Nim on my first day of Bridge class, sophomore year. The game was definitely fun, but I didn't catch on to the mathematical underpinnings of the game--so thank you for enlightening me. The Nim-sum trick is definitely a game-changer (pun intended). However, I would have to find an infinitely patient opponent, seeing as there is so much scratch work that goes into making an informed play.
ReplyDeleteI fussed with some Nim-sums to find some recognizable positions. It turns out that the position (a, b, c) is always an N-position, when a, b and c are all powers of 2. For example, (1, 2, 4), (2, 2, 4), (4, 4, 4) and (2, 4, 16) are all N-positions. Note: a, b and c need not be unique powers of 2. Similarly, the position (a, a, b) is an N-position when a is a power of 2. For example, (2, 2, 13) and (6, 4, 4) are N positions.
I also found that the position (a, 2a, 3a) is a P-position when a is a power of 2. For example, (1, 2, 3), (2, 4, 6), (4, 8, 12), (8, 16, 24) and (16, 24, 48) are all examples of P-positions. I also found that (10, 20, 30) and (18, 36, 54) are examples of P-positions, even though the "a" values aren't powers of 2.
Nice! Practical applications of this in situations where pulling out a calculator would be awkward are definitely tough to construct, so this is a nice start to figuring out quick computations. Maybe we'll be able to put together some more "quick cheats" for pulling one over on our friends with Nim. :)
DeleteBayard,
ReplyDeleteThis is a fantastic first post. You covered everything from how the game is played, to strategy and theory, to proof, all of which are very easy to read through. The organization and format of the post allows the reader to pin-point the key ideas and take away the main message.
Its really interesting to see how the three piles play off of each other so well, and taking one less or more than intended can change up the entire game. For example in your demonstration of (13,12,8) to (4,12,8) is much different from changing the (13,12,8) to a (12,12,8). Not being able to properly formulate that yield can be dangerous and precision is very important (because I can 100% see myself messing up on that numerous times).
Im curious to see if there is a slight advantage to which player goes first in a game, and if so, why is that? Its also fun to see all the variations that come with a game like Nim and how those variations influence a proof/theory. All-in-all, great job!
Great question, Jon! Let's discuss that today in class and see what we can come up with.
DeleteGood stuff! And great comments, too! Excited to discuss more about this in class.
ReplyDeleteBayard,
ReplyDeleteGreat post! I had never heard of the game of Nim before this class, and I already have a strong understanding of it. I read through the comments and Jon's last comment asking if it matters which player goes first matters intrigued me. So I went to look up more information on the game of Nim. I was surprised to see that it was one of the first computer games to come out. (I had always thought this was pong)
Way back in 1939 the computer beat roughly 90% of the competition. The other 10% were games that attendants played to demonstrate it was possible.
http://www.historyofinformation.com/expanded.php?id=4472
This led me to believe that it didn't matter who went first or second, but I don't think that all 90,000 people who attempted and lost knew how to play the game of him. So I kept looking.
I was very surprised to read that for a game starting with heaps of (3,4,5) the first player will win, regardless of the style of play (misere, or normal). So does mean you would like to go second for a game with different pile sizes, or play style?
https://en.wikipedia.org/wiki/Nim