Minimize cost to make X and Y equal by given increments | Function to find gcd of x and y ; Function to find lcm of x and y ; Function to find minimum Cost ; Subtracted initial cost of x ; Subtracted initial cost of y ; Driver Code ; Returns the minimum cost required
function gcd ( x , y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } function lcm ( x , y ) { return ( x * y ) / gcd ( x , y ) ; } function minimumCost ( x , y ) { var lcm_ = lcm ( x , y ) ; var costx = ( lcm_ - x ) / x ; var costy = ( lcm_ - y ) / y ; return costx + costy ; } var x = 5 , y = 17 ; document . write ( minimumCost ( x , y ) + " " ) ;