DESCRIPTION
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 <= 100,000,000); both a and b are considered to be within the range .
PROGRAM NAME: pprime
INPUT FORMAT
Line 1: Two integers, a and b
OUTPUT FORMAT
The list of palindromic primes in numerical order, one per line.
HINTS (use them carefully!)
HINT1 HINT2
TRANSLATION
找到给粗区间[a..b]中的所有回文素数
ANALYSIS
突然想起还有usaco这么个题库 不要在意之前干嘛去了
咳咳,枚举a~b区间的数字,分别判断是不是回文数和素数
玄学优化:
1. 除5外,以5,0结尾的数字是合数,false
2. 除2外,以偶数结尾的数字是合数,false
3. 除11外,偶数数位的回文数字是合数,false
4. 最大不可能超过9989899 我也不知道为什么
CODE
/*
ID:wjp13241
PROG:pprime
LANG:C++
*/
#include <stdio.h>
#include <cmath>
using namespace std;
int prime(int x)
{
for (int i=2;i<=(int)(sqrt(x));i++)
if (!(x%i))
return 0;
return 1;
}
int palindrome(int x)
{
int f[10]={0};
while(x)
{
f[++f[0]]=x%10;
x/=10;
}
int l=0;
while (++l<=f[0]/2)
{
if (f[l]!=f[f[0]-l+1])
return 0;
}
return 1;
}
int main()
{
freopen("pprime.in","r",stdin);
freopen("pprime.out","w",stdout);
int a,b;
scanf("%d%d",&a,&b);
a=a==1?2:a;
b=b>9989899?9989899:b;
for (int i=a;i<=b;i++)
{
if (!palindrome(i))
continue;
if (!prime(i))
continue;
printf("%d\n",i);
}
fclose(stdin);
fclose(stdout);
return 0;
}