Minimum operations required to convert X to Y by multiplying X with the given co | Function to return the minimum operations required ; Not possible ; To store the greatest power of p that divides d ; While divible by p ; To store the greatest power of q that divides d ; While divible by q ; If d > 1 ; Since , d = p ^ a * q ^ b ; Driver code
function minOperations ( x , y , p , q ) { if ( y % x != 0 ) return - 1 var d = Math . floor ( y / x ) var a = 0 while ( d % p == 0 ) { d = Math . floor ( d / p ) a += 1 } var b = 0 while ( d % q == 0 ) { d = Math . floor ( d / q ) b += 1 } if ( d != 1 ) return - 1 return ( a + b ) } var x = 12 var y = 2592 var p = 2 var q = 3 document . write ( minOperations ( x , y , p , q ) )