Getting Stuck: The Nim Variant That Played Me
I like to believe that I’ve learned to recognize traps in mathematical problems. Especially in games. If something looks too neat, too regular, too easy, I get suspicious. But this time, the trap wasn’t in the setup — it was in my own head.
The Game That Looked Harmless
Here’s the problem:
-
One pile of
nstones. -
Two players alternate.
-
On your turn, you may remove 1, 3, or 4 stones.
-
Whoever takes the last stone wins.
That’s it. Barebones, no hidden twists. Just arithmetic.
I’ve seen countless problems like this. My instinct: there’s always a neat modular pattern in the losing positions. Like in ordinary Nim (remove 1 or 2 stones), where the losing positions are just multiples of 3. I was convinced I’d crack this in fifteen minutes.
Spoiler: it took me an entire evening, and I ended up learning more about myself than the stones.
My First Pass: Confidence at Full Blast
I began the way every combinatorial game analyst does: mark small cases.
-
0→ losing (no move). -
1→ winning (take 1). -
2→ losing (forced to leave1, which is winning). -
3→ winning (take all 3). -
4→ winning (take all 4). -
5→ winning (take 3 → leave2, which is losing).
So far, clean. I rubbed my hands: maybe all evens are losing?
The Betrayal of 6
Next case:
6.
-
Take 1 → leave
5(winning). Bad. -
Take 3 → leave
3(winning). Bad. -
Take 4 → leave
2(losing). Bingo! That’s good.
So 6 is winning. My “all evens are losing” hypothesis immediately collapsed.
I tried to rescue it: maybe it’s “every even number except 6”? That felt like stretching duct tape over a broken bridge.
The Catastrophe of 7
Then came 7.
-
Take 1 → leave
6(winning). -
Take 3 → leave
4(winning). -
Take 4 → leave
3(winning).
Every single move loses. Which means 7 is a losing position.
This was irritating. I thought: maybe the losing numbers are every other odd one? 2, 7… maybe 12, 17… I scribbled down guesses.
Descent Into Pattern-Chasing
At this point, I was chasing phantom structures.
-
Maybe the losing positions were all
n mod 5 = 2? Nope: 2 works, 7 doesn’t. -
Maybe they were spaced by 5, then 2, then 4, in some cycle? Didn’t match.
-
Maybe it was
mod 7? Again, inconsistent.
I started writing little tables, convinced I was “just one more case” away from spotting it.
By n = 15, I had a mess: losing at 0, 2, 7, 9, 13, 15. My notebook looked like the diary of a conspiracy theorist — circles, arrows, false modular “rules,” everything except string and pushpins.
False Hope From Symmetry
I tried a different tack. Maybe the game has a “mirror strategy”: whatever my opponent does, I reply with something that pairs with it. That kind of symmetry trick works in some take-away games.
I attempted to define “pairs” of moves: if they take 1, I take 4; if they take 3, I take 2. Except — wait, I can’t even take 2. The move set doesn’t allow it.
I abandoned that idea quickly, but not before wasting half an hour drawing mirrored trees of moves.
Giving Up on Intuition
By this point, my head was fried. I had oscillated between “I see the pattern!” and “the universe is chaos” at least five times. The truth is, there’s only so long you can manually track moves before your brain starts hallucinating regularities.
So I finally caved and wrote a Python script.
def classify(n):
dp = [False] * (n + 1) # False = L, True = W
for i in range(1, n + 1):
dp[i] = any(i - m >= 0 and not dp[i - m] for m in (1, 3, 4))
return dp
dp = classify(25)
for i, state in enumerate(dp):
print(i, "W" if state else "L")
And the output:
0 L
1 W
2 L
3 W
4 W
5 W
6 W
7 L
8 W
9 L
10 W
11 W
12 W
13 L
14 W
15 L
16 W
17 W
18 W
19 L
20 W
21 L
22 W
23 W
24 W
25 L
I just stared at it. The losing positions weren’t multiples of anything. They weren’t following a neat congruence. They were… scattered.
The Humbling
I had assumed the game would bow to my pattern-hunting ego. Instead, it laughed at me. The losing positions were at 0, 2, 7, 9, 13, 15, 19, 21, 25… — not regular, but not random either. They were dictated by deeper structure.
The “structure” is called the Sprague–Grundy theorem, which says every impartial game can be reduced to Nim via Grundy numbers. If I had remembered that from the start, I could have approached the game systematically. Instead, I flailed in the dark.
When I finally computed Grundy numbers for each pile size, everything clicked: losing positions are exactly those with Grundy number 0. Of course. But by then, I was too exhausted to celebrate.
Reflection
This episode of Getting Stuck wasn’t just about a pile of stones. It was about how easily we can fool ourselves with patterns that aren’t there.
I went in expecting elegance. I got chaos.
I wanted a five-minute solution. I got a three-hour ego check.
I thought I was playing the game. Turns out, the game was playing me.
And that’s exactly why I love doing this.
Comments
Post a Comment