Starting with a positive integer $n$, a sequence is created satisfying the following rule: each term is obtained from the previous one by subtracting the largest perfect square that is less than or equal to the previous term, until reaching the number zero. For example, if $n=142$, we will have the following sequence of 5 terms:

$$
a_{1}=142, a_{2}=21, a_{3}=5, a_{4}=1, a_{5}=0
$$

since $21=142-11^{2}, 5=21-4^{2}, 1=5-2^{2}$ and $0=1-1^{2}$.

a) Give an example of a sequence that has exactly 6 terms.

b) Find the smallest value of $n$ for which the sequence thus created has exactly 7 terms.
Solution

a) An example is the sequence

$$
a_{1}=23, a_{2}=7=23-16, a_{3}=3=7-4, a_{4}=2=3-1, a_{5}=1=2-1, a_{6}=0=1-1
$$

b) Since $a_{n+1}=a_{n}-x^{2}$, with $x^{2} \leq a_{n}\frac{a_{n+1}-1}{2}$. To obtain the minimum value, the sequence will be constructed backwards, and at each step, the minimum estimate of the increment $x^{2}$ obtained previously will be used. We have $a_{7}=0$ and $a_{6} \geq 1^{2}$. Therefore,

$$
\begin{aligned}
a_{5} & =a_{6}+x^{2} \\
& \geq 1+1^{2} \\
a_{4} & =a_{5}+x^{2} \\
& \geq 2+1^{2} \\
a_{3} & =a_{4}+x^{2} \\
& \geq 3+2^{2} \\
a_{2} & =a_{3}+x^{2} \\
& \geq 7+4^{2} \\
a_{1} & =a_{2}+x^{2} \\
& \geq 23+144
\end{aligned}
$$

Thus, the smallest value of $n$ is $23+144=167$ and the sequence of 7 terms that will be created is

$$
a_{1}=167, a_{2}=23, a_{3}=7, a_{4}=3, a_{5}=2, a_{6}=1, a_{7}=0
$$