Project Euler Lab - Problem 665

#665 - Proportionate Nim

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

Two players play a game with two piles of stones, alternating turns.

On each turn, the corresponding player chooses a positive integer \(n\) and does one of the following:

  • removes \(n\) stones from one pile;
  • removes \(n\) stones from both piles; or
  • removes \(n\) stones from one pile and \(2n\) stones from the other pile.

The player who removes the last stone wins.

We denote by \((n,m)\) the position in which the piles have \(n\) and \(m\) stones remaining. Note that \((n,m)\) is considered to be the same position as \((m,n)\).

Then, for example, if the position is \((2,6)\), the next player may reach the following positions:
\((0,2)\), \((0,4)\), \((0,5)\), \((0,6)\), \((1,2)\), \((1,4)\), \((1,5)\), \((1,6)\), \((2,2)\), \((2,3)\), \((2,4)\), \((2,5)\)

A position is a losing position if the player to move next cannot force a win. For example, \((1,3)\), \((2,6)\), \((4,5)\) are the first few losing positions.

Let \(f(M)\) be the sum of \(n+m\) for all losing positions \((n,m)\) with \(n\le m\) and \(n+m \le M\). For example, \(f(10) = 21\), by considering the losing positions \((1,3)\), \((2,6)\), \((4,5)\).

You are given that \(f(100) = 1164\) and \(f(1000) = 117002\).

Find \(f(10^7)\).

This problem is taken from Project Euler, Problem 665.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=665. Published Sunday, 14th April 2019, 10:00 am. Solved by 291 members at time of mirroring.

Why this is useful

Algorithmic Development. Graph/state-space search transfers to routing, dependency resolution, and execution-path optimisation (Phases 16, 17).

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

Prerequisites

Lessons that prepare you:
19.14 Computational Complexity, Feasibility Estimation, and Proving Algorithms Correct · 19.7 Dynamic Programming: Memoization and Tabulation · 19.9 Graph Algorithms: BFS, DFS, Dijkstra, and Minimum Spanning Trees

Recommended stepping-stone problems: #898 · #503 · #888

Concepts: game-theory brute-force-reduction

Likely techniques: bfs-dfs 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 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.