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 |
SAMPLE INPUT (file pprime.in)
5 500
OUTPUT FORMAT
The list of palindromic primes in numerical order, one per line.
SAMPLE OUTPUT (file pprime.out)
5
7
11
101
131
151
181
191
313
353
373
383
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #define ll long long 5 using namespace std; 6 char s[10]; 7 int l; 8 bool p(ll x) 9 { 10 if(x==1) return false; 11 if(x==2) return true; 12 for(ll i=2;i*i<=x;i++){ 13 if(x%i==0) return false; 14 } 15 return true; 16 } 17 18 int main() 19 { 20 freopen("pprime.in","r",stdin); 21 freopen("pprime.out","w",stdout); 22 ll a,b,c,d,e,x,y,m; 23 scanf("%lld %lld",&x,&y); 24 for(a=1;a<=9;a++){ 25 if(p(a)&&a>=x&&a<=y) cout<<a<<endl; 26 } 27 for(a=1;a<=9;a+=2){ 28 m=11*a; 29 if(p(m)&&m>=x&&m<=y) cout<<m<<endl; 30 } 31 for(a=1;a<=9;a+=2){ 32 for(b=0;b<=9;b++){ 33 m=101*a+10*b; 34 if(p(m)&&m>=x&&m<=y) cout<<m<<endl; 35 } 36 } 37 for(a=1;a<=9;a+=2){ 38 for(b=0;b<=9;b++){ 39 for(c=0;c<=9;c++){ 40 m=10001*a+1010*b+100*c; 41 if(p(m)&&m>=x&&m<=y) cout<<m<<endl; 42 } 43 } 44 } 45 for(a=1;a<=9;a+=2){ 46 for(b=0;b<=9;b++){ 47 for(c=0;c<=9;c++){ 48 for(d=0;d<=9;d++){ 49 m=1000001*a+100010*b+10100*c+1000*d; 50 if(p(m)&&m>=x&&m<=y) cout<<m<<endl; 51 } 52 } 53 } 54 } 55 return 0; 56 }