Project Euler Lab - Problem 866

#866 - Tidying Up B

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

A small child has a “number caterpillar” consisting of \(N\) jigsaw pieces, each with one number on it, which, when connected together in a line, reveal the numbers \(1\) to \(N\) in order.

Every night, the child's father has to pick up the pieces of the caterpillar that have been scattered across the play room. He picks up the pieces at random and places them in the correct order.
As the caterpillar is built up in this way, it forms distinct segments that gradually merge together.

Any time the father places a new piece in its correct position, a segment of length \(k\) is formed and he writes down the \(k\)th hexagonal number \(k\cdot(2k-1)\). Once all pieces have been placed and the full caterpillar constructed he calculates the product of all the numbers written down. Interestingly, the expected value of this product is always an integer. For example if \(N=4\) then the expected value is \(994\).

Find the expected value of the product for a caterpillar of \(N=100\) pieces. Give your answer modulo \(987654319\).

This problem is taken from Project Euler, Problem 866.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=866. Published Sunday, 3rd December 2023, 04:00 am. Solved by 551 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.