Minimum number of given operation required to convert n to m | Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code
< ? php function minOperations ( $ n , $ m ) { if ( $ m % $ n != 0 ) return -1 ; $ minOperations = 0 ; $ q = $ m / $ n ; while ( $ q % 2 == 0 ) { $ q = $ q / 2 ; $ minOperations ++ ; } while ( $ q % 3 == 0 ) { $ q = $ q / 3 ; $ minOperations ++ ; } if ( $ q == 1 ) return $ minOperations ; return -1 ; } $ n = 120 ; $ m = 51840 ; echo ( minOperations ( $ n , $ m ) ) ; ? >