본문 바로가기
알고리즘/Codewars

[Codewars] You're a square!

by 줍 2024. 6. 9.

Q. 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

 

S.  n의 제곱근이 정수인지 확인하면 된다고 생각해서 이렇게 풀이했는데 ..

isSquare(n) {
  return n < 0 ? false : pow(sqrt(n).toInt(), 2) == n ? true : false;
}

 

+

이렇게 간단하게 1로 나눴을때 나머지가 0이면 되는거였다!

isSquare(num n) => sqrt(n) % 1 == 0;

'알고리즘 > Codewars' 카테고리의 다른 글

[Codewars] Create Phone Number  (0) 2024.06.09
[Codewars] Find The Parity Outlier  (1) 2024.06.09
[Codewars] Stop gninnipS My sdroW!  (1) 2024.06.08
[Codewars] Multiples of 3 or 5  (0) 2024.06.06
[Codewars] Mumbling  (0) 2024.06.04