Find all primes $p$ and $q$ such that $p^2 + 7pq + q^2$ is a perfect square.

One obvious solution is $p = q$ and under such a situation all primes $p$ and $q$ will satisfy.

Further if $p \neq q$ then we can assume without the loss of generality that $p > q$. Assuming this and that there exists at least one such perfect square I have tried to show some contradiction modulo $4$ as any odd perfect square leaves a remainder of $1$ when divided by $4$, but it is not working. However I firmly believe that $p = q$ is the only solution, but I have failed to prove this.


6 Answers 6


If $p^2+q^2+7pq = r^2$ ($r$ being any integer), then $(p+q)^2 + 5pq = r^2$. So $5pq = r^2-(p+q)^2 = (r+p+q)(r-p-q)$.

Since $p, q$ and $5$ are all prime, it follows that one of the factors on the right-hand side is equal to one of them, and the other factor is the product of the other two. As clearly visible, $r+p+q$ is greater than any of the numbers $p, q$ and $5$. So it must be the product of two of those numbers (maybe three too), and the other factor $r-p-q$ must be equal to $p, q$ or $5$ (or $1$).

Now, different cases arise:

CASE $1$:

If $r-p-q = p$ then $r=2p+q$, and thus original equation becomes $p^2+q^2+7pq = (2p+q)^2$, which simplifies to $p=q$, Same if $r-p-q = q$.

CASE $2$: If $r-p-q = 5$ then $r = p+q+5$, and the equation $5pq = (r+p+q)(r-p-q)$ becomes $pq = 2(p+q)+5$. You can write this as $(p-2)(q-2)=9$, and the only solutions to this are $p=q=5$ and $p=3;q=11$ (or the other way round).

CASE $3$: $r+p+q$ might be equal to the product of all three numbers $5pq$, with $r-p-q = 1$. But then the equation becomes $2p+2q+1 = 5pq$, which is clearly impossible because the right-hand side is visibly greater than the left-hand side (Though there are solutions like $(1,1)$, but $1$ is not prime ).

So, to summarise the whole answer, We can say that only solutions are:



  • 4
    $\begingroup$ Fairly well, I consider it the best answer. +1 $\endgroup$ Dec 17, 2016 at 0:21

Assume $p^2+7pq+q^2=n^2$ with $p\ge q$. Note that $p^2+2pq+p^2=(p+q)^2$, hence $$(n+p+q)(n-p-q)=n^2-(p+q)^2=5pq$$ We know the prime factorization of $5pq$ and that $n-p-q<n+p+q$, hence conclude that $n-p-q$ is $\in\{1,5,q,p\text{ (if $5q>p$)},5q\text{ (if $5q<p$)}\}$. Inverstigate these cases one by one.

  • $\begingroup$ 1, 5 q and p work as well if $5q<p$ right? or am I missing something? $\endgroup$
    – Postskjerm
    Dec 15, 2016 at 16:54
  • 1
    $\begingroup$ @stenvikteam: that set contains four elements: $1, 5,$ and $q$; and either $p$ (if $5q > p$) or $5q$ (if $5q < p$). $p$ doesn't work if $5q < p$, because $(n+p+q)(n-p-q) = 5pq$, so $n-p-q < \sqrt{5pq} < p$. $\endgroup$
    – TonyK
    Dec 17, 2016 at 0:13

Since the equation $p^2+7pq+q^2=n^2$ is symmetric in $p$ and $q$, we may assume without loss of generality that $p\lt q$ (setting aside the obvious solutions with $p=q$). For reasons that will become clear in a moment, it's easy enough to check for solutions with $p\lt q\le7$ (there are none).

Multiplying both sides of the equation by $4$, it's possible to rewrite it as


Now the prime $q$ cannot divide both factors on the left hand side, since the sum of those two factors is congruent to $4p$ mod $q$. But $2p+7q-2n\lt9q\lt q^2$, so we must have $2p+7q+2n=aq^2$ and $2p+7q-2n=b$ with $ab=45$. Multiplying each of these by $a$ and summing (to eliminate $n$), we have




which solves to

$$q={7+2\sqrt{ap+1}\over a}$$

(We can disregard the negative square root, since $q\gt7$.) This implies $ap+1$ is a square, which clearly implies $ap+1=k^2$ for some integer $k\ge2$ (since $ap\ge2$). But now the inequality $q\gt p$ becomes $7+2k\gt k^2-1$, or $(k-1)^2\lt9$. Thus $k\lt4$, so there are just two possibilities: $k=2$ or $k=3$, correpsponding to $ap+1=4$ and $ap+1=9$. But since $p$ is primes and $a$ is a divisor of $45$ (hence odd), the only possibility is $p=3$ and $a=1$, which gives $q=7+2\sqrt4=11$. And indeed


And that's it: The only prime pairs $(p,q)$ for which $p^2+7pq+q^2$ is a square are $(3,11)$, $(11,3)$, and pairs of the form $(p,p)$.

Remark (added later, after a restless night's sleep): When I first wrote up this answer, I did the scratchwork to check that there are no solutions with $p\lt q\le7$. This did the job, to be sure, but it felt a little unsatisfying. It only later (in the middle of the night) occurred to me to think mod $8$: If $p\lt q$, then $q$, and hence $n$, are both odd, so that $q^2\equiv n^2\equiv1$ mod $8$, which reduces the equation to $p(p-q)\equiv0$ mod $8$. From this it's easy to see that $p\equiv q$ mod $8$, which implies $q\ge2+8=10$.

  • $\begingroup$ Incredibly thorough, and well written. +1! $\endgroup$ Dec 16, 2016 at 3:45

Let$p^2+7\times p\times q+q^2=m^2$

$\implies (p+q)^2+5\times p\times q=m^2$

$\implies 5\times p\times q=(m+p+q)(m-p-q)$.

Clearly $m+p+q>m-p-q$

$\implies m+p+q=1,5,p,q,5\times p,5\times q,p\times q,5\times p\times q$


& $m-p-q=5 \times p \times q,p \times q,5 \times p,5 \times q,p,q,5,1$


Observe $m+p+q=1,5,p,q$ are not possible as $p,q$ are at least 2 & $m+p+q=p,q\implies m+p=0,m+q=0$

From (1)&(2)

$m=\frac{5 \times p+q}{2},\frac{5 \times p\times q+1}{2}$ . Consider $m+p+q=5\times p$&$m-p-q=q$

$\implies p=q$

Similarly second simmatric eq leads same result.

Take $m+p+q=p \times q$ & $ m-p-q=5$.

$\implies 2(p+q)=p \times q-5

$\implies (p-2)(q-2)=9$

$\implies p=q=5 or (p,q)=(3,11),(11,3)$

So all solutions are$(p,q),(3,11),(11,3)$

  • $\begingroup$ This looks good except for a lack of spaces which reduces clarity. Also, it looks like in at least one instance you failed to properly close math mode. I'm not going to downvote for that, though, that would be petty. But please do go over it and make corrections. $\endgroup$
    – Mr. Brooks
    Dec 20, 2016 at 22:01

Of course $p=q$ works for any $p$. Suppose $p\lt q$. Then because $$ p^2+7pq+q^2=n^2 $$ we have $3p\lt n\lt3q$.

If $q\mid2p+7q-2n$ and $q\mid2p+7q+2n$, then $q\mid 4p$. Thus, $q$ can only divide one of them.

Since $$ (2p+7q-2n)(2p+7q+2n)=45q^2 $$ we have either $$ q^2\mid2p+7q-2n\quad\text{or}\quad q^2\mid2p+7q+2n $$ Thus, $q^2\le2p+7q-2n\lt9q$ or $q^2\le2p+7q+2n\lt15q$. In either case, $q\lt15$. Checking the $15$ cases where $1\lt p\lt q\lt15$ gives that the only solution is $(3,11)$.

  • $\begingroup$ You might take a look at the "mod 8" remark I just added to my answer. It implies you need only consider two possibilities: $(p,q)=(3,11)$ and $(5,13)$. $\endgroup$ Dec 16, 2016 at 20:53
  • $\begingroup$ That's true. Looking at more moduli gives more information. $p\equiv q\pmod8$ reduces the number of cases to check. $\endgroup$
    – robjohn
    Dec 16, 2016 at 21:11
  • $\begingroup$ In any event, your answer is a whole lot simpler than mine. Bravo! $\endgroup$ Dec 16, 2016 at 21:27

COMMENT.- Just for fun, prove easily the impossibility for twin numbers and other couples of primes. First, not possible for $p=2\lt q$. In fact $$q^2+14q+4=z^2\Rightarrow z^2\ge55\Rightarrow z\ge 8$$ Actually $\color{red}{z\ge 9}$ because $z$ must be odd. The discriminant of $q^2+14q+4-z^2=0$ is $ 45+z^2=t^2$ so making $t=z+h$ one has $45=2zh+h^2\ge18h+h^2$. It is clear how to finish. $$***$$ Now with the twin numbers $(p,q)=(p,p+2)$ where $p\ge3$. One has


Because of $p\ge3$ we have $z\ge 12$ so $\color{red}{z\ge 13}$ because $z$ should be odd.

The equation in $3p$ $$(3p)^2+6(3p)+4-z^2=0$$ has discriminant $$9-4+z^2=t^2\iff5+z^2=t^2$$ This is impossible because $z$ being greater or equal than $13$ the minimal difference $t^2-z^2$ must be greater or equal than $ 14^2-13^2=27.$ $$***$$ There are much primes of the form $(p,q)=(p,p+4)$, for example $(3,7),(7,11),(13,17)$,…… For these we have $$p^2+7p(p+4)+(p+4)^2=z^2=9p^2+36p+16=z^2\Rightarrow z^2\ge 205\Rightarrow \color{red}{z\ge 15}$$ The equation $$(3p)^2+12(3p)+16-z^2=0$$ has discriminant $36-16+z^2=t^2\iff 20=t^2-z^2$ but then we have $20\ge 16^2-15^2=31$, absurde.

Wants someone to try to see the case $(p,q)=(p+2h)\large ?$ This includes all the odd primes.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .