4. Given the number $2009^{2009^{2009}}$, written in the decimal number system. In one step, we perform the following operation: we erase the first and last digit and add their sum to the number that remains after erasing the first and last digit.

a) If after a finite number of steps a two-digit number remains, can this two-digit number be a perfect square.

b) If after a finite number of steps a one-digit number remains, determine the one-digit number.
Solution. We use the following known results:

Property 1. Every natural number when divided by 3 and 9 gives the same remainder as the sum of its digits.

Property 2. The square of a natural number when divided by 3 gives a remainder of 0 or 1.

We will prove the following property.

Property 3. The remainder when divided by 9 does not change at any step. Proof. Let after some step the number $A=\overline{a_{n} a_{n-1} \ldots a_{1} a_{0}}$ is obtained. According to Property 1, we have

$$
A=\overline{a_{n} a_{n-1} \ldots a_{1} a_{0}} \equiv a_{n}+a_{n-1}+\ldots+a_{1}+a_{0}(\bmod 9)
$$

After removing the first and last digit, we get the number $\overline{a_{n-1} \ldots a_{1}}$. Thus, after one step, the number $B=\overline{a_{n-1} \ldots a_{1}}+a_{n}+a_{0}$ is obtained. Now we have

$$
B=\overline{a_{n-1} \ldots a_{1}}+a_{n}+a_{0} \equiv a_{n-1}+\ldots+a_{1}+a_{n}+a_{0}(\bmod 9)
$$

i.e., $A \equiv B(\bmod 9)$.

a) If after a finite number of steps a two-digit number $A$ is obtained, then according to Property 2, the number $A$ will give the same remainder when divided by 3 as the number $2009^{2009^{2009}}$. We have $2009 \equiv 2 \equiv-1(\bmod 3)$, so it follows that

$$
2009^{2009^{2009}} \equiv(-1)^{2009^{2009}} \equiv-1 \equiv 2(\bmod 3)
$$

According to Property 3 and the above, we get $A \equiv 2(\bmod 3)$, and according to Property 2, it cannot be a perfect square.

b) If after a finite number of steps a one-digit number $a$ is obtained, then according to Property 3, we get

$$
a \equiv 2009^{2009^{2009}}(\bmod 9)
$$

We have $2009 \equiv 2(\bmod 9)$, so it follows that

$$
2009^{2009^{2009}} \equiv 2^{2009^{2009}}(\bmod 9)
$$

and clearly $2^{3} \equiv-1(\bmod 9)$. Further,

$$
2009^{2009} \equiv 2^{2009} \equiv-1^{2009} \equiv-1 \equiv 2(\bmod 3)
$$

i.e.,

$$
2009^{2009}=3 k+2
$$

where $k$ is an odd natural number. Now we have

$$
2009^{2009^{2009}} \equiv 2^{3 k+2}=\left(2^{3}\right)^{k} \cdot 4 \equiv(-1) \cdot 4 \equiv 5(\bmod 9)
$$

from which it is clear that $a=5$.