2. Determine all natural numbers $n$ such that

$$
\left(n^{3}+39 n-2\right) n!+17 \cdot 21^{n}+5
$$

is a perfect square.
Solution. Let us denote

$$
a_{n}=\left(n^{3}+39 n-2\right) n!+17 \cdot 21^{n}+5
$$

If $n \geq 4$, then 8|n!. Furthermore, $a_{n} \equiv 5^{n}+5(\bmod 8)$.

If $n$ is even, then $5^{n} \equiv 1(\bmod 8)$, so $a_{n} \equiv 6(\bmod 8)$. However, all perfect squares have remainders 0, 1, or 4 when divided by 8. Therefore, if $n \geq 4$ and $n$ is even, then $a_{n}$ is not a perfect square.

Let $n \geq 7$. Clearly, $7 \mid n!$. Then $a_{n} \equiv 5(\bmod 7)$. On the other hand, the remainders of perfect squares when divided by 7 are $0,1,2$ or 4. Therefore, $a_{n}$ is not a perfect square for $n \geq 7$. Thus, from the previous discussion, we need to check for $n=1, n=2, n=3$ and $n=5$.

If $n=5, a_{5} \equiv 2 \cdot 1^{5}+5 \equiv 2(\bmod 5)$.

Since the remainders of a perfect square when divided by 5 are 0, 1, or 4, $a_{5}$ is not a perfect square.

For $n=3$, we have $a_{3} \equiv 3(\bmod 7)$, so $a_{3}$ is not a perfect square.

For $n=2$, we have $a_{2} \equiv 1+5 \equiv 2(\bmod 4)$, so $a_{2}$ is not a perfect square.

For $n=1, a_{1}=(1+39-2) \cdot 1+17 \cdot 21+5=400$.

Therefore, only for $n=1, a_{n}$ is a perfect square.