Reputation: 2311
This is part of a bigger question. Its actually a mathematical problem. So it would be really great if someone can direct me to any algorithm to obtain the solution of this problem or a pseudo code will be of help.
The question. Given an equation check if it has an integral solution. For example:
(26a+5)/32=b
Here a
is an integer. Is there an algorithm to predict or find if b
can be an integer. I need a general solution not specific to this question. The equation can vary. Thanks
Upvotes: 1
Views: 2278
Reputation: 29734
Linear Diophantine equations take the form ax + by = c
. If c
is the greatest common divisor of a
and b
this means a=z'c
and b=z''c
then this is Bézout's identity of the form
with a=z'
and b=z''
and the equation has an infinite number of solutions. So instead of trial searching method you can check if c
is the greatest common divisor (GCD) of a
and b
If indeed a
and b
are multiples of c
then x
and y
can be computed using extended Euclidean algorithm which finds integers x
and y
(one of which is typically negative) that satisfy Bézout's identity
(as a side note: this holds also for any other Euclidean domain, i.e. polynomial ring & every Euclidean domain is unique factorization domain). You can use Iterative Method to find these solutions:
Integral solution to equation `a + bx = c + dy`
Upvotes: 3
Reputation: 17575
Your problem is an example of a linear Diophantine equation. About that, Wikipedia says:
This Diophantine equation [i.e., a x + b y = c] has a solution (where x and y are integers) if and only if c is a multiple of the greatest common divisor of a and b. Moreover, if (x, y) is a solution, then the other solutions have the form (x + k v, y - k u), where k is an arbitrary integer, and u and v are the quotients of a and b (respectively) by the greatest common divisor of a and b.
In this case, (26 a + 5)/32 = b is equivalent to 26 a - 32 b = -5. The gcd of the coefficients of the unknowns is gcd(26, -32) = 2. Since -5 is not a multiple of 2, there is no solution.
A general Diophantine equation is a polynomial in the unknowns, and can only be solved (if at all) by more complex methods. A web search might turn up specialized software for that problem.
Upvotes: 4