How to always win the 21 game

Strategy · · 5 min read

How to always win the 21 game

The 21 game is the one maths teachers love: 21 stones, or 21 matches, or counting out loud to 21, with two players taking turns to take one, two or three. It looks like a guessing game. It is not. One player can win every single time, and after one short rule that player will be you.

This article gives the rule, shows why it works with nothing harder than division, covers the versions where the target number changes, and ends with the same idea for classic Nim with several piles — the game a mathematician solved completely in 1901.

The rules

Put 21 stones in a pile. Players take turns removing one, two or three stones — never none, and never more than three. Whoever takes the last stone wins.

The counting version is the same game in disguise. Players take turns saying the next one, two or three numbers — “1, 2”, then “3”, then “4, 5, 6” — and whoever says 21 wins. Saying a number is taking a stone, so everything below works for both.

The trick: leave a multiple of 4

After each of your moves, leave a number of stones that divides by 4: 20, 16, 12, 8, 4 and finally 0. Whatever your opponent takes, you take the rest of 4: if they take 1, you take 3; if they take 2, you take 2; if they take 3, you take 1. Every round removes exactly four stones, and the last stone is yours.

From 21, the first move is to take one stone, leaving 20. Here is a whole game with you moving first:

  • You take 1: 20 left.
  • They take 3, you take 1: 16 left.
  • They take 2, you take 2: 12 left.
  • They take 1, you take 3: 8 left.
  • They take 3, you take 1: 4 left. Whatever they take now, you take the rest and win.

Why it works

Count backwards from the end. With 1, 2 or 3 stones left, the player to move takes them all and wins. With exactly 4 left, the player to move is stuck: any move leaves 1, 2 or 3, and the other player wins. So 4 is a losing number to face. From 5, 6 or 7 you can hand your opponent that 4, so those are winning — and 8 is losing again, because every move from 8 lands on 5, 6 or 7.

The pattern repeats every four stones, because a round — their move plus yours — can always be made to total 4. That is why the losing numbers are the multiples of 4, and why the winning move is the remainder when the pile is divided by 4. From 21, 21 ÷ 4 leaves 1: take one.

When the trick is against you

If you are handed a multiple of 4, there is no winning move: against someone who knows the trick, you will lose. So take a single stone and keep the game long. Every extra turn is another chance for the other player to slip, and one slip is enough — the moment they leave a number that is not a multiple of 4, take the remainder and the trick is yours.

Other versions

The rule bends, but it never breaks. Work out how many stones one full round can remove, and aim for its multiples:

  • Take one to k stones instead of one to three: leave a multiple of k + 1. With a limit of 5, leave 18, 12, 6 and 0.
  • Last stone loses: leave one more than a multiple of 4 — 17, 13, 9, 5, 1 — so your opponent is forced to take the last one. Here 21 is already such a number, so the second player wins.
  • Whoever says 21 loses, in the counting version: aim to say 4, 8, 12, 16 and 20. Here too the second player can always win.

Classic Nim: the same idea in binary

In classic Nim there are several piles, and a move takes any number of stones from one pile. Counting back by hand gets slow, but in 1901 the Harvard mathematician Charles Bouton published the complete answer, and gave the game its name.

Write each pile as a sum of 4s, 2s and 1s: 7 is 4 + 2 + 1, 5 is 4 + 1, 3 is 2 + 1. Then count how many piles contain a 4, how many a 2 and how many a 1. If every count is even, the player to move loses against perfect play. If any count is odd, there is a move that makes them all even again — find it and play it.

Take the well-known 3-5-7 start. The 4s appear twice (in 5 and 7), the 2s twice (in 3 and 7), the 1s three times — odd. Take one stone from the 3 and you leave 2-5-7, where every count is even. Mathematicians call this the binary sum of the piles; the 21 game is the one-pile case of the same theory.

Practise it against the bot

Knowing the trick and using it under pressure are different things. On Duelto you can play Nim right now, without an account: the 21 game, a version with a changing limit, and classic piles, against a bot at three levels. You always move first from a position you can win, so even the Hard bot, which never misses a winning move, can be beaten — as long as you never let the multiple of 4 slip.

Lose a game and the bot shows you the moment it went wrong. Once Hard falls in the 21 game, try Limit, then Piles. After that, Nim online is rated: the version and the stones are drawn at random, either player may move first, and each has 60 seconds for the whole game, so the trick has to be fast as well as right.

Play Nim free — against the bot or a real opponent