I was solving problem 137 of Project Euler, which led me to find $n$ such that $5n^2+2n+1$ is a perfect square. But such numbers are very rare (the 13th is around 3 billions) so after decomposing into $(n+1)^2 + (2n)^2 = m^2$ and looking for Pythagorean triples and their $(a^2-b^2,2ab,a^2+b^2)$ generation, I ended up having to look for $k$ such that $5k^2+4$ is a perfect square.

This is a much easier task, which retrospectively makes sense since every $k$ will lead to some $n=O(k^2)$, so you only need to iterate to $10^5$ to find the $13$th number.

So we proved that there are more squares in $5n^2+4$ than in $5n^2+2n+1$.
Was there an easier way to spot this without pulling out the Pythagorean triple trick? Is there an intuitive reason or more generic underlying principle solely by looking at the equations?

  • 1
    $\begingroup$ Because it is obvious the $9$ with $5n^2+4$ $\endgroup$
    – Piquito
    Apr 5, 2020 at 23:50
  • 1
    $\begingroup$ And it is not hard at all the $25$ with the second form. $\endgroup$
    – Piquito
    Apr 5, 2020 at 23:52
  • 2
    $\begingroup$ I don't know if it helps, but $5n^2+4$ is a square for every second Fibonacci number ($n=1,3,8,21$ etc). $\endgroup$
    – TonyK
    Apr 6, 2020 at 9:00

3 Answers 3


I should start by clarifying that both equations yield the same number of squares; both yield countably infinitely many perfect squares. Up to any given upper bound, however, the former equation produces roughly twice as many perfect squares as the latter.

Solving $5n^2+4=m^2$ over the integers is equivalent to solving the equation $$m^2-5n^2=4,$$ Similarly, solving $5n^2+2n+1=m^2$ over the integers is equivalent to solving the equation $$(5n+1)^2-5m^2=-4.$$ Intuitively the two equations $$x^2-5y^2=4\qquad\text{ and }\qquad x^2-5y^2=-4,$$ should have roughly the same number of solutions (up to any given upper bound), and indeed their solution sets are in bijective correspondence through the maps $$(x,y)\ \rightarrow\ \big(\tfrac{x+5y}{2},\tfrac{x+y}{2}\big) \qquad\text{ and }\qquad \big(\tfrac{-x+5y}{2},\tfrac{x-y}{2}\big)\ \leftarrow\ (x,y).$$ But for the second equation, you only get valid solutions to the original equation when $x\equiv1\pmod{5}$. So you might expect the first equation to have about five times as many solutions up to any given upper bound. A more careful analysis shows that you only get about twice as many solutions, though.

This answer comes from the standard method of solving the Pell equation $$x^2+Dy^2=C,$$ with parameters $D$ and $C$, where $D$ is a squarefree integer. It shows that all solutions to $$5n^2+4=m^2,$$ are parametrized by $$m_k+n_k\sqrt{5}=\pm2\left(\frac{3+\sqrt{5}}{2}\right)^k,$$ and similarly that all solutions to $$5n^2+2n+1=m^2,$$ are parametrized by $$n_k+m_k\sqrt{5}=\pm(1+\sqrt{5})\left(\frac{7+3\sqrt{5}}{2}\right)^k.$$ In particular the solution sets to both equations are exponential families, with growth factors $$\frac{3+\sqrt{5}}{2}\qquad\text{ and }\qquad \frac{7+3\sqrt{5}}{2}=\left(\frac{3+\sqrt{5}}{2}\right)^2,$$ so the former has about twice as many solutions as the latter, up to any given upper bound.

  • $\begingroup$ thanks; very useful. Based on what you said I read about the Pell equation on wikipedia and it looks like the growth rate is purely based on the "norm" of the fundamental solution. So maybe the observation that one equation "should lead to 5 times more solutions" can apply to finding a smaller fundamental solution $\endgroup$
    – Thomas
    Apr 6, 2020 at 5:50

You can think of it in terns of terminal-digit patterns in base 5. For the terminal digits of a base-5 representation to be consistent with a square either the last digit must be 1 or 4, or else the last nonzero digit is 1 or 4 and then come an even number of terminal zeroes. Only five-twelfths of all terminal-digit patterns in base 5 are consistent with a square.

The expression $5n^2+4$ will always give the last digit $4$. By the first criterion above you always get a set of terminal digits corresponding to some squares, thus improving your chances of hitting on an actual integer square more than two times versus a "random" pattern. With $5n^2+2n+1$ you get some terminal-digit patterns that correspond to squares (if the base-5 representation of $n$ ends with $0$ then the base-5 representation of $5n^2+2n+1$ ends in $1$, and we're good) but other times you miss (if the base-5 representation of $n$ ends with $1$ then the base-5 representation of $5n^2+2n+1$ ends in $3$, which won't work).

  • 2
    $\begingroup$ I detectca downvote. I do not detect a reason why/how this answer can be improved. It's comical, in a way. $\endgroup$ Apr 6, 2020 at 1:22
  • 1
    $\begingroup$ often the downvote is abused simply because members don't have to give any reason or their name. anonymity is the cause. $\endgroup$
    – user25406
    Apr 6, 2020 at 14:41
  • 1
    $\begingroup$ not me, I actually upvoted $\endgroup$
    – Thomas
    Apr 6, 2020 at 14:59
  • $\begingroup$ @thomas we have the SE Correlation: the propensity to explain how an answer can be improved varies inversely with the propensith to cast downvotes. That's what's cominal. $\endgroup$ Apr 6, 2020 at 15:11

It proves that the problem are related, I won't conclude that the latter has more squares

$5*n^2+2*n+1$ compare with $5*n^2+4$

By inspection if $n = 1$, $3$ it makes $5*n^2+4$ a perfect square, but this doesn't mean it has more square...... What about other greater number

Since $5*n^2+4$ came up will trying to tackle $5*n^2+2*n+1$, I conclude it has lesser squares instead

Check the number $5*n^2+4$ well

also trying do this $5*n^2+2*n+1 = (n+m)^2$

$5*n^2+2*n+1 = n^2+2*n*m+m^2$

$4*n^2+2*n*(1-m) = m^2-1$

$(2*n)^2+2*n*(1-m)+1 = m^2$

Therefore $m > 2*n$ and $m > w$

Say $2*n*(1-m)+1 = w^2$

$2*n-2*n*m+1 = w^2$

Now say $m = w + 2*n +x $ where $x \in Z$

I hope you understand what am trying to do, it's going to be messy


You must log in to answer this question.

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