Project Euler Lab - Problem 260

#260 - Stone Game

● AdvancedOfficial difficulty: 37%Two-player gamesTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

A game is played with three piles of stones and two players.
On each player's turn, the player may remove one or more stones from the piles. However, if the player takes stones from more than one pile, then the same number of stones must be removed from each of the selected piles.

In other words, the player chooses some \(N \gt 0\) and removes:

  • \(N\) stones from any single pile; or
  • \(N\) stones from each of any two piles (\(2N\) total); or
  • \(N\) stones from each of the three piles (\(3N\) total).

The player taking the last stone(s) wins the game.

A winning configuration is one where the first player can force a win.
For example, \((0,0,13)\), \((0,11,11)\), and \((5,5,5)\) are winning configurations because the first player can immediately remove all stones.

A losing configuration is one where the second player can force a win, no matter what the first player does.
For example, \((0,1,2)\) and \((1,3,3)\) are losing configurations: any legal move leaves a winning configuration for the second player.

Consider all losing configurations \((x_i, y_i, z_i)\) where \(x_i \le y_i \le z_i \le 100\).
We can verify that \(\sum (x_i + y_i + z_i) = 173895\) for these.

Find \(\sum (x_i + y_i + z_i)\) where \((x_i, y_i, z_i)\) ranges over the losing configurations with \(x_i \le y_i \le z_i \le 1000\).

This problem is taken from Project Euler, Problem 260.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=260. Published Friday, 16th October 2009, 01:00 pm. Solved by 1,547 members at time of mirroring.

Why this is useful

Algorithmic Development. Optimal substructure and state-space reasoning are exactly how American-option pricing and optimal execution are solved (Phases 13, 16).

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

Prerequisites

Lessons that prepare you:
17.1 Python for Quants: NumPy, pandas, and Vectorization · 19.8 Bit Manipulation and State Compression · 19.14 Computational Complexity, Feasibility Estimation, and Proving Algorithms Correct · 19.7 Dynamic Programming: Memoization and Tabulation

Recommended stepping-stone problems: #899 · #605 · #770

Concepts: game-theory string-processing brute-force-reduction

Likely techniques: bitmask-dp

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 C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided in a local notebook.

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.