본문 바로가기
코테연습

5.You're a square!

by hxunz 2022. 3. 14.

A square of squares

You like building blocks. You especially like building blocks that are squares. And what you even like more, is to arrange them into a square of square building blocks!

However, sometimes, you can't arrange them into a square. Instead, you end up with an ordinary rectangle! Those blasted things! If you just had a way to know, whether you're currently working in vain… Wait! That's it! You just have to check if your number of building blocks is a perfect square.

Task

Given an integral number, determine if it's a square number:

In mathematics, a square number or perfect square is an integer that is the square of an integer; in other words, it is the product of some integer with itself.

The tests will always use some integral number, so don't worry about that in dynamic typed languages.

Examples

-1  =>  false
 0  =>  true
 3  =>  false
 4  =>  true
25  =>  true
26  =>  false

 

 

var isSquare = function(n){
  const sqrt = Math.sqrt(n);
  const result = Number.isInteger(sqrt);
  
  return result;
}

Math.sqrt() 를 써서 주어진 숫자에 대한 제곱근을 구했다...

그리고 그 제곱근이 정수인지 아닌지 확인하려고 Number.isInteger()를 했다..

 

제출하고 나서 다른 사람이 푼거 봤는데 완전 간단하게 했드라 ㅋ 

 

function isSquare(n) {
  return Math.sqrt(n) % 1 === 0;
}

제곱근한걸 1로 나눴을때 나머지가 0이면 true 0이 아니면 false

'코테연습' 카테고리의 다른 글

7.List Filtering  (0) 2022.03.17
6.Isograms  (0) 2022.03.16
4.Get the Middle Character  (0) 2022.03.11
3.Descending Order  (0) 2022.03.10
2. Disemvowel Trolls  (0) 2022.03.09

댓글