4. A natural number $x$ in a base $r$ numeral system (where $r>3$) has the form $\overline{p p q q}$, and $q=2p$. It turns out that the $r$-ary representation of the number $x^2$ is a seven-digit palindrome $c$ with three identical middle digits. (A palindrome is a number that reads the same from left to right and from right to left). For which $r$ is this possible?
Answer: $r=3 n^{2}$, where $n-$ is a natural number, $n>1$.

Solution. Let's agree to write $u \equiv v(\bmod w)$ if $(u-v) \vdots w$. Notice that

$$
x=\overline{p p q q}_{r}=\left(p r^{2}+q\right)(r+1)=p\left(r^{2}+2\right)(r+1)
$$

From the condition on $x^{2}$, we get the equality

$$
p^{2}\left(r^{2}+2\right)^{2}\left(r^{2}+2 r+1\right)=a\left(1+r^{6}\right)+b\left(r+r^{5}\right)+c\left(r^{2}+r^{3}+r^{4}\right)
$$

where $a, b, c$ are some $r$-ary digits. Let's make several observations.

1) For any natural $n$

$$
r^{n}=(1+r-1)^{n} \equiv(-1)^{n}(1-n(1+r))\left(\bmod (1+r)^{2}\right)
$$

The right side of $(*)$ is divisible by $(1+r)^{2}$, from which

$$
0 \equiv a(2-6(1+r))-b(2-6(1+r))+c(1-3(1+r))=(1-3(1+r))(2 a-2 b+c)\left(\bmod (1+r)^{2}\right)
$$

Since $1-3(1+r)$ is coprime with $(1+r)^{2}$, $2 a-2 b+c$ is divisible by $(1+r)^{2}$. But this number lies in the interval $(-2 r, 3 r) \subset\left(-(1+r)^{2},(1+r)^{2}\right)$, hence $2 a=2 b-c$.

2) Let's equate the remainders of the left and right sides of $(*)$ modulo $1+r^{2}$. By 1)

$$
2 p^{2} r \equiv b r\left(1+r^{4}\right)+c r^{3} \equiv(2 b-c) r=2 a r\left(\bmod \left(1+r^{2}\right)\right)
$$

Since $r$ is coprime with $1+r^{2}$, $2\left(p^{2}-a\right)$ is divisible by $1+r^{2}$. But

$$
2\left(p^{2}-a\right)-2 a>-2 r>-1-r^{2}
$$

Thus, $a=p^{2}$.

3) Let's equate the remainders of the left and right sides of $(*)$ modulo $r$:

$$
4 p^{2} \equiv a(\bmod r) \Longleftrightarrow 4 a \equiv a(\bmod r) \Longleftrightarrow 3 a \vdots r \Longleftrightarrow 3 a=r \text { or } 3 a=2 r
$$

since $3 ap^{2}\left(r^{6}+2 r^{5}\right)=r^{6}\left(p^{2}+\frac{2 p^{2}}{r}\right)=r^{6}\left(p^{2}+\frac{4}{3}\right)>r^{6}\left(p^{2}+1\right)
$$

Therefore, the leading digit of $x^{2}$ is greater than $p^{2}$, and the trailing digit, by $(*)$, is $p^{2}$. Thus, $x^{2}$ is not a palindrome, and $r=\frac{3 p^{2}}{2}$ does not work.

Now let $r=3 p^{2}$. We will show that $x^{2}$ will be a palindrome of the required form for any $p>1$. Let's expand the brackets in the left side of $(*)$:

$$
\begin{aligned}
x^{2}=p^{2}\left(r^{4}+4 r^{2}+4\right)\left(r^{2}+2 r+1\right) & =4 p^{2}+8 p^{2} r+8 p^{2} r^{2}+8 p^{2} r^{3}+5 p^{2} r^{4}+2 p^{2} r^{5}+p^{2} r^{6}= \\
& =p^{2}+\left(2 p^{2}+1\right) r+\left(2 p^{2}+2\right) r^{2}+\left(2 p^{2}+2\right) r^{3}+\left(2 p^{2}+2\right) r^{4}+\left(2 p^{2}+1\right) r^{5}+p^{2} r^{6}
\end{aligned}
$$

(in the last line, the terms are regrouped taking into account the equality $3 p^{2}=r$). The coefficients of the powers of $r$ are the $r$-ary digits of $x^{2}$, since all of them do not exceed $2 p^{2}+2=r+2-p^{2}<r$. These digits obviously satisfy the condition of the problem.