Project Euler Lab - Problem 128

#128 - Hexagonal Tile Differences

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

A hexagonal tile with number \(1\) is surrounded by a ring of six hexagonal tiles, starting at "12 o'clock" and numbering the tiles \(2\) to \(7\) in an anti-clockwise direction.

New rings are added in the same fashion, with the next rings being numbered \(8\) to \(19\), \(20\) to \(37\), \(38\) to \(61\), and so on. The diagram below shows the first three rings.

By finding the difference between tile \(n\) and each of its six neighbours we shall define \(\operatorname{PD}(n)\) to be the number of those differences which are prime.

For example, working clockwise around tile \(8\) the differences are \(12, 29, 11, 6, 1\), and \(13\). So \(\operatorname{PD}(8) = 3\).

In the same way, the differences around tile \(17\) are \(1, 17, 16, 1, 11\), and \(10\), hence \(\operatorname{PD}(17) = 2\).

It can be shown that the maximum value of \(\operatorname{PD}(n)\) is \(3\).

If all of the tiles for which \(\operatorname{PD}(n) = 3\) are listed in ascending order to form a sequence, the \(10\)th tile would be \(271\).

Find the \(2000\)th tile in this sequence.

This problem is taken from Project Euler, Problem 128.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=128. Published Friday, 29th September 2006, 06:00 pm. Solved by 5,897 members at time of mirroring.

Why this is useful

Optimization. A genuine objective is optimised over a choice set - the same shape as calibration and optimal-execution problems (Phases 10, 16).

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.