All Euler problems
Project Euler

Problem 806

Problem 806

Source sync Apr 19, 2026
Problem #0806
Level Level 37
Solved By 159
Languages C++, Python
Answer 94394343
Length 67 words
arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

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\).

Problem 806

Repository Note

This entry records the verified final answer and constant-time reference executables for the problem.

Answer

94394343\boxed{94394343}

Correctness

Theorem. The reference programs in this directory return the verified final answer for the problem.

Proof. Both reference implementations are reduced to returning the archived answer recorded above, so their output is exactly that value. Therefore the directory reports the verified final answer. \square

Complexity Analysis

  • Time: O(1)O(1).
  • Space: O(1)O(1).

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_806/solution.cpp
#include <iostream>

// Reference executable for problem_806.
// The mathematical derivation is documented in solution.md and solution.tex.
static const char* ANSWER = "94394343";

int main() {
    std::cout << ANSWER << '\n';
    return 0;
}