
내가 작성한 코드
class Solution {
public int solution(int n) {
// 반복문 최대값 n만큼 지정 >> n만큼 안돔
for(int i = 1; i <= n; i++) {
// i * i 가 n보다 크면 바로 중단
if(i * i == n) {
return 1;
}
if(i * i > n) {
return 2;
}
}
return 2;
}
}
다른 사람의 코드
class Solution {
public int solution(int n) {
if (n % Math.sqrt(n) == 0) {
return 1;
} else {
return 2;
}
}
}
- Math.sqrt(n)
- 숫자
n
의 제곱근을 계산하는 메서드 - 제곱이 아닌 수를 넣으면 근사치를 반환 Ex. Math.sqrt(21) >> 4.58257569495584
Share article