JiYoung Dev 🖥

[JAVA] 제곱근 구하기 (Math) 본문

Question

[JAVA] 제곱근 구하기 (Math)

Shinjio 2023. 8. 8. 08:38

Math.sqrt()

 

class Solution {
    public long solution(long n) {
        long answer = 0;
        
        double x = Math.sqrt(n);
        
        if(x * 10 % 10 == 0) {
        	answer = (long)((x+1) * (x+1));
        }else {
        	answer = -1;
        }
        
        return answer;
    }
}