Project Euler Lab - Problem 806

#806 - Nim on Towers of Hanoi

● ResearchOfficial difficulty: 98%Optimal substructureTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

This problem combines the game of Nim with the Towers of Hanoi. For a brief introduction to the rules of these games, please refer to Problem 301 and Problem 497, respectively.

The unique shortest solution to the Towers of Hanoi problem with \(n\) disks and \(3\) pegs requires \(2^n-1\) moves. Number the positions in the solution from index 0 (starting position, all disks on the first peg) to index \(2^n-1\) (final position, all disks on the third peg).

Each of these \(2^n\) positions can be considered as the starting configuration for a game of Nim, in which two players take turns to select a peg and remove any positive number of disks from it. The winner is the player who removes the last disk.

We define \(f(n)\) to be the sum of the indices of those positions for which, when considered as a Nim game, the first player will lose (assuming an optimal strategy from both players).

For \(n=4\), the indices of losing positions in the shortest solution are 3,6,9 and 12. So we have \(f(4) = 30\).

You are given that \(f(10) = 67518\).

Find \(f(10^5)\). Give your answer modulo \(1\,000\,000\,007\).

This problem is taken from Project Euler, Problem 806.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=806. Published Saturday, 9th July 2022, 11:00 pm. Solved by 166 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 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.