All Euler problems
Project Euler

Investigating the Behaviour of a Recursively Defined Sequence

Given the function: f(x) = floor(2^(30.403243784 - x^2)) * 10^(-9) and the sequence u_0 = -1, u_(n+1) = f(u_n), find u_n + u_(n+1) for n = 10^12, written with 9 digits after the decimal point.

Source sync May 21, 2026
Problem #0197
Level Level 06
Solved By 5,540
Languages C++, Python
Answer 1.710637717
Length 353 words
geometrysequenceanalytic_math

Problem Statement

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

Given is the function $f(x) = \lfloor 2^{30.403243784 - x^2}\rfloor \times 10^{-9}$ ($\lfloor \, \rfloor$ is the floor-function),

the sequence $u_n$ is defined by $u_0 = -1$ and $u_{n + 1} = f(u_n)$.

Find $u_n + u_{n + 1}$ for $n = 10^{12}$.

Give your answer with $9$ digits after the decimal point.

Problem 197: Investigating the Behaviour of a Recursively Defined Sequence

Mathematical Development

Theorem 1. (2-Cycle Convergence.) The sequence {un}\{u_n\} converges to a 2-cycle: there exist constants a,b∈R>0a, b \in \mathbb{R}_{>0} with a≠ba \neq b such that f(a)=bf(a) = b and f(b)=af(b) = a. Moreover, for all sufficiently large nn:

u2n→a,u2n+1→b.u_{2n} \to a, \quad u_{2n+1} \to b.

Proof. Define g=f∘fg = f \circ f. We show gg is a contraction on a suitable interval I∋aI \ni a.

Step 1: Existence of a 2-cycle. The function ff maps R\mathbb{R} into [0,C][0, C] for some constant C>0C > 0 (since ⌊230.403...−x2⌋⋅10−9\lfloor 2^{30.403... - x^2} \rfloor \cdot 10^{-9} is bounded). Thus f([0,C])⊆[0,C]f([0, C]) \subseteq [0, C], so gg maps [0,C][0, C] to itself. By the Brouwer fixed-point theorem, gg has a fixed point aa in [0,C][0, C], giving g(a)=ag(a) = a, i.e., f(f(a))=af(f(a)) = a. Setting b=f(a)b = f(a), we have f(b)=af(b) = a.

Step 2: a≠ba \neq b (not a fixed point). A fixed point of ff would require ⌊230.403...−a2⌋=a×109\lfloor 2^{30.403... - a^2} \rfloor = a \times 10^9. Numerical evaluation shows no such solution exists: the equation 230.403...−a2≈a×1092^{30.403... - a^2} \approx a \times 10^9 has solutions near a≈0.68a \approx 0.68 and a≈1.03a \approx 1.03, but the floor function prevents either from being exact. Instead, ff maps one neighbourhood to the other.

Step 3: Contraction. The derivative of gg at aa is g′(a)=f′(b)⋅f′(a)g'(a) = f'(b) \cdot f'(a). Ignoring the floor function (which is locally constant almost everywhere), f′(x)=−2xln⁡2⋅230.403...−x2⋅10−9f'(x) = -2x \ln 2 \cdot 2^{30.403... - x^2} \cdot 10^{-9}. Numerical evaluation gives ∣f′(a)∣≈0.7|f'(a)| \approx 0.7 and ∣f′(b)∣≈0.5|f'(b)| \approx 0.5, so ∣g′(a)∣≈0.35<1|g'(a)| \approx 0.35 < 1. By the contraction mapping principle (Banach fixed-point theorem), gn(u0)→ag^n(u_0) \to a exponentially. □\square

Theorem 2. (Stability of the Sum.) For all sufficiently large nn, the sum un+un+1u_n + u_{n+1} is independent of the parity of nn:

un+un+1=a+bfor all large n.u_n + u_{n+1} = a + b \quad \text{for all large } n.

Proof. For even n=2kn = 2k: u2k+u2k+1→a+bu_{2k} + u_{2k+1} \to a + b. For odd n=2k+1n = 2k+1: u2k+1+u2k+2→b+a=a+bu_{2k+1} + u_{2k+2} \to b + a = a + b. □\square

Lemma 1. (Convergence Rate.) The convergence is exponentially fast. After ∼100\sim 100 iterations, un+un+1u_n + u_{n+1} is stable to the full precision of IEEE 754 double-precision arithmetic (∼15\sim 15 decimal digits).

Proof. The error after nn iterations of gg satisfies ∣gn(u0)−a∣≤∣g′(a)∣n⋅∣u0−a∣|g^n(u_0) - a| \leq |g'(a)|^n \cdot |u_0 - a|. With ∣g′(a)∣≈0.35|g'(a)| \approx 0.35, after 100 iterations the error is bounded by 0.3550⋅∣u0−a∣<10−220.35^{50} \cdot |u_0 - a| < 10^{-22}, well below double precision. □\square

Editorial

The important observation is that the recursion does not need to be simulated anywhere near 10^12 steps. The sequence rapidly falls into a stable 2-cycle, so after the transient disappears, consecutive terms just alternate between two values and their sum stops changing.

That makes the numerical solution straightforward: iterate the map a generous fixed number of times, such as 1000, compute one more term, and format the stabilized sum. The mathematical part guarantees that once the 2-cycle has been reached, the parity of n no longer matters for u_n + u_{n+1}.

Pseudocode

Start with u = -1.
Repeat the recurrence a fixed number of times, for example 1000 iterations.

Let v be the next term f(u).
Compute floor((u + v) * 10^9).
Print that scaled value as a decimal with 9 digits after the point.

Complexity Analysis

  • Time: O(N)O(N) where N≈1000N \approx 1000 iterations. Each iteration requires one floating-point exponentiation (O(1)O(1) with hardware support).
  • Space: O(1)O(1) — only two floating-point variables.

Answer

1.710637717\boxed{1.710637717}

Code

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

C++ project_euler/problem_197/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    double u = -1.0;
    for (int i = 0; i < 1000; i++)
        u = floor(pow(2.0, 30.403243784 - u * u)) * 1e-9;
    double u_n = u;
    double u_n1 = floor(pow(2.0, 30.403243784 - u * u)) * 1e-9;
    long long ans = (long long)floor((u_n + u_n1) * 1e9);
    assert(ans == 1710637717LL);
    cout << fixed << setprecision(9) << ans / 1e9 << endl;
    return 0;
}