This question already has an answer here:

I hope it's not inappropriate asking this here. I stumbled upon this site recently while researching a Project Euler problem, now I figure I'd use it to ask about a recurring theme in these problems: quadratic Diophantine equations.

I've recently boiled down another Project Euler problem (I won't say which, it should be unrecognizable from the original problem and should probably be kept that way) to the following Diophantine equation:


I've been trying to use http://www.alpertron.com.ar/METHODS.HTM as a reference, but I seem to get lost in a sea of constants. And the steps that program on the bottom of that page takes don't seem to match what he says to do. I'd rather be able to understand the steps I'm taking anyway rather than just copying a method.

I'm interested in all positive integer values of n and I'm more or less given a solution exists with n=2. How would I go about finding the rest of the solutions? And how would I solve these kinds of equations in general? If that last part is too complex a question to be handled here, is there any other resource that might help? As far as my current level of math, I have a degree in engineering (and helped a math major with some courses I never took myself) and I've already worked on Project Euler problems involving Pell's equations and continued fraction expansions of square roots.


marked as duplicate by Gottfried Helms, user370967, José Carlos Santos, user91500, Glorfindel Jul 15 '17 at 10:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Is there an answer below which satisfies you? If so, perhaps you might accept it, so that this question doesn't stay in the Unanswered category. If not, indicate what's missing so we can answer your question fully. $\endgroup$ – Kieren MacMillan Aug 23 '14 at 14:55
  • $\begingroup$ See comment immediately above this one. $\endgroup$ – Kieren MacMillan Nov 9 '15 at 2:12
  • $\begingroup$ math.stackexchange.com/questions/1776734/… $\endgroup$ – individ Jun 26 '16 at 17:26

Write the equation as $$ (2n)^2 + (n+1)^2 = y^2,$$ and then apply the [well-known] parameterization for Pythagorean triples.


Start by completing the square on the left: $$ 5\big( n+\tfrac15)^2 + \tfrac45 = y^2 $$ Multiply by 5 to clear denominators: $$ (5n+1)^2 + 4 = 5y^2. $$ Therefore you're looking for solutions to the Pell equation $$ x^2 - 5y^2 = -4 $$ that happen to satisfy $x\equiv1\pmod5$ (so that $n=(x-1)/5$).

Given that you know how to solve Pell's equation, you should be able to find a way to generate all integer solutions $(x_k,y_k)$; some of these values $x_k$ will be $1\pmod 5$, and the set of such $k$ should be an arithmetic progression.

  • $\begingroup$ I guess I should be more specific. I've had problems dealing with Pell's equations of the form x^2-ny^2=1. I've had to calculate solutions for such equations by checking the convergents of the square root of n. I've had other equations where I've had to do similar substitutions as the above, though I wasn't aware about the arithmetic progression thing and checked each answer to make sure it was still an integer after resubstition. I do not know why the convergents work and assume the answer would be complex. I also don't know how to generalize to solve x^2-ny^2=k. $\endgroup$ – Mike Oct 23 '11 at 10:29
  • 1
    $\begingroup$ There's one obvious solution $(a,b)$ to $x^2-5y^2=-4$. Prove that if $(x,y)$ is any solution then $(u,v)$ is another given by $(u+v\sqrt5)/2=((3+\sqrt5)/2)(x+y\sqrt5)/2$. It follows that $((3+\sqrt5)/2)^r(a+b\sqrt5)/2$ gives solutions for all $r$. If you choose $r$ cleverly, you'll even get $x\equiv1\pmod5$. If that's not enough, the libraries are full of intro number theory texts that discuss quadratic diophantine equations. $\endgroup$ – Gerry Myerson Oct 23 '11 at 11:47
  • $\begingroup$ How do you do mathematical notation here like exponents and radicals? Ah well, I was able to prove that statement. First I went to prove that if a+bz=c+dz, where a,b,c, and d are rational and z is irrational, then a=c and b=d. I rewrite the equation as a-c=(d-b)z. The left side is rational. The right size is rational if and only if d-b=0. So that step is proven. I cleared the parentheses in your equation to yield u=(3x+5y)/2 and v=(x+3y)/2. I then wrote u+v(root 5) in terms of x and y, then multiplied by the original equation you gave and simplified, giving u^2-5v^2=x^2-5y^2 $\endgroup$ – Mike Oct 23 '11 at 23:13
  • $\begingroup$ I guess I'll need to check the libraries for that and maybe something on game theory. In any case, I used u as x(n+1) and checked to see if that equation with (x0,y0)=(1,1) would generate all the solutions I needed. It appears the answers I needed were given for even values of r, so I redid the recurrences to take that into account. I was given what the 10th value should be for the problem, which matched the 10th iteration. So while I don't know how you arrived at that formula, it did generate all solutions I needed. Another problem solved. Thanks. $\endgroup$ – Mike Oct 24 '11 at 2:36
  • 1
    $\begingroup$ @Mike this post describes how to format mathematics on this site: meta.math.stackexchange.com/questions/107/… $\endgroup$ – Greg Martin Mar 25 '13 at 3:02

As it says above, you may manipulate the original problem into:

$$(n+1)^2 + (2n)^2 = (y)^2.$$

The sum of two squares equalling a square is a Pythagorean type equation, so parametrize as follows:

\begin{align} p^2 - q^2&=n+1 \tag{1} \\ 2pq&=2n \tag{2} \\ p^2 + q^2 &= y^2 \tag{3} \end{align}

Put this $n$ back into (1), we have

$$pq + 1 = p^2 - q^2 \implies 1 = p^2 - pq -q^2.$$

Completing the square of the first and second terms:

\begin{align} 1 = (p-q/2)^2 - q^2/4 - q^2 \\ &\implies 1 = (p-q/2)^2 - (5/4)q^2 \\ &\implies 4 = (2p - q)^2 - 5q^2 \end{align}

Let $r = 2p - q$, this implies that $p=\frac{r+q}{2}$, thus $n=pq=\frac{(r+q)q}{2}$, thus $y=\sqrt{\left(\frac{r+q}{2}\right)^2 + q^2}$. The equation above is now $$r^2 - 5q^2 = 4$$ which is a Pell type equation

It's fair I think to represent the solution of the problem in terms of the solutions of a Pell type equation, which are infinite in number:

In this way, the solution set is $$(n,y) = \left(\frac{(r+q)q}{2}, \sqrt{\left(\frac{r+q}{2}\right)^2 + q^2}\right)$$ such that $(r,q)$ belongs in the solution set of $r^2 - 5q^2 = 4$. Here are some examples:

\begin{array}{rcl} (r,q) &\to& (n,y^2) \\\hline (3,1) &\to& (2,5) \\ (7,3) &\to& (15,34) \\ (18,8)&\to& (104,233) \\ \end{array} and so on...


Not the answer you're looking for? Browse other questions tagged or ask your own question.