极品素数(Pascal)
发布网友
发布时间:2022-10-05 15:28
我来回答
共1个回答
热心网友
时间:2023-10-19 12:18
program superPrime;
uses
sysutils;
const
infilename = 'super.in';
outfilename = 'super.out';
var
tfin : TextFile;
tfout : TextFile;
n, i, j, pc, spc : integer;
isprime : array[1..1000000] of integer;
begin
assign(tfin, infilename);
read(tfin, n);
close(tfin);
pc := 0;
spc := 0;
{初始化}
for i := 2 to n do
isprime[i] := 1;
{素数快筛}
for i := 2 to n do
begin
if isprime[i] = 1 then
begin
j := i + i;
pc := pc + 1;
while j <= n do
begin
isprime[j] := 0;
j := j + i;
end;
end;
end;
{判断个数}
for i := 2 to pc do
if isprime[i] = 1 then
begin
spc := spc + 1;
end;
assign(tfout, infilename);
rewrite(tfout);
writeln(tfout, spc);
close(tfout);
end.