Solving an equation over the integers

Consider the problem of finding all values of $$n \in \mathbb{N}$$ s.t. $$\sqrt{n} + \sqrt{n + 2005}$$

is an integer.

One can easily verify that $$n = 1,004,004$$ and $$39,204$$ satisfy this requirement, and in fact are the only such solution values for $$n$$. The simplest approach is to set the right hand side to some variable $$m \in \mathbb{Z}$$, square both sides, manipulate and note the prime factorization of $$2005$$... that is, $$2005 = 2005 \cdot 1 = 401 \cdot 5$$ and equate factors.

I would like to compute the solution directly, however.

The natural approaches do not work:

Solve[
Sqrt[n] + Sqrt[n + 2005] == m, {n, m}, Integers]

and

FindInstance[
Sqrt[n] + Sqrt[n + 2005] == m , {n, m}, Integers]

and

Assuming[{n, m} \[Element] Integers,
Solve[Sqrt[n] + Sqrt[n + 2005] == m, {n, m}]]

I've tried a number of minor variations, adding constraints such as $$\{ n, m \} > 0$$, and so forth. None work.

As yarchik shows (below), one can solve this with the result from number theory that each of the component square roots must resolve to an integer (demonstrated here). That leads to two questions:

• What would we do if we didn't know that fact from number theory?
• Supposing we do know that fact from number theory. How would we programmatically impose it as some form of constraint or subsidiary equation rather than "by hand"?

Suggestions?

• Sorry,Sqrt+Sqrt is not an integer. Something to adjust. Jul 26 '21 at 7:11
• @user64494: No no no. The factorization does not give the solutions directly. See here: youtube.com/watch?v=BGGL2IqmsLk Jul 26 '21 at 7:20

Use both Reduce and Solve .

result = Reduce[Sqrt[n] + Sqrt[n + 2005] == m, {m, n},Integers]
Solve[result, {m, n}]

{{m -> 401, n -> 39204}, {m -> 2005, n -> 1004004}}

• Oh my. Clever. Thanks so very much. ($\checkmark$) Jul 26 '21 at 14:38
Solve[p^2 == n && q^2 == n + 2005, {p, q, n}, Integers]

yields

{{p -> -1002, q -> -1003, n -> 1004004},
{p -> -1002, q -> 1003, n -> 1004004},
{p -> -198, q -> -203, n -> 39204},
{p -> -198, q -> 203, n -> 39204},
{p -> 198, q -> -203, n -> 39204},
{p -> 198, q -> 203, n -> 39204},
{p -> 1002, q -> -1003, n -> 1004004},
{p -> 1002, q -> 1003, n -> 1004004}}

It means basically 2 solutions for $$n$$, namely $$n=39204, 1004004$$.

• Yes. Thanks... there are a number of manipulations that lead to solvable equations. But why doesn't the direct method work? My whole goal is to eliminate the need for "human" calculation. Jul 26 '21 at 7:22
• @DavidG.Stork It does not work because it involves a non-trivial step not implemented in (known to) Mathematica, namely: a sum of square roots of integers is an integer number only when each one is a perfect square. Jul 26 '21 at 7:27
• Alas, just as I feared. I would have thought that this level of symbol manipulation would have been built into Mathematica. Might some clever use of Expand help? The original problem is fairly simple, and I mentioned "human" techniques to solve it. What if the the equation is a bit more complex and the transformation not as "obvious"? Or... can one incorporate that squared result as some symbolic constraint, rather than "by hand"? Jul 26 '21 at 7:35
• It is an interesting question how to teach MA that "a sum of square roots of integers is an integer number only when each one is a perfect square". Here is, by the way, a proof: math.stackexchange.com/a/279558/435814 . Maybe update the question that someone can provide more insight. Jul 26 '21 at 8:03
• Solving over PositiveIntegers gives a cleaner set of solutions. Jul 26 '21 at 9:38