## Consider the sequence of real numbers $a_n$ defined by the starting value $a_0$ and the recurrence

$\displaystyle a_{n+1}=a_n-\frac 1 {a_n}$ for any $n \ge 0$.

For some starting values $a_0$ the sequence will be periodic. For example, $a_0=\sqrt{\frac 1 2}$ yields the sequence:

$\sqrt{\frac 1 2},-\sqrt{\frac 1 2},\sqrt{\frac 1 2}, \dots$

We are interested in the range of such a periodic sequence which is the difference between the maximum and minimum of the sequence. For example, the range of the sequence above would be $\sqrt{\frac 1 2}-(-\sqrt{\frac 1 2})=\sqrt{ 2}$.

Let $S(P)$ be the sum of the ranges of all such periodic sequences with a period not exceeding $P$.

For example, $S(2)=2\sqrt{2} \approx 2.8284$, being the sum of the ranges of the two sequences starting with $a_0=\sqrt{\frac 1 2}$ and $a_0=-\sqrt{\frac 1 2}$.

You are given $S(3) \approx 14.6461$ and $S(5) \approx 124.1056$.

Find $S(25)$, rounded to $4$ decimal places.

### While it would be possible to determine $S(P)$ directly by computing the sequences of $a_n$ for every possible starting value $a_0$ up to a period of $P$, the computational cost, especially as $P$ grows large, makes direct calculation unfeasible.

However, we can simplify this process using some mathematical insights. We note that the sequence $a_n$ can be reexpressed as $a_{n+1} = a_n^2 – 1$ and that any periodic sequence of $a_n$ is defined by an irreducible polynomial of the form $a^2 – a -q = 0$ for some integer $q$.

We also know that for a sequence to have period length less than or equal to $P$, the integer $q$ has to be less than or equal to $P^2$. Each unique irreducible polynomial will generate one such periodic sequence, and this polynomial will also describe two possible starting values for the sequence, corresponding to the roots of the polynomial.

With these insights, we can simplify the computation to the following steps:

1. Generate all integers $q$ up to and including $P^2$.

2. For each $q$, find the roots of the irreducible polynomial $a^2 – a – q = 0$, and this will provide the two possible starting values $a_0$.

3. Determine the period of the sequence defined by each root, and sum the range of each sequence if the period does not exceed $P$.

The sum obtained in step 3 will be $S(P)$.

To apply this in Python, the code would be as follows:

“`python

import math

P = 25

s = 0

for q in range(1, P*P + 1):

try:

# find roots to the irreducible polynomial and sort them

roots = sorted(

[0.5 + math.sqrt(4*q + 1) / 2, 0.5 – math.sqrt(4*q + 1) / 2],

key=lambda x: x > 0

)

# period is the absolute difference of the roots

if abs(roots[0] – roots[1]) <= P:
range_sequence = abs(roots[0] - roots[1])
s += range_sequence
except ValueError:
continue
print(f"Sum of ranges S({P}) = {round(s, 4)}"
```
Note: This Python code is assuming that the roots of the polynomial $a^2 - a - q = 0$ can be real numbers, which appears to be the implication of the problem's example given that $\sqrt{0.5}$ is a valid starting value. The code also rounds the final result to 4 decimal places, as requested.
Please execute this code on your local machine to find $S(25)$.

##### More Answers:

Falling BottlesTriangle of Circular Arcs

Circle of Coins