TouchStone
  Please Login
Login Sign Up
 Homepage  Problem Set  Examinations  Submissions  Discussions  Statistics
  • Home
  • Problem Set
  • P1029
  • Problem
  • P1029回文质数
    Limits : Time Limit : 1000 MS   Memory Limit : 65536 KB
    Description

    因为151既是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 是回文质数。

    写一个程序来找出范围[a,b](5 <= a < b <= 100,000,000)( 一亿)间的所有回文质数;

    Input Format

    一 行: 二个整数 a 和 b .

    Output Format

    输出一个回文质数的列表,一行一个。

    Sample Input

    5 500

    Sample Output

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

    Hint

    Hint 1: Generate the palindromes and see if they are prime.

    提示 1: 找出所有的回文数再判断它们是不是质数(素数).

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

    提示 2: 要产生正确的回文数,你可能需要几个像下面这样的循环。


    (产生长度为5的回文数:)

    for (d1 = 1; d1 <= 9; d1+=2) { (只有奇数,偶数必非质数)

    for (d2 = 0; d2 <= 9; d2++) {

    for (d3 = 0; d3 <= 9; d3++) {

    palindrome = 10000d1 + 1000d2 +100d3 + 10d2 + d1;

    判断回文...