Eratosthenes筛法笔记

E r a t o s t h e n e s Eratosthenes 筛法

概念

E r a t o s t h e n e s Eratosthenes 筛选法,是古希腊数学家 E r a t o s t h e n e s ( 274 B . C .   194 B . C . ) Eratosthenes(274B.C.~194B.C.) 提出的一种针对自然数列中的自然数而实施的筛选法,其作用为求一定范围内的质数。

这是一个很经典的算法,其贯穿所谓“质数数的倍数一定是合数”的理念,筛选出一定范围内的素数。

此算法主要步骤如下

  • 排除 1 1 (将1设置为非质数)
  • 循环变量 i i 2 2 开始遍历到 n n ,若 i i 为质数,则嵌套第二重循环将 i i 的倍数设置为非质数,如此重复

E r a t o s t h e n e s Eratosthenes 筛法有些许小优化,本文就不一一赘述了。

程序

program Eratothenes;
Var flag:array[1..1000005] of boolean;
    n,i,j:longint;
Begin
        read(n);
        fillchar(flag,sizeof(flag),1);
        flag[1]:=false;
        for i:=2 to trunc(sqrt(n)) do
        Begin
                if not flag[i] then
                Begin
                        continue;
                end;
        end;
end.
发布了23 篇原创文章 · 获赞 37 · 访问量 9065

猜你喜欢

转载自blog.csdn.net/weixin_41221124/article/details/100805882