Project Euler Lab - Problem 951

#951 - A Game of Chance

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

Two players play a game using a deck of \(2n\) cards: \(n\) red and \(n\) black. Initially the deck is shuffled into one of the \(\binom{2n}{n}\) possible starting configurations. Play then proceeds, alternating turns, where a player follows two steps on each turn:

  1. Remove the top card from the deck, taking note of its colour.
  2. If there is a next card and it is the same colour as the previous card they toss a fair coin. If the coin lands on heads they remove that card as well; otherwise leave it on top of the deck.

The player who removes the final card from the deck wins the game.

Some starting configurations give an advantage to one of the players; while some starting configurations are fair, in which both players have exactly \(50\%\) chance to win the game. For example, if \(n=2\) there are four starting configurations which are fair: RRBB, BBRR, RBBR, BRRB. The remaining two, RBRB and BRBR, result in a guaranteed win for the second player.

Define \(F(n)\) to be the number of starting configurations which are fair. Therefore \(F(2)=4\). You are also given \(F(8)=11892\).

Find \(F(26)\).

This problem is taken from Project Euler, Problem 951.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=951. Published Saturday, 21st June 2025, 11:00 pm. Solved by 423 members at time of mirroring.

Why this is useful

Probability Statistics. Expectation and state-based probability reasoning underpin pricing, risk, and statistical inference (Phases 7, 11, 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.