https://www.codewars.com/kata/56269eb78ad2e4ced1000013
Codewars - Achieve mastery through coding practice and developer mentorship
A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!
www.codewars.com
DESCRIPTION:
You might know some pretty large perfect squares. But what about the NEXT one?
Complete the findNextSquare method that finds the next integral perfect square after the one passed as a parameter. Recall that an integral perfect square is an integer n such that sqrt(n) is also an integer.
If the parameter is itself not a perfect square then -1 should be returned. You may assume the parameter is non-negative.
Examples:(Input --> Output)
121 --> 144
625 --> 676
114 --> -1 since 114 is not a perfect square
설명:
당신은 꽤 많은 완전 제곱수를 알고 있을지도 모릅니다. 하지만 다음과 같은 것은 어떤가요?
매개변수로 전달된 수의 다음 완전 제곱수를 찾는 findNextSquare 메서드를 완성하세요. 정수인 완전 제곱수는 정수 n이므로 sqrt(n)도 정수입니다.
매개변수 자신이 완전 제곱수가 아니라면 -1을 반환해야 합니다. 매개변수가 음수가 아니라고 가정할 수 있습니다.
예시:(입력 --> 출력)
121 --> 144
625 --> 676
114 --> -1 since 114 is not a perfect square
풀이
sq의 값의 제곱근이 정수이면 그 제곱근보다 하나 큰 값을 제곱해서 반환하고 정수가 아니라면 -1을 반환했다.
코드
function findNextSquare(sq) {
const num = Math.sqrt(sq);
if(Number.isInteger(num)) return (num + 1) ** 2;
return -1;
}
'알고리즘 > Codewars' 카테고리의 다른 글
[Codewars] Printer Errors (7 kyu) / JavaScript (0) | 2022.12.19 |
---|---|
[Codewars] Growth of a Population (7 kyu) / JavaScript (0) | 2022.12.17 |
[Codewars] Does my number look big in this? (6 kyu) / JavaScript (0) | 2022.12.15 |
[Codewars] Equal Sides Of An Array (6 kyu) / JavaScript (0) | 2022.12.14 |
[Codewars] Playing with digits (6 kyu) / JavaScript (0) | 2022.12.13 |
댓글