문제 설명

Untitled

문제 분석

Untitled

Untitled

<aside> 💡 참고로 1은 팰린드롬 수는 맞지만 소수가 아님!

</aside>

슈도코드

Untitled

구현

public class 소수_팰린드롬_1747 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int N = scan.nextInt();
        int[] arr = new int[10000001]; // 1,000,000보다 크거나 같은 값이기 때문에 0을 하나 더 써줌
        for (int i = 2; i < arr.length; i++) {
            arr[i] = i;
        }
        
        // 소수 구하기
        for (int i = 2; i < Math.sqrt(arr.length); i++) {
            if (arr[i] == 0) continue;

            for (int j = i + i; j < arr.length; j += i) { // 배수 지우기
                arr[j] = 0;
            }
        }

        // 입력 값부터 1씩 증가시키면서 소수와 팰린드롬 수가 맞는지 확인
        int i = N;
        while (true) {
            if (arr[i] != 0) {
                int value = arr[i];
                if (isPalindrome(value)) {
                    System.out.println(value);
                    break;
                }
            }
            i++;
        }
    }

    // 팰린드롬 수 판별
    private static boolean isPalindrome(int value) {
        char[] temp = String.valueOf(value).toCharArray();
        int s = 0;
        int e = temp.length - 1;
        while (s < e) {
            if (temp[s] != temp[e]) {
                return false;
            }
            s++;
            e--;
        }
        return true;
    }
}