Project Euler Problem 197

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

Project Euler Problem 197

Solution

Answer: 1.710637717

Let

$$f(x)=\left\lfloor 2^{30.403243784-x^2}\right\rfloor \cdot 10^{-9}, \qquad u_{n+1}=f(u_n),\qquad u_0=-1.$$

We must compute

$$u_{10^{12}}+u_{10^{12}+1}$$

to 9 decimal places.

The crucial observation is that the sequence does not converge to a single fixed point. Instead, it converges extremely rapidly to a 2-cycle:

$$u_{2k}\to a,\qquad u_{2k+1}\to b,$$

with

$$a=f(b),\qquad b=f(a).$$

Therefore, for enormous $n$,

$$u_n+u_{n+1}\approx a+b.$$

So we only need to iterate until the alternating terms stabilize.


Mathematical Analysis

Define

$$u_{n+1}=f(u_n).$$

A fixed point would satisfy

$$u=f(u),$$

but numerically the iteration oscillates between two nearby values.

Hence we instead seek a 2-cycle:

$$a=f(b),\qquad b=f(a).$$

Equivalently,

$$f(f(a))=a.$$

Because the floor operation makes the map strongly contractive near the limit cycle, convergence is very fast.

The stopping criterion suggested in the problem statement is

$$|u_{n+2}-u_n|<10^{-12}.$$

Once this occurs, the sequence has effectively settled into the alternating pair.


Python Implementation

from decimal import Decimal, getcontext, ROUND_FLOOR

# Use high precision
getcontext().prec = 50

TEN_NEG_9 = Decimal('1e-9')
C = Decimal('30.403243784')
TWO = Decimal(2)

def f(x):
    """
    Compute:
        floor(2^(30.403243784 - x^2)) * 1e-9
    using Decimal arithmetic.
    """
    exponent = C - x * x

    # Decimal power
    value = TWO ** exponent

    # floor(value)
    floored = value.to_integral_value(rounding=ROUND_FLOOR)

    return floored * TEN_NEG_9

# Initial value
u0 = Decimal(-1)

u_prev2 = u0
u_prev1 = f(u_prev2)

while True:
    u = f(u_prev1)

    # Check convergence to a 2-cycle
    if abs(u - u_prev2) < Decimal('1e-12'):
        break

    u_prev2, u_prev1 = u_prev1, u

# The 2-cycle values
a = u_prev2
b = u_prev1

answer = a + b

# Print with exactly 9 digits after decimal
print(format(answer, '.9f'))

Code Walkthrough

Precision setup

getcontext().prec = 50

We use 50 decimal digits to avoid accumulated rounding issues.


Defining the function

def f(x):

Implements

$$f(x)=\left\lfloor 2^{30.403243784-x^2}\right\rfloor \cdot 10^{-9}.$$

Exponent

exponent = C - x * x

Computes $30.403243784-x^2$.

Power

value = TWO ** exponent

Computes $2^{30.403243784-x^2}$.

Floor

floored = value.to_integral_value(rounding=ROUND_FLOOR)

Applies the floor function exactly.

Scale

return floored * TEN_NEG_9

Multiplies by $10^{-9}$.


Iteration

u_prev2 = u0
u_prev1 = f(u_prev2)

Stores consecutive terms.

Then repeatedly:

u = f(u_prev1)

computes the next term.


Detecting the 2-cycle

if abs(u - u_prev2) < Decimal('1e-12'):

Since the sequence alternates,

$$u_n \approx u_{n+2}$$

after convergence.


Final sum

answer = a + b

Once converged, $a+b$ equals

$$u_{10^{12}}+u_{10^{12}+1}$$

to the required precision.


Numerical Result

The iteration converges to approximately

$$a \approx 0.681175878, \qquad b \approx 1.029461839.$$

Hence

$$a+b \approx 1.710637717.$$


Final Verification

  • Values remain bounded near $1$, consistent with the exponent size.
  • The sequence clearly alternates between two values.
  • The sum stabilizes to 9 decimal places rapidly.
  • This matches the known behavior of Project Euler 197.

Answer: 1.710637717