This is a classic coin game (also known as a Nim variant) with the following rules:
- There is a pile of coins.
- Two players take turns removing 1 to 3 coins from the pile.
- The player who takes the last coin wins.
🧠 Goal:
Find the winning strategy for the first player.
✅ Key Insight: Winning and Losing Positions
We define:
- A winning position: The current player can force a win no matter what the opponent does.
- A losing position: No matter what move the current player makes, the opponent can always respond in a way that forces a win.
Let’s look at small examples to find a pattern.
🔢 Let's Try Small Cases:
| Coins Left | Player to Move Can Win? | Reason |
|---|
| 0 | ❌ No | You lost! |
| 1 | ✅ Yes | Take it and win |
| 2 | ✅ Yes | Take both |
| 3 | ✅ Yes | Take all |
| 4 | ❌ No | Whatever you do (take 1–3), opponent can take the rest and win |
| 5 | ✅ Yes | Take 1 → leave 4 (opponent in losing position) |
| 6 | ✅ Yes | Take 2 → leave 4 |
| 7 | ✅ Yes | Take 3 → leave 4 |
| 8 | ❌ No | Any move leaves opponent in winning position |
🧮 Pattern Observed:
The losing positions are when the number of coins is a multiple of 4:
Losing positions: n≡0(mod4)
If you're forced to move when there are 4, 8, 12, ... coins left, you will lose if your opponent plays optimally.
✅ Winning Strategy for First Player:
To guarantee a win, the first player must force the opponent into a losing position after each turn.
Step-by-step Strategy:
-
On the first move, the first player should remove enough coins so that the remaining number of coins is divisible by 4.
- If there are N coins initially:
- Remove r=Nmod4
- If r=0, you're already in a losing position — bad luck!
This ensures that after each full round (you + opponent), exactly 4 coins are removed, maintaining the total coins as a multiple of 4 — keeping the opponent in a losing position.
🎯 Example:
Suppose there are 25 coins.
- 25mod4=1, so first player removes 1 coin, leaving 24 coins (a multiple of 4).
- Now:
- Opponent removes 2 → you remove 2 → total removed this round: 4
- Opponent removes 3 → you remove 1 → total removed: 4
- etc.
Eventually, only 4 coins remain → opponent’s turn → you force them to face the 4-coin losing position.
They remove some coins, you take the last ones and win!
✅ Final Answer:
The first player has a winning strategy if the initial number of coins is not divisible by 4.
- First move: Remove Nmod4 coins (if not zero).
- Subsequent moves: Always remove 4−x coins, where x is how many the opponent just took.
This guarantees the second player always faces a multiple of 4 coins — a losing position.
📌 Summary:
| Number of Coins | Position Type | Winner (with perfect play) |
|---|
| n≡0mod4 | Losing | Second player |
| n≡ |