Min operations to reduce N by multiplying by any number or taking square root | Python3 program for the above approach ; Function to reduce N to its minimum possible value by the given operations ; Keep replacing n until is an integer ; Keep replacing n until n is divisible by i * i ; Print the answer ; Given N ; Function call
import math NEW_LINE def MinValue ( n ) : NEW_LINE INDENT while ( int ( math . sqrt ( n ) ) == math . sqrt ( n ) and n > 1 ) : NEW_LINE INDENT n = math . sqrt ( n ) NEW_LINE DEDENT for i in range ( int ( math . sqrt ( n ) ) , 1 , - 1 ) : NEW_LINE INDENT while ( n % ( i * i ) == 0 ) : NEW_LINE INDENT n /= i NEW_LINE DEDENT DEDENT print ( n ) NEW_LINE DEDENT n = 20 NEW_LINE MinValue ( n ) NEW_LINE