5. Let $p$ and $q$ respectively be the smallest and largest prime factors of $n$. Find all positive integers $n$ such that $p^{2}+q^{2}=n+9$.
First eliminate the special case where $n=p=q$, which leads to $2 p^{2}=p+9$. This has no integer solutions.
Rewrite the equation as $p^{2}+q^{2}-9=k p q$ for some positive integer $k$
Now, since $q \mid q^{2}$ and $q \mid k p q$, we have $q \mid p^{2}-9=(p-3)(p+3)$ and, since $q$ is prime, $q \mid p-3$ or $q \mid p+3$
Hence either $p=3$ or $q \mid p+3$
If $p=3$ then $n=q^{2}$ and $3 \mid n$ so $q=3$ and $n=9$, which works.
If $q \mid p+3$ and $p=2$ then $q=5$ and $n=20$, which works.
If $p>3$ then $q>\frac{p+3}{2}$ and so $q=p+3$, which is even. So this is impossible.
Hence only solutions are $n=9$ and $n=20$.
An alternative approach, using modular arithmetic, observes that $p^{2} \equiv 9(\bmod q)$
Since $q$ is prime, this implies that $p \equiv \pm 3(\bmod q)$ and then proceeds as above.