Project Euler Lab - Problem 555

#555 - McCarthy 91 Function

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

The McCarthy 91 function is defined as follows: \[ M_{91}(n) = \begin{cases} n - 10 & \text{if } n > 100 \\ M_{91}(M_{91}(n+11)) & \text{if } 0 \leq n \leq 100 \end{cases} \]

We can generalize this definition by abstracting away the constants into new variables: \[ M_{m,k,s}(n) = \begin{cases} n - s & \text{if } n > m \\ M_{m,k,s}(M_{m,k,s}(n+k)) & \text{if } 0 \leq n \leq m \end{cases} \]

This way, we have \(M_{91} = M_{100,11,10}\).

Let \(F_{m,k,s}\) be the set of fixed points of \(M_{m,k,s}\). That is, \[F_{m,k,s}= \left\{ n \in \mathbb{N} \, | \, M_{m,k,s}(n) = n \right\}\]

For example, the only fixed point of \(M_{91}\) is \(n = 91\). In other words, \(F_{100,11,10}= \{91\}\).

Now, define \(SF(m,k,s)\) as the sum of the elements in \(F_{m,k,s}\) and let \(S(p,m) = \displaystyle \sum_{1 \leq s < k \leq p}{SF(m,k,s)}\).

For example, \(S(10, 10) = 225\) and \(S(1000, 1000)=208724467\).

Find \(S(10^6, 10^6)\).

This problem is taken from Project Euler, Problem 555.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=555. Published Sunday, 10th April 2016, 04:00 am. Solved by 856 members at time of mirroring.

Why this is useful

General Problem Solving. Builds computational thinking, decomposition, and debugging discipline - transferable, without a specific financial application.

We classify relevance honestly - not every Euler problem is a trading application.

Prerequisites

Lessons that prepare you:
17.1 Python for Quants: NumPy, pandas, and Vectorization

Recommended stepping-stone problems: #191 · #346 · #679

Concepts: string-processing

Likely techniques: hashing

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.