어떤 자연수를 제곱했을 때 나오는 정수를 제곱수라고 합니다. 정수 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()
- Returns True.
- Exists for duck type compatibility with float.is_integer().
- float.is_interger()
- float 인스턴스가 정숫값을 가진 유한이면 True 를, 그렇지 않으면 False 를 돌려줍니다.
(-2.0).is_integer()
True
(3.2).is_integer()
False
- 참고자료: Python 공식문서 - 내장형
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로 구한 제곱근이 정수일 것입니다.
https://github.com/seonmin5/codingtest_Python
GitHub - seonmin5/codingtest_Python
Contribute to seonmin5/codingtest_Python development by creating an account on GitHub.
github.com
반응형
'Python' 카테고리의 다른 글
[Python/level 0] 원하는 문자열 찾기 - 181878, lower(), in, int (0) | 2024.08.17 |
---|---|
[Python] append() vs. extend() 공통점과 차이점 (0) | 2024.08.17 |
[Python/level 0] A 강조하기 - 181874, lower(), replace() (0) | 2024.08.17 |
[Python/level 0] 길이에 따른 연산 - 181879, prod() (0) | 2024.08.17 |
[Python/level 0] 대문자와 소문자 - 120893, swapcase() (0) | 2024.08.17 |