Project Euler Lab - Problem 301

#301 - Nim

● AppliedOfficial difficulty: 15%Two-player gamesTier B - browser, with the efficient algorithmNot viewed
↖ Euler Lab

Nim is a game played with heaps of stones, where two players take it in turn to remove any number of stones from any heap until no stones remain.

We'll consider the three-heap normal-play version of Nim, which works as follows:

  • At the start of the game there are three heaps of stones.
  • On each player's turn, the player may remove any positive number of stones from any single heap.
  • The first player unable to move (because no stones remain) loses.

If \((n_1,n_2,n_3)\) indicates a Nim position consisting of heaps of size \(n_1\), \(n_2\), and \(n_3\), then there is a simple function, which you may look up or attempt to deduce for yourself, \(X(n_1,n_2,n_3)\) that returns:

  • zero if, with perfect strategy, the player about to move will eventually lose; or
  • non-zero if, with perfect strategy, the player about to move will eventually win.

For example \(X(1,2,3) = 0\) because, no matter what the current player does, the opponent can respond with a move that leaves two heaps of equal size, at which point every move by the current player can be mirrored by the opponent until no stones remain; so the current player loses. To illustrate:

  • current player moves to \((1,2,1)\)
  • opponent moves to \((1,0,1)\)
  • current player moves to \((0,0,1)\)
  • opponent moves to \((0,0,0)\), and so wins.

For how many positive integers \(n \le 2^{30}\) does \(X(n,2n,3n) = 0\) ?

This problem is taken from Project Euler, Problem 301.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=301. Published Saturday, 11th September 2010, 04:00 pm. Solved by 7,429 members at time of mirroring.

Why this is useful

General Problem Solving. Builds computational thinking, decomposition, and debugging discipline - transferable, without a specific financial application.

We classify relevance honestly - not every Euler problem is a trading application.

Prerequisites

Lessons that prepare you:
19.7 Dynamic Programming: Memoization and Tabulation

Concepts: game-theory

Likely techniques: grundy

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier B - browser, with the efficient algorithm
Runs in the browser only with the intended efficient algorithm; a naive loop will hit the timeout.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.