임의의 양의 정수 n에 대해, n이 어떤 양의 정수 x의 제곱인지 아닌지 판단하려 합니다.
n이 양의 정수 x의 제곱이라면 x+1의 제곱을 리턴하고, n이 양의 정수 x의 제곱이 아니라면 -1을 리턴하는 함수를 완성하세요.
| n | return |
|---|---|
| 121 | 144 |
| 3 | -1 |
121은 양의 정수 11의 제곱이므로, (11+1)를 제곱한 144를 리턴합니다.
3은 양의 정수의 제곱이 아니므로, -1을 리턴합니다.
class Solution {
public long solution(long n) {
long answer = 0;
Double x = Math.sqrt(n);
if ( x == x.intValue() ) {
answer = (long)Math.pow(x+1, 2);
} else {
answer = -1;
}
return answer;
}
}
// (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;
}
}
// (2)
class Solution {
public long solution(long n) {
double i = Math.sqrt(n);
return Math.floor(i) == i ? (long)Math.pow(i+1, 2) : -1;
}
}