首页 > 其他 > 详细

O(n) 筛法求素数

时间:2014-10-28 21:13:00      阅读:248      评论:0      收藏:0      [点我收藏+]

var tot,i,j,k,m,n:longint;
prime:array[0..100000] of boolean;
p:array[0..100000] of longint;
begin
read(n);
fillchar(prime,sizeof(prime),true);
prime[1]:=false;
tot:=0;
fillchar(p,sizeof(p),0);
for i:=2 to n do
begin
if prime[i] then
begin
inc(tot);
p[tot]:=i;
end;
for j:=1 to tot do
begin
if i*p[j]>n then break;
prime[i*p[j]]:=false;
if i mod p[j]=0 then break;
end;
end;
for i:=1 to tot do
writeln(p[i]);
end.

O(n) 筛法求素数

原文:http://www.cnblogs.com/syzcannot/p/4057644.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!