๋ฌธ์
ํ์ด
# ํ๋ก๊ทธ๋๋จธ์ค 2๋จ๊ณ - k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ
import math
def isPrime(n):
if n == 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
def k_digit(n, k):
rev_base = ''
while n > 0:
n, mod = divmod(n,k)
rev_base += str(mod)
return rev_base[::-1]
def solution(n, k):
answer = 0
k_digit_number = k_digit(n,k)
n_list = k_digit_number.split('0')
for i in n_list:
if i != "":
if isPrime(int(i)):
answer += 1
return answer
print(solution(437674, 3))
n์ k์ง์๋ก ๋ฐ๊พธ๋ ํจ์๋ฅผ k_digit ์ด๋ผ๊ณ ๋ง๋ค๊ณ
n์ด ์์์ธ์ง ํ๋ณํ๋ ํจ์๋ฅผ isPrime์ ์ ์ธํ์๋ค.
'๐๏ธ Algorithm > โฌ ํ๋ก๊ทธ๋๋จธ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
โฌ [Programmers] [Python] [Level1] ์ถ์ต ์ ์ (0) | 2023.04.13 |
---|---|
โฌ [Programmers] [Python] [Level3] ์ผ๊ทผ ์ง์ (0) | 2023.04.12 |
โฌ [Programmers] [Python] [Level2] ์ ํ๋ฒํธ ๋ชฉ๋ก (0) | 2023.04.11 |
โฌ [Programmers] [Python] [Level3] ์ด์ค ์ฐ์ ์์ ํ (0) | 2023.04.11 |
โฌ [Programmers] [Python] [Level1] ์์ ์ฐพ๊ธฐ (0) | 2023.04.10 |