Skip to content
  • Overview
  • Curriculum
    • FLUMental maths and numerical fluency
    • COMBCounting and combinatorics
    • PROBProbability
    • GAMEGames, decision theory and puzzles
      • 1Expected-value games

        • Pricing a game: EV, re-rolls and when to stop
      • 2Game theory

        • Game theory: dominance, mixing and the indifference condition
      • 3Poker and decision theory

        • Poker for traders: pot odds, ranges and bluffing frequency
      • 4Logic puzzles and multi-agent problems

        • Logic puzzles: information bounds and common knowledge
      • 5Classic brainteasers

        • Measuring and timing puzzles
        • Weighing, searching and strategy puzzles
      • 6Combinatorial games

        • Nim, symmetry strategies and who wins
    • MMMarket making
    • MKTMarkets and products

Practise

  • Question bank
  • Mental arithmetic
  • Market simulator
  • Arbitrage trees
  • Horse racing
  • Bid book
  • Screening tests
  • Mock papers

Reference

  • Formula reference
  • Search

Your record

  • Review queue
  • Progress
  • Leaderboard
  • Profile
  • Invite friends
AccountSend feedback
  1. Curriculum
  2. /Trading and market making
  3. /Games, decision theory and puzzles
  4. /Combinatorial games

Nim, symmetry strategies and who wins

GAME · Chapter 6·12 min read·Asked at Jane Street, SIG, Optiver, Citadel

After this lesson you should be able to

  • Decide who wins a take-away game by finding the losing positions.
  • Play Nim correctly using the XOR rule.
  • Recognise when a mirroring strategy settles a game immediately.

Two-player games with no chance and no hidden information always have a winner determined from the start. The interview question is which player, and the answer comes from one of three techniques: find the pattern in small cases, apply the XOR rule for Nim, or find a symmetry you can mirror.

Definition 6.1

Winning and losing positions

P-position and N-position — A position is *losing* (a P-position, good for the previous player) if every move from it leads to a winning position. It is *winning* (an N-position, good for the player to move) if some move leads to a losing one. Under normal play the terminal position — no moves available — is losing, and everything else is built up from there.

Proposition 6.2

Tabulate the small cases

For a take-away game, write out positions 0,1,2,…0, 1, 2, \dots0,1,2,… and mark each as winning or losing using the definition. A pattern almost always emerges within the first half dozen, and once you have it you can state the answer for any size and prove it by induction in one sentence.

Holds when

  • Subtraction game allowing 1, 2 or 3: the losing positions are the multiples of 4.
  • Generally, if you may remove 1 to kkk, the losing positions are the multiples of k+1k+1k+1.
  • The strategy follows from the pattern: always move *to* a losing position.

Example 6.3

Twenty-one coins are on the table. Players alternate removing one, two or three, and whoever takes the last coin wins. Who wins, and how?

Show the worked solutionHide the worked solution

Worked solution

  1. Formula
    losing positions=multiples of k+1=4\text{losing positions} = \text{multiples of } k+1 = 4losing positions=multiples of k+1=4
  2. Substitute
    21=4×5+121 = 4 \times 5 + 121=4×5+1
  3. Solve
    21≢0(mod4)⇒the first player wins21 \not\equiv 0 \pmod 4 \Rightarrow \text{the first player wins}21≡0(mod4)⇒the first player wins
  4. take 1, leaving 20\text{take } 1, \text{ leaving } 20take 1, leaving 20
  5. Answer
    first player, by taking 1 then restoring a multiple of 4\text{first player, by taking 1 then restoring a multiple of 4}first player, by taking 1 then restoring a multiple of 4

Sanity check. After the opening move, whatever the opponent takes — ttt coins, with 1≤t≤31 \le t \le 31≤t≤3 — reply with 4−t4 - t4−t. The total falls by exactly four each round, so the opponent always faces a multiple of four and eventually faces zero.

Equation 6.4

Nim

Several piles; a move removes any positive number from one pile; the player taking the last object wins. The XOR — the "Nim-sum" — of the pile sizes decides everything.

losing position  ⟺  a1⊕a2⊕⋯⊕an=0\text{losing position} \iff a_1 \oplus a_2 \oplus \cdots \oplus a_n = 0losing position⟺a1​⊕a2​⊕⋯⊕an​=0
⊕\oplus⊕
Bitwise exclusive or: add the binary digits without carrying.
=0= 0=0
Every pile size cancels in every bit position, so the player to move loses.

Why XOR of all things. Two facts do all the work. From a Nim-sum of zero, *any* move breaks it — you have changed one pile, so at least one bit position no longer cancels. From a non-zero Nim-sum, there is always a move back to zero: look at the highest set bit, find a pile with that bit set, and reduce it so the remaining piles cancel. Together these say the zero positions are exactly the losing ones, and the winning strategy is "always hand your opponent a zero". The Sprague–Grundy theorem extends this to every impartial game, by assigning each position a Nim-value.

The rest of this lesson is in Premium

You have read the opening. 10 more sections follow, including 4 worked examples and 3 quick checks.

Start the free 7-day trialSign in

Nothing is charged for 7 days, and you can cancel before then. Or read Arithmetic that survives a clock in full, free.

← Weighing, searching and strategy puzzlesBack to Games, decision theory and puzzles →
On this page
  • Winning and losing positions
  • Tabulate the small cases
  • Worked example
  • Nim

QuantMax · 141 lessons · 1342 questions · c5c0caa

  • Premium
  • Arbitrage trees
  • Horse racing
  • Invite friends
  • Account
  • About QuantMax
  • Terms
  • Privacy

Firm names identify publicly reported question patterns and nothing more. QuantMax is not affiliated with, endorsed by, or recruiting for any firm named in the curriculum. Everything you do in lessons and the question bank is kept to your account.