# How to find integer solutions?

Following is the equation: Need to solve for integer values of x,y when a,b lies between -1000 and 1000. Problem is to find the number of integer values of pair (x,y) that satisfy it.

• Have you tried searching the documentation centre ? – Sektor Feb 15 '14 at 18:51
• Assuming a and b are integers then the solutions are (a+1,b+1) or (a-1,b-1) for any a,b. This can be seen by rearranging (x-a)(y-b)=1. The factors must either both be 1 or both be -1. Noting when a=b=1 only (2,2) applies and similarly a=b= -1 the (-2,-2). Mathematica is not required. – ubpdqn Feb 16 '14 at 10:32

Reduce works in general. First, multiply both sides of the equation by x y to simplify, and then:

Reduce[a b - b x - a y + x y == 1, {x, y, a, b}, Integers]
((C | C) ∈ Integers && x == C && y == C &&
a == -1 + C && b == -1 + C) ||
((C | C) ∈ Integers && x == C && y == C &&
a == 1 + C && b == 1 + C)


This answer says, in words, to pick any two integers C and C, set them equal to x and y. Corresponding values for a and b are then a=-1+x and b=-1+y. A second solution is given by a=1+x and b=1+y.

Accordingly, there are as many integer answers to this problem as there are integer pairs in the range you wish to consider.

For each pair (a,b) you can use Reduce,

a=1;b=10;
Reduce[(a/x - 1) (b/y - 1) == 1/(x y) , {x, y}, Integers]


Then, use ToRules to convert to proper solutions.

However, this is probably quite inefficient and may take some time for all combinations of $a,b$.

Problem is to find the number of integer values of pair (x,y) that satisfy it

Here is a way to proceed:

We'll use Tuples to generate the pairs (a, b) of Integers in the range we're interested. I'm just going to do this for the range [-2, 2]

Tuples[Range[-2, 2], 2]

{{-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2,
2}, {-1, -2}, {-1, -1}, {-1, 0}, {-1, 1}, {-1,
2}, {0, -2}, {0, -1}, {0, 0}, {0, 1}, {0, 2}, {1, -2}, {1, -1}, {1,
0}, {1, 1}, {1, 2}, {2, -2}, {2, -1}, {2, 0}, {2, 1}, {2, 2}}


Now we use FindInstance with the 4th argument (This tells Mathematica how many solutions you want found). Below I've requested a maximum of 3

 sol = FindInstance[(#1/x - 1) (#2/y - 1) == 1/(x y), {x, y}, Integers, 3] &
@@@ Tuples[Range[-2, 2], 2] You can then use Length to find how many solutions were found for each pair:

Length /@ sol


{2, 1, 2, 1, 2, 1, 1, 1, 0, 1, 2, 1, 2, 1, 2, 1, 0, 1, 1, 1, 2, 1, 2, 1, 2}