제곱수 판별하기

송용진·2023년 8월 13일
0

알고리즘

목록 보기
15/173

어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다.
정수 n이 매개변수로 주어질 때,
n이 제곱수라면 1을
아니라면 2를 return하도록 solution 함수를 완성해주세요.

내 코드

import math
def solution(n):
    answer = 0
    print(math.sqrt(n))
    if math.sqrt(n) > 0 and math.sqrt(n) - int(math.sqrt(n)) == 0:
        answer = 1
    else:
        answer = 2
    return answer

예시 코드

def solution(n):
    return 1 if (n ** 0.5).is_integer() else 2
profile
백엔드 개발자

0개의 댓글