1

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

Abhiroop Sarkar
  • 2,251
  • 1
  • 26
  • 45
  • "The equation can vary" is a bit vague. Or are you asking for a general solution to problems like [Fermat's Last Theorem](http://en.wikipedia.org/wiki/Fermat's_Last_Theorem)? – nwellnhof Mar 15 '14 at 16:27
  • Only 2 variables, or can there be more? Any number or is there some upper limit? Similarly for the number terms. Can the equation contain logs, powers, variables in the denominator, whatever else one can put in an equation, or only some of these? – Bernhard Barker Mar 15 '14 at 16:28
  • I am dealing with only 2 variables exactly. – Abhiroop Sarkar Mar 15 '14 at 16:33

2 Answers2

4

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.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
3

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

enter image description here

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

enter image description here

(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`

Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118