Palindromic prime
Time Limit: 2000/1000ms (Java/Others)
Problem Description:
Number 131 is a primary palindrome because it's a prime number and a palindrome (when read backwards, it's the same number). Write a program that counts the number
of principal palindromes in the range of two provided numbers a and b (5 <= a <b <= 10^8) ; both a and b are considered to be in range.
For example, the range 5-120 contains 3 principal palindromes (5, 7, 11, 101).
Output:
For each case, output the answer to the question.
Sample Output:
3
4
Problem-solving ideas: This problem is the same as Hangdian hdu1431, link --> problem solution report: hdu 1431 prime number palindrome , but changed to the number of palindromic prime numbers in the output interval, also note that the input a may be is greater than b, so we need to judge.
1 #include<bits/stdc++.h>
2 #define maxn 9999999
3 using namespace std;
4 bool prime1[maxn];
5 int cnt,a,b,num;
6 bool prime2(int n)
7 {
8 int sum=0,tmp=n;
9 while(tmp){
10 sum=sum*10+tmp%10;
11 tmp/=10;
12 }
13 if(sum==n)return true;
14 else return false;
15 }
16 int main()
17 {
18 memset(prime1,true,sizeof(prime1));
19 prime1[0]=prime1[1]=false;
20 for(int i=4;i<maxn;i+=2)prime1[i]=false;
21 for(int i=3;i<=3163;i+=2){
22 if(prime1[i]){
23 for(int j=i*i;j<maxn;j+=2*i)prime1[j]=false;
24 }
25 }
26 while(cin>>a>>b){
27 num=0;
28 for(int i=a;i<=b;i++){
29 if(i>=maxn)continue;
30 if(prime1[i]&&prime2(i))num++;
31 }
32 cout<<num<<endl;
33 }
34 return 0;
35 }