## Tuesday, January 19, 2016

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

Find smallest values of x and y such that ax - by = 0 - GeeksforGeeks
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.

Simple Solution is to try every possible value of x and y starting from 1, 1 and stop when the equation is satisfied.
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.
`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"`
`void` `findSmallest(``int` `a, ``int` `b)`
`{`
`    ``// Find LCM`
`    ``int` `lcm = (a*b)/gcd(a, b);`

`    ``cout << ``"x = "` `<< lcm/a`
`         ``<< ``"\ny = "` `<< lcm/b;`
`}`
Read full article from Find smallest values of x and y such that ax - by = 0 - GeeksforGeeks