
Шли годы, но он так и остался суеверным - он не любил чисел, у которых после 1 стоит 3, то есть образуется число 13. На Новый Год он решил дать новое задание: посчитать, сколько любимых Дедом Морозом простых чисел есть на промежутке [А, В]?
Код решения:
*****
const pr:array[1..127] of integer=(2,3,5,7,11,13,17,19,23,29,
31,37,41,43,47,53,59,61,67,71,
73,79,83,89,97,101,103,107,109,113,
127,131,137,139,149,151,157,163,167,173,
179,181,191,193,197,199,211,223,227,229,
233,239,241,251,257,263,269,271,277,281,
283,293,307,311,313,317,331,337,347,349,
353,359,367,373,379,383,389,397,401,409,
419,421,431,433,439,443,449,457,461,463,
467,479,487,491,499,503,509,521,523,541,
547,557,563,569,571,577,587,593,599,601,
607,613,617,619,631,641,643,647,653,659,
661,673,677,683,691,701,709);
var a, b, rez, i, mm:longint;
flag:boolean;
function proste(t:longint):boolean;
var k, max:longint;
begin
k:=1;
max:=trunc(sqrt(t));
while (pr[k]<=max) and (k<127) do
begin
if t mod pr[k]=0 then begin
proste:=false;
exit;
end;
inc(k);
end;
proste:=true;
end;
function check(N:longint):boolean;
var s:string; ln, j:integer;
begin
check:=true;
str (n,s);
ln:=length(s);
for j:=1 to ln-1 do
if copy(s,j,2)='13' then check:=false;
end;
BEGIN
readln (a, b);
if a>b then begin i:=a; a:=b; b:=i; end;
rez:=0;
if (a=1) and (b=1) then rez:=0;
if (a=1) and (b>1) then a:=2;
if (a=2) and (b=2) then rez:=1;
if (a=2) and (b>2) then begin inc(rez); inc(a); end
else
if a mod 2 = 0 then inc (a);
i:=a;
while i<=b do
begin
if proste(i) then
if check(i) then inc (rez);
inc(i, 2);
end;
writeln (rez);
END.
31,37,41,43,47,53,59,61,67,71,
73,79,83,89,97,101,103,107,109,113,
127,131,137,139,149,151,157,163,167,173,
179,181,191,193,197,199,211,223,227,229,
233,239,241,251,257,263,269,271,277,281,
283,293,307,311,313,317,331,337,347,349,
353,359,367,373,379,383,389,397,401,409,
419,421,431,433,439,443,449,457,461,463,
467,479,487,491,499,503,509,521,523,541,
547,557,563,569,571,577,587,593,599,601,
607,613,617,619,631,641,643,647,653,659,
661,673,677,683,691,701,709);
var a, b, rez, i, mm:longint;
flag:boolean;
function proste(t:longint):boolean;
var k, max:longint;
begin
k:=1;
max:=trunc(sqrt(t));
while (pr[k]<=max) and (k<127) do
begin
if t mod pr[k]=0 then begin
proste:=false;
exit;
end;
inc(k);
end;
proste:=true;
end;
function check(N:longint):boolean;
var s:string; ln, j:integer;
begin
check:=true;
str (n,s);
ln:=length(s);
for j:=1 to ln-1 do
if copy(s,j,2)='13' then check:=false;
end;
BEGIN
readln (a, b);
if a>b then begin i:=a; a:=b; b:=i; end;
rez:=0;
if (a=1) and (b=1) then rez:=0;
if (a=1) and (b>1) then a:=2;
if (a=2) and (b=2) then rez:=1;
if (a=2) and (b>2) then begin inc(rez); inc(a); end
else
if a mod 2 = 0 then inc (a);
i:=a;
while i<=b do
begin
if proste(i) then
if check(i) then inc (rez);
inc(i, 2);
end;
writeln (rez);
END.
*****
Комментариев нет:
Отправить комментарий