how to calculate an integer x let x^2+2mx-n is a square number?

Let m, n be positive integers.

Let $x^2+2mx-n=p^2$ for some p which is a positive integer.

How does one solve for x?

Sorry forgot this: x must be an integer.

-
Try completing the squares. – Gautam Shenoy Nov 14 '12 at 5:07
@GautamShenoy hi, sorry but what do you mean by completing the squares? – CHENZhao Nov 14 '12 at 5:34
Add and subtract $m^2$ to LHS. Then you will get $(x+m)^2 = p^2+n+m^2$. Now take square roots. Being a quadratic equation, it will have two roots. It will have a $-m\pm \sqrt{(.)}$ like form. – Gautam Shenoy Nov 14 '12 at 5:48
You could also have used the usual formula for quadratic equations. What I showed you was a faster way to arrive at it. – Gautam Shenoy Nov 14 '12 at 5:50
@GautamShenoy sorry but I mean we dont know what p is. – CHENZhao Nov 14 '12 at 6:03

We change notation slightly, to make it clear that what is called $p$ is variable.

Our equation can be rewritten as $$(x+m)^2-m^2-n=y^2,$$ and then as $$(x+m)^2-y^2=m^2+n,$$ or equivalently $$[(x+m)-y][(x+m)+y]=m^2+n.$$ If $m^2+n$ is of the form $4k+2$, there are no integer solutions. For suppose to the contrary that there is a solution $(x,y)$. Note that $(x+m)-y$ and $(x+m)+y$ differ by an even number $2y$. So they are both odd or both even. If they are both odd, then their product cannot have shape $4k+2$. And it cannot have shape $4k+2$ if both are even.

Now assume first that $m^2+n$ is odd. Let $s$ be any divisor of $m^2+n$, and let $t=(m^2+n)/s$. We can solve the system of linear equations $(x+m)-y=s$, $(x+m)+y=t$ for $x$ and $y$. In particular, we get $y=\frac{1}{2}(t-s)$. This is positive if, for example, we let $s$ be any positive divisor of $m^2+n$ which is less than $\sqrt{m^2+n}$. We also get positive $y$ if $s$ is a negative divisor of $m^2+n$ which is less than $-\sqrt{m^2+n}$.

If $m^2+n$ is a multiple of $4$, $s$ and $t$ must be chosen to be both even. Apart from that, the analysis is the same.

In both the case $m^2+n$ is even, and the case $m^2+n$ is divisible by $4$, all integer solutions of our Diophantine equation are obtained as described above from factors of $m^2+n$.

Remark: If we know the prime power factorization of $m^2+n$, we can find an expression for the number of solutions of our diophantine equation.

-
So there is no way to find x without factoring m^2+n? – CHENZhao Nov 14 '12 at 7:27
Well, if $m^2+n$ is odd, we can use the trivial factorization $m^2+n=st$ where $s=1$ and $t=m^2+n$ to get one solution. And if $m^2+n$ is divisible by $4$, we can use the semi-trivial factorization $m^2+n=st$ where $s=2$ and $t=(m^2+n)/2$. So when there is a solution, we can always find one easily. But getting *all the integer solutions is essentially equivalent to finding the prime power factorizations of $m^2+n$. – André Nicolas Nov 14 '12 at 7:35
Thank you very much, that helps a lot. – CHENZhao Nov 14 '12 at 8:30

Given positive integers $m,n$ you want positive integers $x,p$ such that $$x^2+2mx-n=p^2$$ Rewrite as $$x^2+2mx+m^2-p^2=n+m^2$$ which is $$(x+m+p)(x+m-p)=n+m^2$$ Now for each way of factoring $n+m^2$, you get possible solutions. If $n+m^2=de$, then you have $$x+m+p=d,\quad x+m-p=e$$ which gives you $$x=-m+(d+e)/2,\quad p=(d-e)/2$$ For integers, you have to have $d,e$ both even, or both odd; for positive integers, you need $d\gt e$.

-
So there is no way to find x without factoring m^2+n? – CHENZhao Nov 14 '12 at 7:26
Finding $x$ and factoring $m^2+n$ are equivalent --- if you can do either one, you can do the other at (essentially) no extra charge. – Gerry Myerson Nov 14 '12 at 10:51

If $m$, $n$, and $p$ are known values, then

$x^2+2mx-n=p^2$

$\Longrightarrow x^2 +2mx - (p^2+n)=0$.

This can be solved using the quadratic formula, giving

$x=\pm \sqrt{m^2+p^2+n}-m$ .

-
sorry but I mean we dont know what p is – CHENZhao Nov 14 '12 at 6:04
Judging by the solution, we need $m^2+p^2+n$ to be a perfect square. – Gautam Shenoy Nov 14 '12 at 6:37