Skip to content
Related Articles
Open in App
Not now

Related Articles

Linear Diophantine Equations

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 23 Aug, 2022
Improve Article
Save Article

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.

Examples:  

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.
Recommended Practice

Solution: 

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 the above approach.  

C++




// C++ program to check for solutions of diophantine
// equations
#include <bits/stdc++.h>
using namespace std;
 
//utility function to find the GCD of two numbers
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);
}
 
//driver function
int main()
{
    // First example
    int a = 3, b = 6, c = 9;
    isPossible(a, b, c)? cout << "Possible\n" :
                        cout << "Not Possible\n";
 
    // Second example
    a = 3, b = 6, c = 8;
    isPossible(a, b, c)? cout << "Possible\n" :
                       cout << "Not Possible\n";
 
    // Third example
    a = 2, b = 5, c = 1;
    isPossible(a, b, c)? cout << "Possible\n" :
                       cout << "Not Possible\n";
 
    return 0;
}


Java




// Java program to check for solutions of
// diophantine equations
import java.io.*;
 
class GFG {
     
    // Utility function to find the GCD
    // of two numbers
    static int gcd(int a, int b)
    {
        return (a % b == 0) ?
                Math.abs(b) : gcd(b,a%b);
    }
     
    // This function checks if integral
    // solutions are possible
    static boolean isPossible(int a,
                            int b, int c)
    {
        return (c % gcd(a, b) == 0);
    }
     
    // Driver function
    public static void main (String[] args)
    {
        // First example
        int a = 3, b = 6, c = 9;
        if(isPossible(a, b, c))
            System.out.println( "Possible" );
        else
            System.out.println( "Not Possible");
     
        // Second example
        a = 3; b = 6; c = 8;
        if(isPossible(a, b, c))
            System.out.println( "Possible") ;
        else
            System.out.println( "Not Possible");
     
        // Third example
        a = 2; b = 5; c = 1;
        if(isPossible(a, b, c))
            System.out.println( "Possible" );
        else
            System.out.println( "Not Possible");
    }
}
 
// This code is contributed by anuj_67.


Python3




# Python 3 program to check for solutions
# of diophantine equations
from math import gcd
 
# This function checks if integral
# solutions are possible
def isPossible(a, b, c):
    return (c % gcd(a, b) == 0)
 
# Driver Code
if __name__ == '__main__':
     
    # First example
    a = 3
    b = 6
    c = 9
    if (isPossible(a, b, c)):
        print("Possible")
    else:
        print("Not Possible")
 
    # Second example
    a = 3
    b = 6
    c = 8
    if (isPossible(a, b, c)):
        print("Possible")
    else:
        print("Not Possible")
 
    # Third example
    a = 2
    b = 5
    c = 1
    if (isPossible(a, b, c)):
        print("Possible")
    else:
        print("Not Possible")
         
# This code is contributed by
# Surendra_Gangwar


C#




// C# program to check for
// solutions of diophantine
// equations
using System;
 
class GFG
{
     
    // Utility function to find
    // the GCD of two numbers
    static int gcd(int a, int b)
    {
        return (a % b == 0) ?
                Math.Abs(b) :
               gcd(b, a % b);
    }
     
    // This function checks
    // if integral solutions
    // are possible
    static bool isPossible(int a,
                           int b,
                           int c)
    {
        return (c % gcd(a, b) == 0);
    }
     
    // Driver Code
    public static void Main ()
    {
        // First example
        int a = 3, b = 6, c = 9;
        if(isPossible(a, b, c))
            Console.WriteLine("Possible");
        else
            Console.WriteLine("Not Possible");
     
        // Second example
        a = 3; b = 6; c = 8;
        if(isPossible(a, b, c))
            Console.WriteLine("Possible") ;
        else
            Console.WriteLine("Not Possible");
     
        // Third example
        a = 2; b = 5; c = 1;
        if(isPossible(a, b, c))
            Console.WriteLine("Possible");
        else
            Console.WriteLine("Not Possible");
    }
}
 
// This code is contributed by anuj_67.


PHP




<?php
// PHP program to check for solutions of
// diophantine equations
 
// utility function to find the
// GCD of two numbers
function gcd($a, $b)
{
    return ($a % $b == 0) ?
                  abs($b) : gcd($b, $a % $b);
}
 
// This function checks if integral
// solutions are possible
function isPossible($a, $b, $c)
{
    return ($c % gcd($a, $b) == 0);
}
 
// Driver Code
 
// First example
$a = 3;
$b = 6;
$c = 9;
if(isPossible($a, $b, $c) == true)
    echo "Possible\n" ;
else
    echo "Not Possible\n";
 
// Second example
$a = 3;
$b = 6;
$c = 8;
 
if(isPossible($a, $b, $c) == true)
    echo "Possible\n" ;
else
    echo "Not Possible\n";
 
// Third example
$a = 2;
$b = 5;
$c = 1;
if(isPossible($a, $b, $c) == true)
    echo "Possible\n" ;
else
    echo "Not Possible\n";
 
// This code is contributed by ajit..
?>


Javascript




<script>
 
// Javascript program to check for solutions of
// diophantine equations
 
    // Utility function to find the GCD
    // of two numbers
    function gcd(a, b)
    {
        return (a % b == 0) ?
                Math.abs(b) : gcd(b,a%b);
    }
       
    // This function checks if integral
    // solutions are possible
    function isPossible(a,
                            b, c)
    {
        return (c % gcd(a, b) == 0);
    }
   
 
// Driver Code
 
        // First example
        let a = 3, b = 6, c = 9;
        if(isPossible(a, b, c))
           document.write( "Possible" + "<br/>" );
        else
           document.write( "Not Possible" + "<br/>" );
       
        // Second example
        a = 3; b = 6; c = 8;
        if(isPossible(a, b, c))
           document.write( "Possible" + "<br/>" ) ;
        else
           document.write( "Not Possible" + "<br/>" );
       
        // Third example
        a = 2; b = 5; c = 1;
        if(isPossible(a, b, c))
           document.write( "Possible" + "<br/>" );
        else
           document.write( "Not Possible" + "<br/>" );
                       
</script>


Output : 

Possible
Not Possible
Possible

Time Complexity: O(min(a,b))

Auxiliary Space: O(1)

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 review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!