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.
Input : a = 3, b = 6, c = 9 Output: Possible Explanation : The Equation turns out to be, 3x + 6y = 9 one integral solution would be x = 1 , y = 1 Input : a = 3, b = 6, c = 8 Output : Not Possible Explanation : o integral values of x and y exists that can satisfy the equation 3x + 6y = 8 Input : a = 2, b = 5, c = 1 Output : Possible Explanation : Various integral solutions possible are, (-2,1) , (3,-1) etc.
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
Below is the implementation of above approach.
Possible Not Possible Possible
How does this work?
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.
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Find number of solutions of a linear equation of n variables
- Gaussian Elimination to Solve Linear Equations
- Using Chinese Remainder Theorem to Combine Modular equations
- Program to find root of an equations using secant method
- Solve the Linear Equation of Single Variable
- Number of solutions to Modular Equations
- Find n-variables from n sum equations with one missing
- System of Linear Equations in three variables using Cramer's Rule
- Find 'N' number of solutions with the given inequality equations
- Find the repeating and the missing number using two equations
- Program to implement Linear Extrapolation
- Find the values of X and Y in the Given Equations
- Find n positive integers that satisfy the given equations
- Count of integers in a range which have even number of odd digits and odd number of even digits