Back

Stack of Coins 2

Medium
Created: February 6, 2026

You and your friend have decided to make treasure hunting your weekend project. After a few weeks of treasure hunting, you guys stumble on another pirate’s treasure: an old chest filled with coins: 99 copper coins and 1 gold coin. As before, these old coins are no longer legal tender, so the copper ones are worthless. Only the gold coin is worth something due to its material.

Not wanting to lose again, your friend comes up with another game of wits. He stacks the 100 coins in a tall stack with the gold coin at the bottom. You and him will take turns taking either 1, 4, or 5 coins from the top of the stack, all the way until the final coin is taken. If there’s only 2 or 3 coins left, the only option for the player is to take 1 coin, since there aren’t enough coins for the player to take 4 or 5. He lets you decide if you want to go first or second.

Should you go first or second? And what strategy do you use to ensure you’ll win the gold coin?

Solution

Choose to go first and take 4 coins. After that, follow the strategy below.

Let’s start from 0 coins and work backwards. If a player is presented with a stack of 0 coins, it means their opponent took the gold coin, resulting in a loss for the current player. If there are 1, 4, or 5 coins left, the player that moves next can take all the coins and win. A stack of 2 coins left is a losing position as the only option is for the player to take 1 coin, allowing their opponent to take the last coin and win.

Coins in stackPosition for the player that moves next
0Lose
1Win
2Lose
3?
4Win
5Win

We notice that when there are 3 coins left, the next player can take away 1 coin, forcing their opponent into a losing position with 2 coins.

Coins in stackPosition for the player that moves next
0Lose
1Win
2Lose
3Win
4Win
5Win

Let’s extend the table a bit more.

Coins in stackPosition for the player that moves next
0Lose
1Win
2Lose
3Win
4Win
5Win
6Win
7Win
8Lose
9Win
10Lose

As we learnt in Adversarial Games, a position is a winning position if there exists a move for the current player to put the other player in a losing position. A position is a losing position if ALL moves put the other player in a winning position.

A stack of 6 coins is a winning position since you can take 4 coins to leave the other player with a stack of 2 coins: a losing position.

A stack of 7 coins is a winning position since you can take 5 coins to leave the other player with a stack of 2 coins: a losing position.

A stack of 8 coins is a losing position, since taking 1, 4, or 5 coins leaves you with a stack of 7, 4, or 3 coins, all of which are winning positions for their opponent.

Notice that the table is starting to repeat itself. This is because we can determine if a position is winning or losing by looking up to 5 positions up the table. Once it starts repeating, it will repeat all the way down.

Coins in stackPosition for the player that moves next
0Lose
1Win
2Lose
3Win
4Win
5Win
6Win
7Win
8Lose
9Win
10Lose
11Win
12Win
13Win
14Win
15Win
16Lose
17Win
18Lose

In general, we see that the losing positions are positions where the stack of coins are (a) a multiple of 8, or (b) 2 more than a multiple of 8.

Since 100 is 4 more than a multiple of 8, it is a winning position, so you should choose to go first. On your turn, you want to take a number of coins to leave your opponent in a losing position. To do so, you can follow the strategy below: divide the number of coins by 8 to get the remainder kkk, then follow the table below to see what action to take.

kkkWhat to do
0Losing position: all moves lead to a loss in optimal play.
1Take 1 coin
2Losing position: all moves lead to a loss in optimal play.
3Take 1 coin
4Take 4 coins
5Take 5 coins
6Take 4 coins
7Take 5 coins

Try These Next