Project Euler Lab - Problem 593

#593 - Fleeting Medians

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

We define two sequences \(S = \{S(1), S(2), ..., S(n)\}\) and \(S_2 = \{S_2(1), S_2(2), ..., S_2(n)\}\):

\(S(k) = (p_k)^k \bmod 10007\) where \(p_k\) is the \(k\)th prime number.

\(S_2(k) = S(k) + S(\lfloor\frac{k}{10000}\rfloor + 1)\) where \(\lfloor \cdot \rfloor\) denotes the floor function.

Then let \(M(i, j)\) be the median of elements \(S_2(i)\) through \(S_2(j)\), inclusive. For example, \(M(1, 10) = 2021.5\) and \(M(10^2, 10^3) = 4715.0\).

Let \(F(n, k) = \sum_{i=1}^{n-k+1} M(i, i + k - 1)\). For example, \(F(100, 10) = 463628.5\) and \(F(10^5, 10^4) = 675348207.5\).

Find \(F(10^7, 10^5)\). If the sum is not an integer, use \(.5\) to denote a half. Otherwise, use \(.0\) instead.

This problem is taken from Project Euler, Problem 593.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=593. Published Saturday, 4th March 2017, 07:00 pm. Solved by 666 members at time of mirroring.

Why this is useful

Mathematical Foundation. Exact integer reasoning and algorithmic efficiency. Foundational rigour and computational discipline rather than a direct trading application.

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.