Project Euler Lab - Problem 847

#847 - Jack's Bean

● ResearchOfficial difficulty: 93%CountingTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Jack has three plates in front of him. The giant has \(N\) beans that he distributes to the three plates. All the beans look the same, but one of them is a magic bean. Jack doesn't know which one it is, but the giant knows.

Jack can ask the giant questions of the form: "Does this subset of the beans contain the magic bean?" In each question Jack may choose any subset of beans from a single plate, and the giant will respond truthfully.

If the three plates contain \(a\), \(b\) and \(c\) beans respectively, we let \(h(a, b, c)\) be the minimal number of questions Jack needs to ask in order to guarantee he locates the magic bean. For example, \(h(1, 2, 3) = 3\) and \(h(2, 3, 3) = 4\).

Let \(H(N)\) be the sum of \(h(a, b, c)\) over all triples of non-negative integers \(a\), \(b\), \(c\) with \(1 \leq a + b + c \leq N\).
You are given: \(H(6) = 203\) and \(H(20) = 7718\).

A repunit, \(R_n\), is a number made up with \(n\) digits all '1'. For example, \(R_3 = 111\) and \(H(R_3) = 1634144\).

Find \(H(R_{19})\). Give your answer modulo \(1\,000\,000\,007\).

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