정수 제곱근 판별

Mkim4·2023년 6월 19일
1

프로그래머스-정수 제곱근 판별(링크 바로가기)

(나의 풀이방법)

import math
def solution(n):
    float_a = math.sqrt(n) #11.0, 1.732...
    int_a = int(math.sqrt(n)) #11, 1
    
    if float_a - int_a == 0:
        answer = (int_a+1)**2
    else:
        answer = -1
    return answer

(다른 사람의 풀이방법)

def nextSqure(n):
    sqrt = n ** (1/2)

    if sqrt % 1 == 0:
        return (sqrt + 1) ** 2
    return 'no'

제곱근을 구하는 방식과 제곱근의 값이 정수인지 확인하는 방법이 매우 인상깊다.
세상에 천재는 많다. 그만큼 배울 사람이 많다는 뜻이겠지?

profile
귀요미 개발자

0개의 댓글