Find smallest values of x and y such that ax – by = 0
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++
#include <iostream>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return (gcd(b, a % b));
}
void findSmallest( int a, int b)
{
int lcm = (a * b) / gcd(a, b);
cout << "x = " << lcm / a
<< "\ny = " << lcm / b;
}
int main()
{
int a = 25, b = 35;
findSmallest(a, b);
return 0;
}
|
Java
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return (gcd(b, a % b));
}
static void findSmallest( int a, int b)
{
int lcm = (a * b) / gcd(a, b);
System.out.print( "x = " + lcm / a
+ "\ny = " + lcm / b);
}
public static void main(String[] args)
{
int a = 25 , b = 35 ;
findSmallest(a, b);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return (gcd(b, a % b))
def findSmallest(a, b):
lcm = (a * b) / gcd(a, b)
print ( "x =" , lcm / a, "\ny = " , lcm / b)
a = 25
b = 35
findSmallest(a, b)
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
return (gcd(b, a % b));
}
static void findSmallest( int a, int b)
{
int lcm = (a * b) / gcd(a, b);
Console.Write( "x = " + lcm / a + "\ny = " + lcm / b);
}
public static void Main()
{
int a = 25, b = 35;
findSmallest(a, b);
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return (gcd( $b , $a % $b ));
}
function findSmallest( $a , $b )
{
$lcm = ( $a * $b ) / gcd( $a , $b );
echo "x = " , $lcm / $a , "\ny = " , $lcm / $b ;
}
$a = 25;
$b = 35;
findSmallest( $a , $b );
?>
|
Javascript
<script>
function gcd(a, b)
{
if (b == 0)
return a;
return (gcd(b, a % b));
}
function findSmallest(a, b)
{
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++
void findSmallest( int a, int b)
{
int g = gcd(a, b);
cout << "x = " << b / g << endl
<< "y = " << a / g << endl;
}
|
C
void findSmallest( int a, int b)
{
int g = gcd(a, b);
cout << "x = " << b / g
<< "\ny = " << a / g;
}
|
Java
import java.util.*;
class GFG {
static int gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
static void findSmallest( int a, int b)
{
int g = gcd(a, b);
System.out.println( "x = " + b / g + "y = " + a / g);
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def findSmallest(a, b):
g = gcd(a, b)
print ( "x =" , int (b / g), "\ny =" , int (a / g))
findSmallest( 25 , 35 );
|
C#
using System;
class GFG{
static int gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
static void findSmallest( int a, int b)
{
int g = gcd(a, b);
Console.Write( "x = " + b / g
+ "y = " + a / g);
}
}
|
Javascript
function gcd(a, b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
function findSmallest(a, b)
{
let g = gcd(a, b);
console.log( "x =" , b / g, "\ny =" , a / g);
}
|
Last Updated :
03 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...