Project Euler Lab - Problem 328

#328 - Lowest-cost Search

● ResearchOfficial difficulty: 86%Maximize/minimize an objectiveTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

We are trying to find a hidden number selected from the set of integers \(\{1, 2, \dots, n\}\) by asking questions. Each number (question) we ask, has a cost equal to the number asked and we get one of three possible answers:

  • "Your guess is lower than the hidden number", or
  • "Yes, that's it!", or
  • "Your guess is higher than the hidden number".

Given the value of \(n\), an optimal strategy minimizes the total cost (i.e. the sum of all the questions asked) for the worst possible case. E.g.

If \(n=3\), the best we can do is obviously to ask the number "2". The answer will immediately lead us to find the hidden number (at a total cost \(= 2\)).

If \(n=8\), we might decide to use a "binary search" type of strategy: Our first question would be "\(\mathbf 4\)" and if the hidden number is higher than \(4\) we will need one or two additional questions.
Let our second question be "\(\mathbf 6\)". If the hidden number is still higher than \(6\), we will need a third question in order to discriminate between \(7\) and \(8\).
Thus, our third question will be "\(\mathbf 7\)" and the total cost for this worst-case scenario will be \(4+6+7={\color{red}\mathbf{17}}\).

We can improve considerably the worst-case cost for \(n=8\), by asking "\(\mathbf 5\)" as our first question.
If we are told that the hidden number is higher than \(5\), our second question will be "\(\mathbf 7\)", then we'll know for certain what the hidden number is (for a total cost of \(5+7={\color{blue}\mathbf{12}}\)).
If we are told that the hidden number is lower than \(5\), our second question will be "\(\mathbf 3\)" and if the hidden number is lower than \(3\) our third question will be "\(\mathbf 1\)", giving a total cost of \(5+3+1={\color{blue}\mathbf 9}\).
Since \({\color{blue}\mathbf{12}} \gt {\color{blue}\mathbf 9}\), the worst-case cost for this strategy is \({\color{red}\mathbf{12}}\). That's better than what we achieved previously with the "binary search" strategy; it is also better than or equal to any other strategy.
So, in fact, we have just described an optimal strategy for \(n=8\).

Let \(C(n)\) be the worst-case cost achieved by an optimal strategy for \(n\), as described above.
Thus \(C(1) = 0\), \(C(2) = 1\), \(C(3) = 2\) and \(C(8) = 12\).
Similarly, \(C(100) = 400\) and \(\sum \limits_{n = 1}^{100} C(n) = 17575\).

Find \(\sum \limits_{n = 1}^{200000} C(n)\).

This problem is taken from Project Euler, Problem 328.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=328. Published Saturday, 12th March 2011, 10:00 pm. Solved by 539 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.