Linear Diophantine Equations - GeeksforGeeks
A Diophantine equation is a polynomial equation, usually in two or more unknowns, such that only the integral solutions are required. An Integral solution is a solution such that all the unknown variables take only integer values.
Given three integers a, b, c representing a linear equation of the form : ax + by = c. Determine if the equation has a solution such that x and y are both integral values.
A Diophantine equation is a polynomial equation, usually in two or more unknowns, such that only the integral solutions are required. An Integral solution is a solution such that all the unknown variables take only integer values.
Given three integers a, b, c representing a linear equation of the form : ax + by = c. Determine if the equation has a solution such that x and y are both integral values.
For linear Diophantine equation equations, integral solutions exist if and only if, the GCD of coefficients of the two variables divides the constant term perfectly. In other words the integral solution exists if, GCD(a ,b) divides c.
Thus the algorithm to determine if an equation has integral solution is pretty straightforward.
- Find GCD of a and b
- Check if c % GCD(a ,b) ==0
- If yes then print Possible
- Else print Not Possible
int
gcd(
int
a,
int
b)
{
return
(a%b == 0)?
abs
(b) : gcd(b,a%b);
}
// This function checks if integral solutions are
// possible
bool
isPossible(
int
a,
int
b,
int
c)
{
return
(c%gcd(a,b) == 0);
}
Let GCD of ‘a’ and ‘b’ be ‘g’. g divides a and b. This implies g also divides (ax + by) (if x and y are integers). This implies gcd also divides ‘c’ using the relation that ax + by = c. Refer this wiki link for more details.
Read full article from Linear Diophantine Equations - GeeksforGeeks