Find the values of $p$ and $q$

$$p^3+p=q^2+q \implies p|q^2+q \implies p|q\ \mathrm{or}\ p|(q+1).$$

Obviously, $p\neq q$, so $p\nmid q$. Thus, we can set $q=kp-1$. This then reduces to

$$p^2-k^2p+(k+1)=0,$$

which has an integer solution iff $k^4-4k-4$ is a square. Can you see why this is not the case for large $k$, and determine the solutions from there?


Clearly $p\neq q$, and because $p$ and $q$ are prime and $$p(p^2+1)=p^3+p=q^2+q=q(q+1),$$ we must have $p\mid q+1$ and $q\mid p^2+1$. Write $$q+1=ap\qquad\text{ and }\qquad p^2+1=bq,$$ to find that $p^2-abp+b+1=0$. In particular $b+1\equiv0\pmod{p}$, say $b=cp-1$, but then $$p^2+1=(cp-1)q=(cp-1)(ap-1)=acp^2-(a+c)p+1.$$ Note that $a$, $b$ and $c$ are positive integers, and that $a>1$ as otherwise $p=q+1$ which implies that $(p,q)=(3,2)$ which is not a solution. The equation above simplifies to $$(ac-1)p=a+c,$$ and as $p\geq2$ clearly we cannot have $a,c\geq2$. Hence $c=1$ and so $$p^2+1=bq=(cp-1)q=(p-1)q.$$ In particular $p-1\mid p^2+1$. As $p-1\mid p^2-1$ it follows that $p-1=2$, so $p=3$ and hence $q=5$.


Hint: Since $p$ divides $p^3+p$, also $p$ divides $q(q+1)$, hence $p$ either divides $q$ or $p$ divides $q+1$, because $p$ is prime. Can you finish it?