Project Euler Lab - Problem 802

#802 - Iterated Composition

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

Let \(\Bbb R^2\) be the set of pairs of real numbers \((x, y)\). Let \(\pi = 3.14159\cdots\ \).

Consider the function \(f\) from \(\Bbb R^2\) to \(\Bbb R^2\) defined by \(f(x, y) = (x^2 - x - y^2, 2xy - y + \pi)\), and its \(n\)-th iterated composition \(f^{(n)}(x, y) = f(f(\cdots f(x, y)\cdots))\). For example \(f^{(3)}(x, y) = f(f(f(x, y)))\). A pair \((x, y)\) is said to have period \(n\) if \(n\) is the smallest positive integer such that \(f^{(n)}(x, y) = (x, y)\).

Let \(P(n)\) denote the sum of \(x\)-coordinates of all points having period not exceeding \(n\). Interestingly, \(P(n)\) is always an integer. For example, \(P(1) = 2\), \(P(2) = 2\), \(P(3) = 4\).

Find \(P(10^7)\) and give your answer modulo \(1\,020\,340\,567\).

This problem is taken from Project Euler, Problem 802.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=802. Published Sunday, 12th June 2022, 11:00 am. Solved by 326 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.