Game of Nim

"I know a game I always win."
"It's not much of a game if you can't lose."
"Oh, I can lose, but I always win."

James Fixx described this two-player game in his 1972 book Games for the Superintelligent, although it is certainly older, appearing in the 1961 French film Last Year at Marienbad.

The game begins with matchsticks arranged in three rows: three on the top, five in the middle and seven at the bottom. Players take turns removing matches, from only one row at a time. The objective is to remove the last match.

The solution involves writing the number of matchsticks in each row in binary and making selections so that there are an even number of each binary digit.

One popular variant (aka misère nim) requires one to force one's opponent to remove the last match.

Follow the aforementioned strategy, except when this would leave only single matches, in which case you want to leave an odd number of single matches.

More general versions of both games exist with different numbers and sizes of rows (or piles, as the case may be). In all cases, depending on the starting position note , either one side or the other can follow the strategies above, and cannot be beaten if the player follows it carefully. If they make a mistake, the other player may take over with said strategy. This makes it very easy to implement in a computer game - the human player has the advantage and can win if he does the right thing, but a single slip-up and the computer will win instead.

This trope makes an appearance in:

    open/close all folders 

     Anime & Manga 
  • One episode of Yu-Gi-Oh! (the Toei anime) had Dark Yugi challenge the school's Alpha Bitch to misère nim, with the matchsticks in question being the flowers on the latter's person. As usual, Dark Yugi pulls a come-from-behind victory - while his opponent focused entirely on the bouquet she was holding, she forgot she had an extra flower in her hair.


     Live Action TV  

  • Yucky Choccy (below) was played in an episode of Dara Ó Briain's School Of Hard Sums, with a tray of chocolates and a chilli pepper.


  • Yucky Choccy, a version involving a bar of chocolate, the corner chunk of which is soap, so the loser is the person who has to eat the soap, is described in one of Ian Stewart's maths columns in Scientific American.

     Video Games  

  • Honeycomb Havoc in Mario Party 2 where you have a tree with fruits, coins, and beehives set up in a row, and each of the four players can take one or two items from the tree — if you get the beehive, you're out.
  • Space Rangers includes this as one of the text quests concerning game tournaments. Like other stock puzzles, it provides galactic prestige for winning.
  • A variety of this game made an appearance in Super Mario RPG, in which you had to make sure your opponent took the last coin from a box... Although the CPU apparently didn't know how to count, as he would often take multiple coins if there was more than one coin left, causing him to lose.
  • The Secret Island of Dr. Quandary had DiscAppear, which is a Nim variant played with compact discs with titles like "The Boston Pops Play the Beach Boys". It's implied that whoever gets stuck with the last disc has to listen to it, but fortunately the player is spared such a fate. What's not obvious at all is that the hardest difficulty level inverts the goal by changing the ingredient needed from this mini-game.
  • One EX Mission in Shin Megami Tensei: Strange Journey is to win at a counting-based variant of this game against one of your crewmates, called "Don't Count Thirty". The solution is to control every fourth number - 1, 5, 9, 13, 17, 21, 25, and 29.
  • In the Pokémon Black and White Dream World, you can play a similar game against ghost-type Pokémon, where the players take turns blowing out candles. Some candles, however, give you extra points, while others subtract them, and later ones take two turns to extinguish.
  • In Teenage Mutant Ninja Turtles: Fall of the Foot Clan for the Nintendo Game Boy, there is a bonus stage where you play a similar game against Krang by removing shurikens until there are none left, with the winner being the one who removes the last shuriken.
  • A similar game appears in the bonus chapter of the collector's edition of The Keepers: Lost Progeny. You play against the computer, with each player taking it in turns to remove between one and three daggers. Unfortunately, you can't win in ordinary play, as a) you always go first, and b) the computer follows every move by taking (4 - n) daggers, where n is the number you took. You can only "win" by playing three games - during the third, the computer will make a deliberate mistake.
  • Shows up twice in Professor Layton in Spectre's Call — first time as a puzzle involving a fountain and some valves, second time involving the hotel owner and taking the last bottle.
    • Azran Legacy has another variation.
  • Shows up as a mandatory puzzle in an amusement park in OFF, where you take turns popping balloons, picking to pop one, two, or three each turn.
  • Used in Zork Zero, like many other Stock Puzzles. Fortunately, you can cheat — the number of flowers in each corner of the screen when you're playing Nim gives you a winning move each turn.
  • The Interactive Fiction title Humbug pits you against an octopus in a game of Nim.


     Western Animation  

     Real Life  

  • It's possible to use this game to win money in bars. After all, you're guaranteed a win if you know the winning strategy.
    • A "better" variant, which can truly befuddle your opponent, is to ask your opponent to lay out as many matches as they want, split into as many piles as they want, while you decide whether to go first or second. Once again, the winning strategy is given above.