coding study/programmars
[level 0] 제곱수 판별하기
yeoonii
2024. 5. 27. 19:51
문제 설명
어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수 n이 매개변수로 주어질 때, n이 제곱수라면 1을 아니라면 2를 return하도록 solution 함수를 완성해주세요.
제한사항
- 1 ≤ n ≤ 1,000,000
import math
def solution(n):
ans = 0
if int(math.sqrt(n)) == math.sqrt(n):
ans = 1
else:
ans = 2
return ans
다른 사람의 풀이
def solution(n):
return 1 if (n ** 0.5).is_integer() else 2
"? 0.5승?" 했다가.. 생각해보니까
1/2승이 자연수면 당연히 제곱수임. 당연함..다음엔 저런식으로 생각해보도록 하는걸로!~