This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-08-21] sumin #137 #149
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
풀이시간: 20분 | ||
|
||
<input> | ||
- 1 ≤ n ≤ 1,000,000 | ||
- 3 ≤ k ≤ 10 | ||
|
||
<solution> | ||
1) n을 k진수로 변환 | ||
2) 소수 판별 | ||
|
||
<시간복잡도> | ||
O(log_k*n * √n) | ||
""" | ||
# n을 k진수로 변환하는 함수 | ||
def convert_to_base(n: int, base: int) -> str: | ||
result = '' # k진수로 변환 후 값 | ||
while n: | ||
n, remainder = divmod(n, base) | ||
result += str(remainder) | ||
return result[::-1] | ||
|
||
# 소수 판별 | ||
def is_prime(number: int) -> bool: | ||
if number <= 1: | ||
return False | ||
i = 2 | ||
while i * i <= number: | ||
if number % i == 0: | ||
return False | ||
i += 1 | ||
return True | ||
|
||
|
||
def solution(n: int, k: int) -> int: | ||
converted_num = convert_to_base(n, k) # n을 k진수로 변환 | ||
cnt = 0 # 조건에 맞는 소수의 개수 | ||
# 변환된 수에 대한 조건으로 0이 양쪽 or 오른쪽 or 왼쪽 or 아예 안 붙는 경우에 해당하는 지 확인해야 하기 때문에 0을 기준으로 나눠줌 | ||
# -> 0을 기준으로 나눠지는 수는 해당 조건들 중 어느 하나라도 만족함 | ||
for segment in converted_num.split('0'): | ||
if not segment: # 빈 문자열 | ||
continue | ||
if is_prime(int(segment)): # 소수 | ||
cnt += 1 | ||
return cnt | ||
|
||
# 테스트 케이스 | ||
n1, k1 = 437674, 3 | ||
print(solution(n1, k1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
역시 빈문자열을 제외할 방법이 condition문을 제외하고는 어렵네요 ㅠ