# Selecting two numbers to make an integer value

Suppose that the general equation $f(x,y) = x + y + Sqrt[x*y]$ is to be used to find the values of $x$ and $y$ such that $f(x,y)$ is an integer, ideally using IntegerQ[n + m + Sqrt[n*m]].

The question is: What is an efficient method to select integers $n$ and $m$ to produce values of IntegerQ[n + m + Sqrt[n*m]] ?

Known examples: $(n,m) \in \{(0,1), (1,1), (1,4), (5,20), \cdots \}$

• You could consider checking first if the product of your two integers is a square before doing anything else. – J. M. is away Oct 13 '16 at 3:56

Finding distinct integers that satisfy function is equivalent to finding integers such that $x \times y = n^2$ for some integer $n$ In the following I only find $(m,n): m<n$ but obviously $(n,m)$ also satisfies. For "small numbers":

func[n_] := Module[{su = Subsets[Range[n], {2}], d},
d = Mod[Length@*Divisors /@ Times @@@ su, 2];
Pick[su, d, 1]]


Visualizing real solutions:

pf[n_] :=
Plot3D[x + y + Sqrt[x y], {x, 0, n}, {y, 0, n },
MeshFunctions -> {#3 &}, Mesh -> {Range[3 n]}, MeshStyle -> Red,
PlotStyle -> White]
Row[{pf[10], pf[30]}]


And showing integer pairs with real solutions:

pfm[n_, s_] :=
Plot3D[x + y + Sqrt[x y], {x, 0, n}, {y, 0, n },
MeshFunctions -> {#3 &}, Mesh -> {Range[1, 3 n, s]},
MeshStyle -> Red, PlotStyle -> White]
f[x_, y_] := x + y + Sqrt[x y];
pts[n_] :=
Graphics3D[{PointSize[0.02], Point[{##, f[##]} & @@@ func[n]]}]
Show[pfm[30, 1], pts[30]]


enter image description here

f[x_, y_] := x + y + Sqrt[x*y]

max = 500;

(soln1 =
Select[
Table[{x, y}, {x, 0, max}, {y, 0, max}] //
Flatten[#, 1] &,
IntegerQ[Sqrt[Times @@ #]] &];) //
RepeatedTiming

*  {2.45, Null}  *)

Length[soln1]

(*  2959  *)


Verifying that when f is applied to each pair that the result is an integer

And @@ IntegerQ@*f @@@ soln1

(*  True  *)


Second method

(soln2 =
Table[
If[IntegerQ[Sqrt[x*y]], {x, y}, Nothing], {x, 0, max}, {y, 0, max}] //
Flatten[#, 1] &;) //
RepeatedTiming

(*  {2.35, Null}  *)


Verifying that the two results are identical

soln1 === soln2

(*  True  *)


The n (2959) results can be reduced to (n+1)/2 (1480) unique results since x and y are interchangeable.