1.5.2 Prime Palindromes
2016-01-26 15:11
405 查看
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
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
代码:
PROGRAM NAME: pprime
INPUT FORMAT
Line 1: | Two integers, a and b |
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
代码:
#include <iostream> #include <cstdio> #include <algorithm> #define ll long long using namespace std; char s[10]; int l; bool p(ll x) { if(x==1) return false; if(x==2) return true; for(ll i=2;i*i<=x;i++){ if(x%i==0) return false; } return true; } int main() { freopen("pprime.in","r",stdin); freopen("pprime.out","w",stdout); ll a,b,c,d,e,x,y,m; scanf("%lld %lld",&x,&y); for(a=1;a<=9;a++){ if(p(a)&&a>=x&&a<=y) cout<<a<<endl; } for(a=1;a<=9;a+=2){ m=11*a; if(p(m)&&m>=x&&m<=y) cout<<m<<endl; } for(a=1;a<=9;a+=2){ for(b=0;b<=9;b++){ m=101*a+10*b; if(p(m)&&m>=x&&m<=y) cout<<m<<endl; } } for(a=1;a<=9;a+=2){ for(b=0;b<=9;b++){ for(c=0;c<=9;c++){ m=10001*a+1010*b+100*c; if(p(m)&&m>=x&&m<=y) cout<<m<<endl; } } } for(a=1;a<=9;a+=2){ for(b=0;b<=9;b++){ for(c=0;c<=9;c++){ for(d=0;d<=9;d++){ m=1000001*a+100010*b+10100*c+1000*d; if(p(m)&&m>=x&&m<=y) cout<<m<<endl; } } } } return 0; }