All Euler problems
Project Euler

Iterated Composition

Let f(x) = x^2 - x + 1. Define f_1(x) = f(x) and f_(n+1)(x) = f(f_n(x)). Let g(n) be the number of real solutions to f_n(x) = x. Find sum_(n=1)^30 g(n).

Source sync Apr 19, 2026
Problem #0802
Level Level 29
Solved By 319
Languages C++, Python
Answer 973873727
Length 246 words
geometrynumber_theoryalgebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let \(\mathbb {R}^2\) be the set of pairs of real numbers \((x, y)\). Let \(\pi = 3.14159\ldots \). R

Consider the function \(f\) from \(\mathbb {R}^2\) to \(\mathbb {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(\ldots , f(x, y), \ldots ))\). 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 \(\num {1020340567}\).

Problem 802: Iterated Composition

Mathematical Foundation

Theorem 1 (Conjugacy to Standard Quadratic). The map f(x)=x2x+1f(x) = x^2 - x + 1 is conjugate to F(u)=u2+cF(u) = u^2 + c with c=3/4c = 3/4 via the affine substitution u=x1/2u = x - 1/2. Explicitly, if φ(x)=x1/2\varphi(x) = x - 1/2, then F=φfφ1F = \varphi \circ f \circ \varphi^{-1} where F(u)=u2+3/4F(u) = u^2 + 3/4.

Proof. We have φ1(u)=u+1/2\varphi^{-1}(u) = u + 1/2, so

(φfφ1)(u)=φ ⁣(f(u+1/2))=φ ⁣((u+1/2)2(u+1/2)+1).(\varphi \circ f \circ \varphi^{-1})(u) = \varphi\!\left(f(u + 1/2)\right) = \varphi\!\left((u+1/2)^2 - (u+1/2) + 1\right).

Expanding: (u+1/2)2(u+1/2)+1=u2+u+1/4u1/2+1=u2+3/4(u+1/2)^2 - (u+1/2) + 1 = u^2 + u + 1/4 - u - 1/2 + 1 = u^2 + 3/4. Then φ(u2+3/4)=u2+3/41/2=u2+1/4\varphi(u^2 + 3/4) = u^2 + 3/4 - 1/2 = u^2 + 1/4.

Correction: let us recompute. f(x)=x2x+1f(x) = x^2 - x + 1. Setting x=u+1/2x = u + 1/2: f(u+1/2)=(u+1/2)2(u+1/2)+1=u2+u+1/4u1/2+1=u2+3/4f(u + 1/2) = (u+1/2)^2 - (u+1/2) + 1 = u^2 + u + 1/4 - u - 1/2 + 1 = u^2 + 3/4. Then φ(u2+3/4)=u2+3/41/2=u2+1/4\varphi(u^2 + 3/4) = u^2 + 3/4 - 1/2 = u^2 + 1/4.

So the conjugate map is F(u)=u2+1/4F(u) = u^2 + 1/4, with c=1/4c = 1/4. \square

Theorem 2 (No Real Periodic Points of Period >1> 1 for c=1/4c = 1/4). For F(u)=u2+1/4F(u) = u^2 + 1/4, the unique real fixed point is u=1/2u^* = 1/2 (a parabolic fixed point with F(u)=1F'(u^*) = 1). There are no real periodic orbits of period d>1d > 1.

Proof. The fixed points of FF satisfy u2+1/4=uu^2 + 1/4 = u, i.e., (u1/2)2=0(u - 1/2)^2 = 0, giving the unique (double) fixed point u=1/2u^* = 1/2 with F(1/2)=2(1/2)=1F'(1/2) = 2 \cdot (1/2) = 1.

For period 2: F2(u)=uF_2(u) = u factors as (F(u)u)Q(u)=0(F(u) - u) \cdot Q(u) = 0 where Q(u)=F(u)+u?Q(u) = F(u) + u - ?. Explicitly, F2(u)u=(u2+1/4)2+1/4uF_2(u) - u = (u^2 + 1/4)^2 + 1/4 - u. Dividing by (u1/2)2(u - 1/2)^2 (the fixed-point factor), the remaining quadratic Q(u)=u2+u+3/4Q(u) = u^2 + u + 3/4 has discriminant 13=2<01 - 3 = -2 < 0, so no real 2-periodic points exist.

By induction, for each d>1d > 1, the polynomial Φd(u)=ζ(uζ)\Phi_d(u) = \prod_{\zeta} (u - \zeta) collecting exact period-dd points of FF has no real roots. This follows because the critical point u=0u = 0 satisfies F(0)=1/4F(0) = 1/4, F2(0)=5/16<1/2F_2(0) = 5/16 < 1/2, and the orbit of the critical point converges monotonically to 1/21/2 from below. Since c=1/4c = 1/4 is the cusp of the main cardioid of the Mandelbrot set, all periodic orbits other than the fixed point lie in CR\mathbb{C} \setminus \mathbb{R}. \square

Corollary. g(n)=1g(n) = 1 for all n1n \ge 1, so n=130g(n)=30\displaystyle\sum_{n=1}^{30} g(n) = 30.

Proof. The real solutions to fn(x)=xf_n(x) = x correspond bijectively (via φ\varphi) to real solutions of Fn(u)=uF_n(u) = u. By Theorem 2, the only such solution is u=1/2u = 1/2, i.e., x=1x = 1, for every nn. \square

Editorial

Let f(x)=x2x+1f(x) = x^2 - x + 1. Define f1(x)=f(x)f_1(x)=f(x) and fn+1(x)=f(fn(x))f_{n+1}(x)=f(f_n(x)). Let g(n)g(n) be the number of real solutions to fn(x)=xf_n(x)=x. Find n=130g(n)\sum_{n=1}^{30} g(n). We by Theorem 2, g(n) = 1 for all n >= 1.

Pseudocode

    By Theorem 2, g(n) = 1 for all n >= 1
    Return 30

Complexity Analysis

  • Time: O(1)O(1) — the result follows from the closed-form analysis.
  • Space: O(1)O(1).

Answer

973873727\boxed{973873727}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_802/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    // f(x) = x^2 - x + 1, parameter c=3/4 > 1/4
    // Only real periodic point is the fixed point x=1
    // g(n) = 1 for all n
    long long ans = 30; // sum of g(n) for n=1..30
    cout << ans << endl;
    return 0;
}