Project Euler Lab - Problem 899

#899 - DistribuNim I

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

Two players play a game with two piles of stones. The players alternately take stones from one or both piles, subject to:

  1. the total number of stones taken is equal to the size of the smallest pile before the move;
  2. the move cannot take all the stones from a pile.

The player that is unable to move loses.

For example, if the piles are of sizes 3 and 5 then there are three possible moves. \[(3,5) \xrightarrow{(2,1)} (1,4)\qquad\qquad (3,5) \xrightarrow{(1,2)} (2,3)\qquad\qquad (3,5) \xrightarrow{(0,3)} (3,2)\]

Let \(L(n)\) be the number of ordered pairs \((a,b)\) with \(1 \leq a,b \leq n\) such that the initial game position with piles of sizes \(a\) and \(b\) is losing for the first player assuming optimal play.

You are given \(L(7) = 21\) and \(L(7^2) = 221\).

Find \(L(7^{17})\).

This problem is taken from Project Euler, Problem 899.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=899. Published Sunday, 14th July 2024, 05:00 am. Solved by 664 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.

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.