본문 바로가기

Python

[Python/level 0] 제곱수 판별하기 - 120909, isqrt(), is_integer()

문제 설명

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

 

isqrt()

  • The math.isqrt() method rounds a square root number downwards to the nearest integer.
    제곱근을 가장 가까운 정수로 반올림합니다.
  • Note: The number must be greater than or equal to 0.
    참고로 숫자는 0보다 크거나 같아야 합니다.
  • math.isqrt(x)
Parameter Description
x Required. The number to round the square root of. If x is negative, it returns a ValueError. If x is not a number, it returns a TypeError

 

is_integer()

  • float.is_interger()
    • float 인스턴스가 정숫값을 가진 유한이면 True 를, 그렇지 않으면 False 를 돌려줍니다.
(-2.0).is_integer()
True
(3.2).is_integer()
False

 

code - 1차

import math
def solution(n):
    if n == (math.isqrt(n))**2:
        return 1
    else:
        return 2
  • if n == (math.isqrt(n))**2
    • n이 제곱수라면 제곱근**2의 값과 동일할 것입니다.

 

code - 2차

def solution(n):
    if (n**0.5).is_integer():
        return 1
    else:
        return 2
  • if (n**0.5).is_integer()
    • n이 제곱수라면 **0.5로 구한 제곱근이 정수일 것입니다.

 

반응형