Find two numbers with the given LCM and minimum possible difference | Java implementation of the approach ; Function to return the LCM of a and b ; Function to find and print the two numbers ; To find the factors ; To check if i is a factor of x and the minimum possible number satisfying the given conditions ; Driver code
class GFG { static int lcm ( int a , int b ) { return ( a / __gcd ( a , b ) * b ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void findNums ( int x ) { int ans = - 1 ; for ( int i = 1 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 && lcm ( i , x / i ) == x ) { ans = i ; } } System . out . print ( ans + " ▁ " + ( x / ans ) ) ; } public static void main ( String [ ] args ) { int x = 12 ; findNums ( x ) ; } }