Minimum number operations required to convert n to m | Set | javascript implementation of the above approach ; Function to find the minimum number of steps ; If n exceeds M ; If N reaches the target ; The minimum of both the states will be the answer ; Driver code
var MAXN = 10000000 ; function minimumSteps ( n , m , a , b ) { if ( n > m ) return MAXN ; if ( n == m ) return 0 ; return Math . min ( 1 + minimumSteps ( n * a , m , a , b ) , 1 + minimumSteps ( n * b , m , a , b ) ) ; } var n = 120 , m = 51840 ; var a = 2 , b = 3 ; document . write ( minimumSteps ( n , m , a , b ) ) ;