Project Euler Lab - Problem 568

#568 - Reciprocal Games II

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

Tom has built a random generator that is connected to a row of \(n\) light bulbs. Whenever the random generator is activated each of the \(n\) lights is turned on with the probability of \(\frac 1 2\), independently of its former state or the state of the other light bulbs.

While discussing with his friend Jerry how to use his generator, they invent two different games, they call the reciprocal games:
Both games consist of \(n\) turns. Each turn is started by choosing a number \(k\) randomly between (and including) \(1\) and \(n\), with equal probability of \(\frac 1 n\) for each number, while the possible win for that turn is the reciprocal of \(k\), that is \(\frac 1 k\).

In game A, Tom activates his random generator once in each turn. If the number of lights turned on is the same as the previously chosen number \(k\), Jerry wins and gets \(\frac 1 k\), otherwise he will receive nothing for that turn. Jerry's expected win after playing the total game A consisting of \(n\) turns is called \(J_A(n)\). For example \(J_A(6)=0.39505208\), rounded to \(8\) decimal places.

For each turn in game B, after \(k\) has been randomly selected, Tom keeps reactivating his random generator until exactly \(k\) lights are turned on. After that Jerry takes over and reactivates the random generator until he, too, has generated a pattern with exactly \(k\) lights turned on. If this pattern is identical to Tom's last pattern, Jerry wins and gets \(\frac 1 k\), otherwise he will receive nothing. Jerry's expected win after the total game B consisting of \(n\) turns is called \(J_B(n)\). For example \(J_B(6)=0.43333333\), rounded to \(8\) decimal places.

Let \(D(n)=J_B(n)−J_A(n)\). For example, \(D(6) = 0.03828125\).

Find the \(7\) most significant digits of \(D(123456789)\) after removing all leading zeros.
(If, for example, we had asked for the \(7\) most significant digits of \(D(6)\), the answer would have been 3828125.)

This problem is taken from Project Euler, Problem 568.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=568. Published Saturday, 3rd September 2016, 04:00 pm. Solved by 347 members at time of mirroring.

Why this is useful

Direct Quant. Markov/absorbing-state and simulation reasoning is exactly the machinery behind pricing, risk, and execution models (Phases 7, 8, 13).

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.