#P1217. [USACO1.5] 回文质数 Prime Palindromes

[USACO1.5] 回文质数 Prime Palindromes

Description

Since 151151 is both a prime and a palindrome (it reads the same from left to right and from right to left), 151151 is a palindromic prime.

Write a program to find all palindromic primes in the range [a,b][a, b] with 5a<b100,000,0005 \le a < b \le 100,000,000 (one hundred million).

Input Format

The first line contains two positive integers aa and bb.

Output Format

Output a list of palindromic primes, one per line.

5 500

5
7
11
101
131
151
181
191
313
353
373
383

Hint

Hint 1: Generate palindromes and then check whether they are prime (also called “primes” or “sùshù” in pinyin).

Hint 2: Generate palindromes by combining digits properly. You might need several loops like the one below.

Problem translation from NOCOW.

USACO Training Section 1.5.

Generating palindromes of length 55:

for (d1 = 1; d1 <= 9; d1+=2) {    // 只有奇数才会是素数
     for (d2 = 0; d2 <= 9; d2++) {
         for (d3 = 0; d3 <= 9; d3++) {
           palindrome = 10000*d1 + 1000*d2 +100*d3 + 10*d2 + d1;//(处理回文数...)
         }
     }
 }

Translated by ChatGPT 5