1.欧拉筛法在线性时间内求素数以及欧拉函数

代码:

 1 procedure get;
 2  var i,j,k:longint;
 3  begin
 4  tot:=0;
 5  fillchar(check,sizeof(check),false);
 6  for i:=2 to n do
 7   begin
 8   if not(check[i]) then
 9    begin
10     inc(tot);
11     p[tot]:=i;
12     fai[i]:=i-1;
13    end;
14   for j:=1 to tot do
15    begin
16     k:=i*p[j];
17     if k>n then break;
18     check[k]:=true;
19     if i mod p[j]=0 then
20      begin
21       fai[k]:=fai[i]*p[j];
22       break;
23      end
24     else
25       fai[k]:=fai[i]*(p[j]-1);
26    end;
27  end;
28 end;
View Code

相关文章: