Project Euler Lab - Problem 547

#547 - Distance of Random Points Within Hollow Square Laminae

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

Assuming that two points are chosen randomly (with uniform distribution) within a rectangle, it is possible to determine the expected value of the distance between these two points.

For example, the expected distance between two random points in a unit square is about \(0.521405\), while the expected distance between two random points in a rectangle with side lengths \(2\) and \(3\) is about \(1.317067\).

Now we define a hollow square lamina of size \(n\) to be an integer sized square with side length \(n \ge 3\) consisting of \(n^2\) unit squares from which a rectangle consisting of \(x \times y\) unit squares (\(1 \le x,y \le n - 2\)) within the original square has been removed.

For \(n = 3\) there exists only one hollow square lamina:

0547-holes-1.png

For \(n = 4\) you can find \(9\) distinct hollow square laminae, allowing shapes to reappear in rotated or mirrored form:

0547-holes-2.png

Let \(S(n)\) be the sum of the expected distance between two points chosen randomly within each of the possible hollow square laminae of size \(n\). The two points have to lie within the area left after removing the inner rectangle, i.e. the gray-colored areas in the illustrations above.

For example, \(S(3) = 1.6514\) and \(S(4) = 19.6564\), rounded to four digits after the decimal point.

Find \(S(40)\) rounded to four digits after the decimal point.

This problem is taken from Project Euler, Problem 547.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=547. Published Sunday, 14th February 2016, 04:00 am. Solved by 270 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.