Description
因为151即是一个质数又是一个回文数(从左到右和从右到左是看一样的),所以 151 号是回文质数。
写一个程序来找出范围[a,b](5 <= a < b <= 100,000,000)间的所有回文质数;
Input
第 1 行: 二个整数 a 和 b .
Output
输出一个回文质数的列表,一行一个。
Sample Input
5 500
Sample Output
5
7
11
101
131
151
181
191
313
353
373
383
解题思路:先用筛数法把素数筛出来,再判断它是不是回文数,如果是就输出。
程序:
var
i,j,n,m:longint;
a:array[2..10000000] of boolean;
function huiwen(x:longint):boolean;
var
i,j:longint;
s:string;
begin
huiwen:=true;
str(x,s);
i:=0;
j:=1+length(s);
while i<=length(s) div 2 do
begin
inc(i);
dec(j);
if s[i]<>s[j] then begin huiwen:=false; break; end;
end;
end;
begin
read(n,m);
if m>10000000 then m:=10000000;
for i:=2 to m do
a[i]:=true;
for i:=2 to trunc(sqrt(m)) do
if a[i] then for j:=2 to m div i do
a[i*j]:=false;
for i:=n to m do
if (a[i]) and (huiwen(i)) then writeln(i);
end.