Project Euler Lab - Problem 905

#905 - Now I Know

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

Three epistemologists, known as A, B, and C, are in a room, each wearing a hat with a number on it. They have been informed beforehand that all three numbers are positive and that one of the numbers is the sum of the other two.

Once in the room, they can see the numbers on each other's hats but not on their own. Starting with A and proceeding cyclically, each epistemologist must either honestly state "I don't know my number" or announce "Now I know my number!" which terminates the game.

For instance, if their numbers are \(A=2, B=1, C=1\) then A declares "Now I know" at the first turn. If their numbers are \(A=2, B=7, C=5\) then "I don't know" is heard four times before B finally declares "Now I know" at the fifth turn.

Let \(F(A,B,C)\) be the number of turns it takes until an epistemologist declares "Now I know", including the turn this declaration is made. So \(F(2,1,1)=1\) and \(F(2,7,5)=5\).

Find \(\displaystyle \sum_{a=1}^7 \sum_{b=1}^{19} F(a^b, b^a, a^b + b^a)\).

This problem is taken from Project Euler, Problem 905.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=905. Published Saturday, 31st August 2024, 05:00 pm. Solved by 379 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:
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: #227 · #641 · #306

Concepts: game-theory 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.