Min operations to reduce N to 1 by multiplying by A or dividing by B | Function to check if it is possible to convert a number N to 1 by a minimum use of the two operations ; For the Case b % a != 0 ; Check if n equal to 1 ; Check if n is not divisible by b ; Initialize a variable c ; Loop until n is divisible by b ; Count number of divisions ; Loop until n is divisible by c ; Count number of operations ; Check if n is reduced to 1 ; Count steps ; Return the total number of steps ; Given n , a and b ; Function Call
def FindIfPossible ( n , a , b ) : NEW_LINE INDENT if ( b % a ) != 0 : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( n % b ) != 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return int ( n / b ) NEW_LINE DEDENT DEDENT c = b / a NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE while ( n % b == 0 ) : NEW_LINE INDENT n /= b NEW_LINE x += 1 NEW_LINE DEDENT while ( n % c == 0 ) : NEW_LINE INDENT n /= c NEW_LINE y += 1 NEW_LINE DEDENT if n == 1 : NEW_LINE INDENT total_steps = x + 2 * y NEW_LINE return total_steps NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT n = 48 NEW_LINE a = 3 NEW_LINE b = 12 NEW_LINE print ( FindIfPossible ( n , a , b ) ) NEW_LINE