Select tokens from any one heap to remove
Bouton's Theorem (1901): A player has a forced winning strategy if and only if the XOR sum (Nim-sum) of heap sizes is non-zero. If the Nim-sum is 0, every column in binary has an even number of 1s (balanced).
Misère Rule: The opponent took the final token and lost.
Nim is an ancient mathematical game of strategy with origins dating back thousands of years to ancient China (known as Jianshizi, or "picking stones"). In European history it was played with matchsticks, coins, or counters.
In 1901, Harvard mathematician Charles L. Bouton proved that Nim can be completely solved using binary arithmetic and the bitwise XOR operation (called the Nim-sum, denoted \(\oplus\)).
Convert the number of objects in each pile into binary:
0 1 1
1 0 0
1 0 1
Sum each column in binary without carry (addition modulo 2). If all columns contain an even number of 1s, the Nim-sum is 0 (a balanced or P-position). If any column has an odd number of 1s, the Nim-sum is \(\neq 0\) (an unbalanced or N-position).
0
The Winning Formula: Whenever you are in an unbalanced state (\(\text{Nim-sum} \neq 0\)), there is always at least one legal move that leaves a balanced state (\(\text{Nim-sum} = 0\)) for your opponent. Your opponent is then mathematically guaranteed to return an unbalanced state, allowing you to control the game from start to finish!