https://www.acmicpc.net/problem/2023
import math
from collections import deque
def is_prime(num):
for i in range(2, int(math.sqrt(num))+1):
if num % i == 0:
return False
return True
def main():
n = int(input())
max_num = 8 * int(math.pow(10, (n - 1)))
ans = []
for i in [2, 3, 5, 7]:
q = deque([])
q.append(i)
max_len = int(max_num / 8)
while q:
tmp = q.popleft()
if is_prime(tmp):
if not (tmp // max_len):
for j in [1, 3, 7, 9]:
q.append((tmp * 10) + j)
else:
ans.append(tmp)
for i in ans:
print(i)
main()
prime = [True for _ in range(max_num)]
prime[0:2] = (False, False)
for i in range(int(math.sqrt(max_num)) + 1):
if prime[i]:
j = i * i
while j < max_num:
prime[j] = False
j += i
그래서 첫번째 방법인 2 ~ 로 나눠보는 방식으로 소수를 판단하였다.