Project Euler Lab - Problem 323

#323 - Bitwise-OR Operations on Random Integers

● AppliedOfficial difficulty: 18%RandomnessTier B - browser, with the efficient algorithmNot viewed
↖ Euler Lab

Let \(y_0, y_1, y_2, \dots\) be a sequence of random unsigned \(32\)-bit integers
(i.e. \(0 \le y_i \lt 2^{32}\), every value equally likely).

For the sequence \(x_i\) the following recursion is given:

  • \(x_0 = 0\) and
  • \(x_i = x_{i - 1} \boldsymbol \mid y_{i - 1}\), for \(i \gt 0\). (\(\boldsymbol \mid\) is the bitwise-OR operator).

It can be seen that eventually there will be an index \(N\) such that \(x_i = 2^{32} - 1\) (a bit-pattern of all ones) for all \(i \ge N\).

Find the expected value of \(N\).
Give your answer rounded to \(10\) digits after the decimal point.

This problem is taken from Project Euler, Problem 323.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=323. Published Sunday, 6th February 2011, 07:00 am. Solved by 4,483 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 B - browser, with the efficient algorithm
Runs in the browser only with the intended efficient algorithm; a naive loop will hit the timeout.

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.