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++

`// 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 Eculcid'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; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `a = 25, b = 35; ` ` ` `findSmallest(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the smallest values of ` `// x and y that satisfy "ax - by = 0" ` `class` `GFG { ` ` ` ` ` `// To find GCD using Eculcid'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. ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find the ` `# smallest values of x and y that ` `# satisfy "ax - by = 0" ` ` ` `# To find GCD using Eculcid's algorithm ` `def` `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 code ` `a ` `=` `25` `b ` `=` `35` `findSmallest(a, b) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the smallest ` `// values of x and y that ` `// satisfy "ax - by = 0" ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// To find GCD using ` ` ` `// Eculcid'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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the ` `// smallest values of x ` `// and y that satisfy ` `// "ax - by = 0" ` ` ` `// To find GCD using ` `// Eculcid'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 ` ` ` `$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 ` `?> ` |

*chevron_right*

*filter_none*

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;

`// 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; ` `} ` |

*chevron_right*

*filter_none*

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 contribute@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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Find N values of X1, X2, ... Xn such that X1 < X2 < ... < XN and sin(X1) < sin(X2) < ... < sin(XN)
- Find smallest number K such that K % p = 0 and q % K = 0
- All possible values of floor(N/K) for all values of K
- Find smallest number n such that n XOR n+1 equals to given k.
- Find the smallest number X such that X! contains at least Y trailing zeros.
- Find Kth smallest value for b such that a + b = a | b
- Find K'th smallest number such that A + B = A | B
- Find smallest perfect square number A such that N + A is also a perfect square number
- Split the number into N parts such that difference between the smallest and the largest part is minimum
- Partition N into M parts such that difference between Max and Min part is smallest
- Number of values of b such that a = b + (a^b)
- Count of all possible values of X such that A % X = B
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Find minimum possible values of A, B and C when two of the (A + B), (A + C) and (B + C) are given
- Smallest number S such that N is a factor of S factorial or S!
- Smallest index such that there are no 0 or 1 to its right
- Smallest number k such that the product of digits of k is equal to n
- Smallest x such that 1*n, 2*n, ... x*n have all digits from 1 to 9