Project Euler Lab - Problem 887

#887 - Bounded Binary Search

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

Consider the problem of determining a secret number from a set \(\{1, ..., N\}\) by repeatedly choosing a number \(y\) and asking "Is the secret number greater than \(y\)?".

If \(N=1\) then no questions need to be asked. If \(N=2\) then only one question needs to be asked. If \(N=64\) then six questions need to be asked. However, in the latter case if the secret number is \(1\) then six questions still need to be asked. We want to restrict the number of questions asked for small values.

Let \(Q(N, d)\) be the least number of questions needed for a strategy that can find any secret number from the set \(\{1, ..., N\}\) where no more than \(x + d\) questions are needed to find the secret value \(x\).

It can be proved that \(Q(N, 0) = N - 1\). You are also given \(Q(7, 1) = 3\) and \(Q(777, 2) = 10\).

Find \(\displaystyle \sum_{d=0}^7 \sum_{N=1}^{7^{10}} Q(N, d)\).

This problem is taken from Project Euler, Problem 887.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=887. Published Saturday, 20th April 2024, 05:00 pm. Solved by 316 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.7 Dynamic Programming: Memoization and Tabulation · 19.9 Graph Algorithms: BFS, DFS, Dijkstra, and Minimum Spanning Trees

Recommended stepping-stone problems: #313 · #692 · #961

Concepts: game-theory

Likely techniques: bfs-dfs

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.