CS/algorithm & data structure

[프로그래머스] k진수에서 소수 개수 구하기 (파이썬 풀이)

hjkim0502 2022. 7. 6. 16:31

https://school.programmers.co.kr/learn/courses/30/lessons/92335

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

* 내 풀이:

from math import sqrt
# 소수 체크
def check(num):
    if num == 1:
        return False
    for i in range(2, int(sqrt(num)) + 1):
        if num % i == 0:
            return False
    return True
# 10진수 -> n진수 변환
def change(num, n):
    ans = ''
    while num:
        num, rem = divmod(num, n)
        ans += str(rem)
    return ans[::-1]

def solution(n, k):
    ans = 0
    digits = change(n, k).split('0')
    for digit in digits:
        if digit and check(int(digit)):
            ans += 1
    return ans
  • 주어진 조건대로 n진수 변환 후 0에대해 split 한 뒤에 각 부분들을 10진수로 보았을 때 소수인지 확인
  • 10진수로 본다는 것을 10진수로 변환한다는 것으로 착각했었음