Project Euler Lab - Problem 975

#975 - A Winding Path

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

Given a pair \((a,b)\) of coprime odd positive integers, define the function \[H_{a,b}(x)=\frac{1}{2}-\frac{1}{2(a+b)}\Bigl(b\cos(a\pi x)+a\cos(b\pi x)\Bigr) \]It can be seen that \(H_{a,b}(0)=0\), \(H_{a,b}(1)=1\), and \(0 < H_{a,b}(x) < 1\) for all \(x\) strictly between \(0\) and \(1\).

Given two such pairs \((a,b)\) and \((c,d)\), paths of infinitesimal width traverse the unit cube internally through every point \((x,y,z)\in [0,1]^3\) such that \(z=H_{a,b}(x)=H_{c,d}(y)\). Remarkably, it can be shown that the point \((0,0,0)\) is always connected to the opposite corner \((1,1,1)\). Furthermore, with the additional condition \(\gcd(a+b,c+d)\in\{2,4\}\), it can be shown that there is exactly one path connecting the two points.

0975_examples.png

Shown above are two examples, as viewed from above the cube. That is, we see the paths projected onto the \(xy\)-plane, with corresponding \(z\) values indicated with varying colour. In the second example some paths are coloured grey to indicate that, while they exist, they do not form part of the path from \((0,0,0)\) to \((1,1,1)\).

Define \(F(a, b, c, d)\) to be the sum of the absolute changes in height (or \(z\)-coordinate) over all uphill and downhill sections of the path from \((0,0,0)\) to \((1,1,1)\). In the first example above, the path climbs \(\approx4.00886\) over eleven uphill sections, and descends \(\approx3.00886\) over ten downhill sections, giving \(F(3,5,3,7)\approx7.01772\). You are also given \(F(7,17,9,19)\approx 26.79578\).

Let \(G(m, n)\) be the sum of \(F(p,q,p,2q-p)\) over all pairs \((p,q)\) of primes, \(m\leq p < q\leq n\). You are given \(G(3, 20)\approx463.80866\).

Find \(G(500,1000)\) giving your answer rounded to five digits after the decimal point.

This problem is taken from Project Euler, Problem 975.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=975. Published Saturday, 20th December 2025, 07:00 pm. Solved by 135 members at time of mirroring.

Why this is useful

Numerical Computing. Precision, conditioning, and numerical iteration transfer directly to pricing engines and model calibration (Phases 5, 13, 15).

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.