[JAVA 문제 풀이] 227. 정수의 제곱근 판별

프로그래머스 (12934)
Stupefyee's avatar
Apr 28, 2025
[JAVA 문제 풀이] 227. 정수의 제곱근 판별
notion image
 

내가 작성한 코드

class Solution { public long solution(long n) { long answer = 1; while (answer <= Math.sqrt(n)) { if(Math.pow(answer, 2) == n) { answer++; return answer * answer; } answer++; } return -1; } }
 

다른 사람의 코드

class Solution { public long solution(long n) { if (Math.pow((int)Math.sqrt(n), 2) == n) { return (long) Math.pow(Math.sqrt(n) + 1, 2); } return -1; } }
 
Share article

stupefyee