Min operations to reduce N by multiplying by any number or taking square root | Java implementation of 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 ; Driver code ; Given N ; Function call
import java . lang . Math ; class GFG { static void minValue ( int n ) { while ( ( int ) Math . sqrt ( n ) == Math . sqrt ( n ) && n > 1 ) { n = ( int ) ( Math . sqrt ( n ) ) ; } for ( int i = ( int ) ( Math . sqrt ( n ) ) ; i > 1 ; i -- ) { while ( n % ( i * i ) == 0 ) n /= i ; } System . out . println ( n ) ; } public static void main ( String args [ ] ) { int N = 20 ; minValue ( N ) ; } }