Find all integers $n \geqslant 3$ for which there exist real numbers $a_{1}, a_{2}, \cdots, a_{n+2}$ such that $a_{1}=a_{n+1}, a_{2}=a_{n+2}$ and:

$$
a_{i} a_{i+1}+1=a_{i+2}
$$

for $1 \leqslant i \leqslant n$.
We can start by solving the case $n=3$. We denote $x=a_{1}$ and $y=a_{2}$, then $a_{3}=x y+1$, and $x, y$ must satisfy $x=x y^{2}+y+1$ and $y=x y^{2}+y+1$. This can be rewritten as $x(1-y)(1+y)=(1+y)$ and $y(1-x)(1+x)=(1+x)$. Thus, in particular, $x=y=-1$ is a solution. We also find that $x=-1, y=2$ and $x=2, y=-1$ are also solutions, and these are the only ones. Note also that $-1 \times 2+1=-1$.

Thus, when $n$ is divisible by 3, the sequence $-1,-1,2,-1,-1,2, \cdots,-1,-1,2$ works.

Let's show that it is necessary for 3 to divide $n$. The sequence is actually a circular sequence, and we have an example of a periodic solution with period 3. We set $a_{n+3}=a_{3}$. Consider:

$$
\sum_{i=1}^{n} a_{i} a_{i+3}=\sum_{i=1}^{n} a_{i} a_{i+1} a_{i+2}+a_{i}
$$

Since $a_{1}=a_{n+1}$ and $a_{2}=a_{n+2}$:

$$
\sum_{i=1}^{n} a_{i} a_{i+3}=\sum_{i=1}^{n} a_{i} a_{i+1} a_{i+2}+a_{i+2}
$$

and consequently

$$
\sum_{i=1}^{n} a_{i} a_{i+3}=\sum_{i=1}^{n} a_{i+2}^{2}=\sum_{i=1}^{n} a_{i}^{2}
$$

We deduce that we are in the case of equality in the rearrangement inequality, so for all $1 \leqslant i \leqslant n$, we have $a_{i}=a_{i+3}$.

If $n$ is not a multiple of 3, then 3 is coprime with $n$ since 3 is a prime number. This means that any integer $k$ can be written as $1+3 u+n v$ with integers $u, v$, so $a_{k}=a_{1}$. Thus the sequence is constant. This means that $a_{1}^{2}+1=a_{1}$, which is impossible because the polynomial $x^{2}-x+1$ has no real roots.

We conclude that such a sequence exists if and only if 3 divides $n$.

## Various exercises on sequences