Minimum number of moves to reach N starting from ( 1 , 1 ) | Function to return the minimum number of moves required to reach the cell containing N starting from ( 1 , 1 ) ; To store the required answer ; For all possible values of divisors ; If i is a divisor of n ; Get the moves to reach n ; Return the required answer ; Driver code
function min_moves ( n ) { let ans = Number . MAX_VALUE ; for ( let i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { ans = Math . min ( ans , i + parseInt ( n / i , 10 ) - 2 ) ; } } return ans ; } let n = 10 ; document . write ( min_moves ( n ) ) ;