Project Euler Lab - Problem 411

#411 - Uphill Paths

● AdvancedOfficial difficulty: 48%Optimal substructureTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Let \(n\) be a positive integer. Suppose there are stations at the coordinates \((x, y) = (2^i \bmod n, 3^i \bmod n)\) for \(0 \leq i \leq 2n\). We will consider stations with the same coordinates as the same station.

We wish to form a path from \((0, 0)\) to \((n, n)\) such that the \(x\) and \(y\) coordinates never decrease.
Let \(S(n)\) be the maximum number of stations such a path can pass through.

For example, if \(n = 22\), there are \(11\) distinct stations, and a valid path can pass through at most \(5\) stations. Therefore, \(S(22) = 5\). The case is illustrated below, with an example of an optimal path:

0411_longpath.png

It can also be verified that \(S(123) = 14\) and \(S(10000) = 48\).

Find \(\sum S(k^5)\) for \(1 \leq k \leq 30\).

This problem is taken from Project Euler, Problem 411.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=411. Published Saturday, 19th January 2013, 10:00 pm. Solved by 804 members at time of mirroring.

Why this is useful

Algorithmic Development. Optimal substructure and state-space reasoning are exactly how American-option pricing and optimal execution are solved (Phases 13, 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 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.