human
Minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number | Function to find the minimum number of moves to make N and M equal ..
Array to maintain the numbers included ..
Pair of vertex , count.
Run bfs from N.
If we reached goal.
Iterate in the range.
If i is a factor of aux.
If i is less than M - aux and is not included earlier ..
If aux / i is less than M - aux and is not included earlier ..
Not possible.
Driver code"
How can the above be solved in Python?
assistant
#Here is code to minimum number of moves to make M and N equal by repeatedly adding any divisor of number to itself except 1 and the number and the other steps required.
 def countOperations ( N , M ) :
	visited = [ False ] * ( 100001 )
	Q = [ ]
	Q . append ( [ N , 0 ] )
	visited [ N ] = True
	while ( len ( Q ) > 0 ) :
		aux = Q [ 0 ] [ 0 ]
		cont = Q [ 0 ] [ 1 ]
		Q . pop ( 0 )
		if ( aux == M ) :
			return cont
		i = 2
		while i * i <= aux :
			if ( aux % i == 0 ) :
				if ( aux + i <= M and not visited [ aux + i ] ) :
					Q . append ( [ aux + i , cont + 1 ] )
					visited [ aux + i ] = True
				if ( aux + int ( aux / i ) <= M and not visited [ aux + int ( aux / i ) ] ) :
					Q . append ( [ aux + int ( aux / i ) , cont + 1 ] )
					visited [ aux + int ( aux / i ) ] = True
			i += 1
	return - 1
N , M = 4 , 24
print ( countOperations ( N , M ) )