Minimum repeated addition of even divisors of N required to convert N to M | INF is the maximum value which indicates Impossible state ; Recursive Function that considers all possible even divisors of cur ; Indicates impossible state ; Return 0 if cur == M ; Initially it is set to INF that means we can 't transform cur to M ; Loop to find even divisors of cur ; If i is divisor of cur ; If i is even ; Finding divisors recursively for cur + i ; Check another divisor ; Find recursively for cur + ( cur / i ) ; Return the answer ; Function that finds the minimum operation to reduce N to M ; INF indicates impossible state ; Driver Code ; Given N and M ; Function call
INF = int ( 1e7 ) ; NEW_LINE def min_op ( cur , M ) : NEW_LINE INDENT if ( cur > M ) : NEW_LINE INDENT return INF ; NEW_LINE DEDENT if ( cur == M ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT op = int ( INF ) ; NEW_LINE for i in range ( 2 , int ( cur ** 1 / 2 ) + 1 ) : NEW_LINE INDENT if ( cur % i == 0 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT op = min ( op , 1 + min_op ( cur + i , M ) ) ; NEW_LINE DEDENT if ( ( cur / i ) != i and ( cur / i ) % 2 == 0 ) : NEW_LINE INDENT op = min ( op , 1 + min_op ( cur + ( cur // i ) , M ) ) ; NEW_LINE DEDENT DEDENT DEDENT return op ; NEW_LINE DEDENT def min_operations ( N , M ) : NEW_LINE INDENT op = min_op ( N , M ) ; NEW_LINE if ( op >= INF ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( op ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 ; NEW_LINE M = 24 ; NEW_LINE min_operations ( N , M ) ; NEW_LINE DEDENT