본문 바로가기
알고리즘/백준온라인저지 (Python)

[백준온라인저지] 1747번 - 소수&팰린드롬

by 미래문 2021. 8. 24.
반응형


 

 

https://www.acmicpc.net/problem/1747

 

1747번: 소수&팰린드롬

어떤 수와 그 수의 숫자 순서를 뒤집은 수가 일치하는 수를 팰린드롬이라 부른다. 예를 들어 79,197과 324,423 등이 팰린드롬 수이다. 어떤 수 N (1 ≤ N ≤ 1,000,000)이 주어졌을 때, N보다 크거나 같고,

www.acmicpc.net

 

import sys

n = int(sys.stdin.readline())

max = 1000001

sosu = [False, False] + [True] * (max-2)

for i in range(2, int(max ** 0.5) + 1):
    if sosu[i]:
        for j in range(i+i, max, i):
            if sosu[j]:
                sosu[j] = False

result = 0

for i in range(n, max):
    if str(i) == str(i)[::-1]:
        if sosu[i]:
            result = i
            break

    i += 1

if result == 0:
    result = 1003001

print(result)
반응형

댓글