amog.fun∘
nim : Heaps → {P, N}

Nim

Take any number of stones from one heap. Whoever takes the last stone wins. Click a stone to take it and everything to its right.

Why Amog always wins

\begin{definition}The nim-sum $a \oplus b$ is binary addition without carrying: XOR, digit by digit.\end{definition}

\begin{theorem}[Bouton, 1901]\label{thm:bouton}A Nim position with heaps $h_1, \dots, h_k$ is a loss for the player to move if and only if $h_1 \oplus h_2 \oplus \cdots \oplus h_k = 0$.\end{theorem}

\begin{proof}From nim-sum $0$, any move changes exactly one heap, so the nim-sum becomes nonzero. From nim-sum $s \neq 0$, take the highest bit of $s$ and a heap $h$ with that bit set; then $h \oplus s < h$, so shrinking $h$ to $h \oplus s$ is legal and makes the nim-sum $0$. The empty position has nim-sum $0$ and the player to move has already lost.\end{proof}

\begin{remark}In misère Nim the last player to move loses. Play exactly as usual until your move would leave only heaps of size 1; then leave an odd number of them instead of an even one.\end{remark}

Every other game on these pages is secretly Nim too. By the Sprague–Grundy theorem, any finite impartial game is equivalent to a single Nim heap, whose size is the game's Grundy value.