Project Euler Problem 235

Given is the arithmetic-geometric sequence u(k) = (900-3k)r^{k - 1}.

Project Euler Problem 235

Solution

Answer: 1.002322108633

We are given the arithmetic–geometric sequence

$$u(k)=(900-3k)r^{k-1},$$

and

$$s(n)=\sum_{k=1}^n u(k).$$

We must find the value of $r$ such that

$$s(5000)=-600000000000.$$

The answer must be rounded to $12$ decimal places.


Mathematical analysis

We want to solve

$$\sum_{k=1}^{5000}(900-3k)r^{k-1}=-6\times10^{11}.$$

Define

$$S(r)=\sum_{k=1}^{5000}(900-3k)r^{k-1}.$$

We seek the root of

$$F(r)=S(r)+6\times10^{11}=0.$$


Why binary search works

Observe:

  • At $r=1$,

$$S(1)=\sum_{k=1}^{5000}(900-3k),$$

which is finite and relatively small compared to $6\times10^{11}$.

  • As $r$ increases above $1$, the later negative terms dominate because:

  • eventually $900-3k<0$,

  • and $r^{k-1}$ grows exponentially.

Hence $S(r)$ decreases rapidly and continuously.

Therefore there is a unique solution in an interval such as $[1,2]$, making binary search ideal.


Numerical strategy

We compute

$$S(r)=\sum_{k=1}^{5000}(900-3k)r^{k-1}$$

directly using floating-point arithmetic.

Then:

  • if $S(r)>-6\times10^{11}$, we need larger $r$,
  • otherwise we need smaller $r$.

We repeatedly bisect the interval until

$$|r_{\text{high}}-r_{\text{low}}|<10^{-15}.$$

Finally we round to $12$ decimal places.


Python implementation

TARGET = -600000000000.0

def S(r):
    total = 0.0
    power = 1.0  # r^(k-1)

    for k in range(1, 5001):
        total += (900 - 3 * k) * power
        power *= r

    return total

# Binary search interval
low = 1.0
high = 2.0

# Perform binary search
while high - low > 1e-15:
    mid = (low + high) / 2.0

    if S(mid) > TARGET:
        low = mid
    else:
        high = mid

r = (low + high) / 2.0

print(f"{r:.12f}")

Code walkthrough

Target value

TARGET = -600000000000.0

We store the required value of $s(5000)$.


Function to compute the sum

def S(r):

Defines the function

$$S(r)=\sum_{k=1}^{5000}(900-3k)r^{k-1}.$$


Running power

power = 1.0

Initially $r^{0}=1$.


Summation loop

for k in range(1, 5001):
    total += (900 - 3 * k) * power
    power *= r

Each iteration adds

$$(900-3k)r^{k-1}.$$

Then multiplies by $r$ to prepare the next power.


low = 1.0
high = 2.0

The root lies in this interval.


Midpoint test

mid = (low + high) / 2.0

Evaluate the center.


Decide direction

if S(mid) > TARGET:
    low = mid
else:
    high = mid

Since $S(r)$ decreases with $r$:

  • if too large, increase $r$,
  • otherwise decrease $r$.

Precision stopping

while high - low > 1e-15:

This guarantees enough precision for $12$-digit rounding.


Verification

Known checks:

  • $S(1)$ is only moderately negative,
  • larger $r$ makes the exponentially weighted negative tail dominate,
  • the solution should therefore be slightly above $1$.

The computed value is:

$$r \approx 1.002322108633$$

which matches the known Project Euler result.


Answer: 1.002322108633