The game of sticks/Nim

The game of sticks (or Nim game) is a game played by two players according to the following rules:

• There are 16 sticks arranged in four rows — containing, respectively, 1, 3, 5, and 7 sticks.

• The participants take turns playing. A move consists of removing any positive number of sticks, as long as they are all from the same row.

• The player who takes the last stick from the table loses the game!

Which of the players can ensure victory?

… And if the winning criterion were “Whoever takes the last stick from the table WINS the game”?

Let’s study the game in a simple generic version.

In this version, we have an arbitrary number P of sticks organized in a single row. Each player, alternately, removes at most n sticks on their turn (naturally, n must be sufficiently smaller than P to allow for several rounds).

Questions to be answered:

• What is the winning strategy based on the parameters P and n?

• Under what conditions will the winner be the first (second) to play?

Below is a simulation with P=21 e n=3.

What’s the strategy in this example? Answer: In this instance, if both players know the solution, the player 2 will always win. The winning strategy is to keep the ammount of sticks in the form 4k+1; generally speaking, a multiple of (n+1)k+1. In this instance, P=21=4\times 5+1 and, since the maximum ammount of sticks to remove is 3, the first player (player 1) can’t leave its opponent with 17 sticks (the next number whose remainder in the division by 4 is 1). The strategy will be – everytime player 1 takes away p sticks, player 2 should take away 4-p sticks. Fatally, player 1 will be left with the last stick. It’s worth noticing that, if the remainder P\div (n+1) is different to 1, the player that started the game will win (considering that they know the winning strategy).

To learn a bit more about the mathematics behind finite games – Winning Strategies

Skip to content