The number 151 is a prime palindrome because it is both a prime number and a palindrome (it is the same number when read forward as backward). Write a program that finds all prime palindromes in the range of two supplied numbers a and b (5 <= a < b <= 1000,000,000); both a and b are considered to be within the range .
Input
Line 1: Two integers, a and b
Output
The list of palindromic primes in numerical order, one per line.
Sample Input
5 500
Sample Output
5
7
11
101
131
151
181
191
313
353
373
383
代码:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*
目的:输入两个数 输出这两个数之间的既是回文数又是素数的数*/
int prime(int n)/*判断一个数是不是素数*/
{
int k=0,m,flag=1;/*定义变量名*/
m=(int)sqrt(n);
for (k=2;k<=m&&flag;k++)/*通过循环判断一个数有没有因数*/
{
if (n%k==0)
flag=0;
}
if (flag)
return 1;
else
return 0;
}
int palindrome(long n)/*判断一个数是不是回文数*/
{
long i=0,m=n;
while (m!=0)/*把数字从头到尾反过来然后与原数比较 若想等则输出是回文数*/
{
i=i*10+m%10;
m=m/10;
}
if (i==n)
return n;
else
return 0;
}
int main()
{
int i,a,b;
char c;
do
{
printf("please input two numbers,the frist must be litter than the second./n");
printf("And the two numbers must be litter than 1000000000./n");
scanf(" %d%*c%d",&a,&b);
if (a>=b||b>1000000000)
{
printf("what you enter is wrong,please correct them./n");
}
else
{
for (i = a; i < b; i++)/*从输入的数开始算起到最后一个数挨个判断是不是回文数和素数*/
{
if ( palindrome(i)&& prime(i))
{
printf("%d/n", i);
}
}
}
printf("If you want to end the game please enter 0,else please enter anyother character./n");
scanf(" %c",&c);
}
while (c!='0');/*循环进行上述程序直至用户输入0停止循环*/
printf("The game is end");
return 0;
}