Description
A palindromic prime is simultaneous a prime number and palindromic.
For example, 131 is a prime and also a palindromic.
Write a program that reads an interger n, and finds the nth palindromic prime.
Input
An interger n (1<=n<=50).
Output
The nth palindromic prime.
Sample Input
7
Sample Output
131
// Date:2020/4/9
// Author:xiezhg5
#include <stdio.h>
int isPrime(long n);
long reverse(long n);
long isPalindrome(long n);
int main(void)
{
int num, i, cnt = 0;
while (1)
{
scanf("%d",&num); //输入第几个回文素数
if ((num>=1) && (num<=50) ) break; //读入一个标准数立即判断
}
i = 2;
while (1)
{
//调用isPrime和 isPalindrome函数
if ( isPrime(i) && isPalindrome(i)) cnt ++; //启用累加器
if (cnt == num) break;
i ++;
}
printf("%d",i);
return 0;
}
//判断n是不是素数
int isPrime(long n)
{
int i;
if (n == 2)
return 1;
for (i = 2; i*i <= n;i++)
if (n % i == 0)
return 0;
return 1;
}
//返回逆序后的n
long reverse(long n)
{
int reverseNumber = 0;
//求各进制位上的数字
do
{
reverseNumber = reverseNumber * 10 + n % 10;
n /= 10;
}while(n > 0);
return reverseNumber;
}
//反转后的n与原来的n对比判断是否回文数
long isPalindrome(long n)
{
return reverse(n) == n;
}