Eratosthenes筛

什么是Eratosthenes筛? (What is Sieve of Eratosthenes?)

Sieve of Eratosthenes is an ancient algorithm of finding prime numbers for any given range. It's actually about maintaining a Boolean table to check for corresponding prime no.

Eratosthenes的筛子是一种古老的算法,可以找到任何给定范围的质数。 实际上,这是关于维护布尔表以检查相应的素数。

算法 (Algorithm)

The algorithm is pretty simple.

该算法非常简单。

  1. Say, we need information up to integer N.

    假设我们需要最大为N的信息

  2. Create a table of size N+1, sieve[N+1]

    创建一个大小为N + 1的表,筛子[N + 1]

  3. Assign all the table entries TRUE initially.

    最初将所有表条目分配为TRUE

  4. Base case:

    基本情况:

    0 and 1 is not prime

    0和1不是素数

    Thus

    从而

    sieve[0]=FALSE=sieve[1]

    sieve [0] = FALSE = sieve [1]

  5. For(i=2; i*i<=N; i=i+1)
    IF(sieve[i] is TRUE) //i is prime
    Make all multiple ofi FALSE since they are not prime
    sieve[j]=FALSE where j is multiple of i within range N
    END IF
    END FOR
    
    
  6. Table is built. Now to check any integer K whether prime or not

    表已建立。 现在检查任何整数K是否为素数

    Return

    返回

    sieve[k]

    筛[k]

Example with explanation:

带有说明的示例:

    Let's say our range is up to 20
Thus initially declare sieve[21] and all are true.
sieve[0]=FALSE
sieve[1]=FALSE
------------------------------------------------------------
sieve[2]=true
Thus all multiple of 2, needed to be false
(Of course they are not prime since divisible by 2)
Thus,
sieve[4]=FALSE
sieve[6]=FALSE
sieve[8]=FALSE
sieve[10]=FALSE
sieve[12]=FALSE
sieve[14]=FALSE
sieve[16]=FALSE
sieve[18]=FALSE
sieve[20]=FALSE
------------------------------------------------------------
sieve[3]=true
Thus all multiple of 3, needed to be false
(Of course they are not prime since divisible by 3)
Thus,
sieve[6]=FALSE (it was already false though)
sieve[9]=FALSE
sieve[12]=FALSE(it was already false though)
sieve[15]=FALSE
sieve[18]=FALSE(it was already false though)
------------------------------------------------------------
Sieve[4]=FALSE (Nothing to do more with it)
------------------------------------------------------------
sieve[5]=TRUE
Thus all multiple of 5, needed to be false
(Of course they are not prime since divisible by 5)
Thus,
sieve[10]=FALSE (it was already false though)
sieve[15]=FALSE (it was already false though)
sieve[20]=FALSE (it was already false though)
In this way, after completing all possible entries we can find
Only true entries are
sieve[2]
sieve[3]
sieve[5]
sieve[7]
sieve[11]
sieve[13]
sieve[17]
sieve[19]
Others are false.

So now for any given number with in the N

所以对于N中的任何给定数

We can tell whether the number is prime or not in O(1) time (based on corresponding TRUE/FALSE value).

我们可以在O(1)时间内判断数字是否为质数(基于相应的TRUE / FALSE值)。

何时在编程中使用此算法? (When to use this algorithm in programming?)

Sieve of Eratosthenes can be very efficient for any program we require to check prime numbers for multiple times (Multiple test cases too).

Eratosthenes筛网对于我们需要多次检查质数的任何程序(也可以是多个测试用例)来说都是非常有效的。

In such cases, we construct the Sieve of Eratosthenes only single time and for all query each only takes O(1) time reducing the time complexity overall.

在这种情况下,我们只构造一次Eratosthenes筛,对于所有查询,每个筛仅花费O(1)时间,从而降低了总体时间复杂度。

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cout<<"Enter max range to check for prime numbers\n";
cin>>n;
bool sieve[n+1];
//sieve of eratosthenes
memset(sieve,true,sizeof(sieve));//initially all true
sieve[0]=sieve[1]=false;//0,1 not prime
for(int i=2;i*i<=n;i++)
if(sieve[i])
for(int j=i*2;j<=n;j+=i)//for multiple of i
sieve[j]=false;//can't be prime
cout<<"Enter non-negative integer to check prime or not\n";
cout<<"Negative no to exit\n";
int k;
cin>>k;
while(k>=0){
if(sieve[k])
cout<<k<<" is prime\n";
else
cout<<k<<" is not prime\n";
cout<<"Try more or exit\n";
cin>>k;
}
cout<<"Exited\n";
return 0;
}

Output

输出量

Enter max range to check for prime numbers
1000
Enter non-negative integer to check prime or not
Negative no to exit
997
997 is prime
Try more or exit 
993
993 is not prime 
Try more or exit 
13 
13 is prime 
Try more or exit 
103
103 is prime
Try more or exit 
111
111 is not prime 
Try more or exit 
-1 
Exited

翻译自: https://www.includehelp.com/icp/sieve-of-eratosthenes.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543800.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微信iOS多设备多字体适配方案总结

一、背景 2014下半年&#xff0c;微信iOS版先后适配iPad, iPhone6/6plus。随着这些大屏设备的登场&#xff0c;部分用户觉得微信的字体太小&#xff0c;但也有很多用户不喜欢太大的字体。为了满足不同用户的需求&#xff0c;我们做了全局字体设置功能&#xff0c;在【设置-通用…

python矩阵中插入矩阵_Python | 矩阵的痕迹

python矩阵中插入矩阵The sum of diagonal elements of a matrix is commonly known as the trace of the matrix. It is mainly used in eigenvalues and other matrix applications. In this article, we are going to find the trace of a matrix using inbuilt function nu…

TOP命令监视系统任务及掩码umask的作用

top 命令使用方法及參数。 top 选择參数 參数&#xff1a; -b 以批量模式执行。但不能接受命令行输入&#xff1b;-c 显示命令行&#xff0c;而不不过命令名。-d N 显示两次刷新时间的间隔&#xff0c;比方 -d 5&#xff0c;表示两次刷新间隔为5秒&#xff1b;-i 禁止显示空暇…

python点线图_Python | 点线图

python点线图A mixture of dot and line plot is called a Dot-Line plot. Each dot is connected through a line and it is the next version of the line plot. It maintains the discrete property of the points and also represents the correlation between consecutive…

Android Studio导入工程的正确姿势

为什么80%的码农都做不了架构师&#xff1f;>>> 如果你有很好的网络环境 好的网络环境&#xff0c;这里不是指&#xff1a;我家网速带宽100M&#xff0c;电信的光纤接入。 而是&#xff1a;能翻墙。因为如果本机的gradle和将要导入的工程版本不匹配&#xff0c;Stu…

BBIAF的完整形式是什么?

BBIAF&#xff1a;回来几场 (BBIAF: Be Back In A Few) BBIAF is an abbreviation of "Be Back In A Few". BBIAF是“几回去”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, …

为什么年轻人挣得很多还是穷?北上广深挑战指数报告~

又是年底&#xff0c;又到了做选择的时候。从“激情燃烧的岁月”到“何处安放的青春”&#xff0c;逃离北上广深的口号从未停止过&#xff0c;回到北上广深的呼喊更是一浪接着一浪。应届生们奔波忙碌&#xff0c;想有一份承载自己梦想的工作&#xff0c;想在异乡有一处安身之所…

apple组织名称是什么_什么是Apple Macintosh?

apple组织名称是什么苹果Macintosh (Apple Macintosh) Steve Jobs and Steve Wozniak has founded the line of computers in the year 1984, on the date 24th January, named it Apple Macintosh. Macintosh is shortly abbreviated as Mac. In this, various versions of co…

什么是Apple Desktop Bus? 亚行代表什么?

亚行&#xff1a;Apple桌面总线 (ADB: Apple Desktop Bus) ADB is an abbreviation of "Apple Desktop Bus". ADB是“ Apple Desktop Bus”的缩写 。 It is a low-speed proprietary bit-serial peripheral bus connecting devices to computers. In 1986, it was l…

CentOS 创建SVN 服务器,并且自动同步到WEB 目录

CentOS 创建SVN 服务器&#xff0c;并且自动同步到WEB 目录 标签&#xff1a; centossvnsubversion服务器2013-12-06 10:09 5492人阅读 评论(0) 收藏 举报分类&#xff1a;linux&#xff08;5&#xff09; 一、安装Subversion #yum install subversion二&#xff0c;基本的SVN服…

TTYL的完整形式是什么?

TTYL&#xff1a;稍后再与您交谈 (TTYL: Talk To You Later) TTYL is an abbreviation of Talk To You Later. It is an internet slang that is most generally used in text messaging, instant messaging, and chatting on Facebook, Twitter, WhatsApp, etc. The acronym i…

zhilizhili-ui 2016始动 开始做个样例站吧 (一)

对 我又挖坑了 不过其实也不算挖坑 因为ui构建中就会有填坑的文章 之前一直在写《编写大型web页面 结合现有前端形势思考未来前端》这是一篇巨难写的文章 估计要到年中才能写好了 写作的过程中 发生了国内前端大撕逼 2015的尾声大战 是否可以宣告前端是否开始新的时代 2016年 国…

python 网格_Python | 网格到情节

python 网格Most of the time, we need good accuracy in data visualization and a normal plot can be ambiguous. So, it is better to use a grid that allows us to locate the approximate value near the points in the plot. It helps in reducing the ambiguity and t…

2016年1月计划

开始试着每月做计划和总结&#xff0c;有节奏的规划自己的时间&#xff0c;一月计划&#xff1a; 1、hive那本书拖了很久了&#xff0c;一月一定会看完。 2、因为跟着阚爷的风准备试着做一下讲师&#xff0c;分配给我的是推荐这块&#xff0c;所以网上多找找做推荐的资源&#…

slr1文法_SLR的完整形式是什么?

slr1文法单反&#xff1a;单镜头反光 (SLR: Single Lens Reflex) SLR is an abbreviation of Single Lens Reflex. It is used in high standard cameras. SLR makes use of an automatic moving mirror arrangement that makes it possible for photographers to perceive pre…

vim快捷键2

一、移动光标 1、左移h、右移l、下移j、上移k 2、向下翻页ctrl f&#xff0c;向上翻页ctrl b 3、向下翻半页ctrl d&#xff0c;向上翻半页ctrl u 4、移动到行尾$&#xff0c;移动到行首0&#xff08;数字&#xff09;&#xff0c;移动到行首第一个字符处^ 5、移动光标到下一…

FYR的完整形式是什么?

财政年度&#xff1a;供您参考 (FYR: For Your Reference) FYR is an abbreviation of "For Your Reference". FYR是“供您参考”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is written as a follow-up message for the info…

UIScrollView的简单使用

- UIScrollView 介绍 问&1.UIScrollView 是干什么的? • UIScrollView 也是一种控件&#xff0c;继承自UIView。• 用来实现”滚动”和”缩放”的控件 什么是UIScrollView? UIScrollView是一个能够滚动的视图控件&#xff0c;可以用来展示大量的内容&#xf…

什么是苹果耳塞?

苹果耳塞 (Apple Earbuds) Apple Earbuds are another sound device made by Apple on 23rd October 2001. It is an in-ear sound device and it has been included in all the mobile and music devices of Apple. Apple Earbuds are quite comfortable and easy to handle w…

趣说游戏AI开发:对状态机的褒扬和批判

0x00 前言 因为临近年关工作繁忙&#xff0c;已经有一段时间没有更新博客了。到了元旦终于有时间来写点东西&#xff0c;既是积累也是分享。如题目所示&#xff0c;本文要来聊一聊在游戏开发中经常会涉及到的话题——游戏AI。设计游戏AI的目标之一是要找到一种便于使用并容易拓…