Find smallest values of x and y such that ax - by = 0 - GeeksforGeeks

02 Mar, 2022
Given two values ‘a’ and ‘b’ that represent coefficients in “ax – by = 0”, find the smallest values of x and y that satisfy the equation. It may also be assumed that x > 0, y > 0, a > 0 and b > 0. 
Input: a = 25, b = 35
Output: x = 7, y = 5
 
A Simple Solution is to try every possible value of x and y starting from 1, 1 and stop when the equation is satisfied.A Direct Solution is to use Least Common Multiple (LCM). LCM of ‘a’ and ‘b’ represents the smallest value that can make both sides equal. We can find LCM using below formula.  
   LCM(a, b) = (a * b) / GCD(a, b) 
Greatest Common Divisor (GCD) can be computed using Euclid’s algorithm. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find the smallest values of x and y that// satisfy "ax - by = 0"#include <iostream>using namespace std; // To find GCD using Euclid's algorithmint gcd(int a, int b){    if (b == 0)        return a;    return (gcd(b, a % b));} // Prints smallest values of x and y that// satisfy "ax - by = 0"void findSmallest(int a, int b){    // Find LCM    int lcm = (a * b) / gcd(a, b);     cout << "x = " << lcm / a         << "\ny = " << lcm / b;} // Driver programint main(){    int a = 25, b = 35;    findSmallest(a, b);    return 0;}
// Java program to find the smallest values of// x and y that satisfy "ax - by = 0"class GFG {     // To find GCD using Euclid's algorithm    static int gcd(int a, int b)    {         if (b == 0)            return a;        return (gcd(b, a % b));    }     // Prints smallest values of x and y that    // satisfy "ax - by = 0"    static void findSmallest(int a, int b)    {         // Find LCM        int lcm = (a * b) / gcd(a, b);         System.out.print("x = " + lcm / a                         + "\ny = " + lcm / b);    }     // Driver code    public static void main(String[] args)    {        int a = 25, b = 35;        findSmallest(a, b);    }} // This code is contributed by Anant Agarwal.
# Python program to find the# smallest values of x and y that# satisfy "ax - by = 0" # To find GCD using Euclid's algorithmdef gcd(a, b):    if (b == 0):        return a    return(gcd(b, a % b)) # Prints smallest values of x and y that# satisfy "ax - by = 0"def findSmallest(a, b):     # Find LCM    lcm = (a * b)/gcd(a, b)    print("x =", lcm / a, "\ny = ", lcm / b) # Driver codea = 25b = 35findSmallest(a, b) # This code is contributed# by Anant Agarwal.
// C# program to find the smallest// values of x and y that// satisfy "ax - by = 0"using System; class GFG {     // To find GCD using    // Euclid's algorithm    static int gcd(int a, int b)    {         if (b == 0)            return a;        return (gcd(b, a % b));    }     // Prints smallest values of x and    // y that satisfy "ax - by = 0"    static void findSmallest(int a, int b)    {         // Find LCM        int lcm = (a * b) / gcd(a, b);         Console.Write("x = " + lcm / a + "\ny = " + lcm / b);    }     // Driver code    public static void Main()    {        int a = 25, b = 35;        findSmallest(a, b);    }} // This code is contributed by Sam007.
<?php// PHP program to find the// smallest values of x// and y that satisfy// "ax - by = 0" // To find GCD using// Euclid's algorithmfunction gcd($a, $b){    if ($b == 0)    return $a;    return(gcd($b, $a % $b));} // Prints smallest values// of x and y that// satisfy "ax - by = 0"function findSmallest($a, $b){         // Find LCM    $lcm = ($a * $b) / gcd($a, $b);     echo "x = ", $lcm/$a, "\ny = ", $lcm/$b;}     // Driver Code    $a = 25;    $b = 35;    findSmallest($a, $b);     // This code is contributed by ajit?>
<script>     // Javascript program to find the smallest    // values of x and y that    // satisfy "ax - by = 0"         // To find GCD using    // Euclid's algorithm    function gcd(a, b)    {           if (b == 0)            return a;        return (gcd(b, a % b));    }       // Prints smallest values of x and    // y that satisfy "ax - by = 0"    function findSmallest(a, b)    {           // Find LCM        let lcm = parseInt((a * b) / gcd(a, b), 10);           document.write("x = " + parseInt(lcm / a, 10) +        "</br>y = " + parseInt(lcm / b, 10));    }         let a = 25, b = 35;      findSmallest(a, b);     </script>
Output:
x = 7
y = 5
The above code for findSmallest() can be reduced:  
Since ax - by = 0,
ax = by, which means x/y = b/a
So we can calculate gcd and directly do as -

Value of x = b / gcd;
Value of y = a / gcd; 
 
C
// Prints smallest values of x and y that// satisfy "ax - by = 0"void findSmallest(int a, int b){    // Find GCD    int g = gcd(a, b);     cout << "x = " << b / g         << "\ny = " << a / g;}
This article is contributed by Aakash Sachdeva. 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 you want to share more information about the topic discussed above 
Sam007
jit_t
rameshtravel07
simmytarika5
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Modular multiplicative inverse
Fizz Buzz Implementation
Generate all permutation of a set in Python
How to check if a given point lies inside or outside a polygon?
Check if a number is Palindrome
Segment Tree | Set 1 (Sum of given range)
Merge two sorted arrays with O(1) extra space
Program to multiply two matrices
Singular Value Decomposition (SVD)