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.
該算法非常簡單。
Say, we need information up to integer N.
假設我們需要最大為N的信息 。
Create a table of size N+1, sieve[N+1]
創建一個大小為N + 1的表,篩子[N + 1]
Assign all the table entries TRUE initially.
最初將所有表條目分配為TRUE 。
Base case:
基本情況:
0 and 1 is not prime
0和1不是素數
Thus
從而
sieve[0]=FALSE=sieve[1]
sieve [0] = FALSE = sieve [1]
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 20Thus initially declare sieve[21] and all are true.sieve[0]=FALSEsieve[1]=FALSE------------------------------------------------------------sieve[2]=trueThus all multiple of 2, needed to be false(Of course they are not prime since divisible by 2)Thus,sieve[4]=FALSEsieve[6]=FALSEsieve[8]=FALSEsieve[10]=FALSEsieve[12]=FALSEsieve[14]=FALSEsieve[16]=FALSEsieve[18]=FALSEsieve[20]=FALSE------------------------------------------------------------sieve[3]=trueThus 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]=FALSEsieve[12]=FALSE(it was already false though)sieve[15]=FALSEsieve[18]=FALSE(it was already false though)------------------------------------------------------------Sieve[4]=FALSE (Nothing to do more with it)------------------------------------------------------------sieve[5]=TRUEThus 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 findOnly true entries aresieve[2]sieve[3]sieve[5]sieve[7]sieve[11]sieve[13]sieve[17]sieve[19]Others are false. .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}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 eratosthenesmemset(sieve,true,sizeof(sieve));//initially all truesieve[0]=sieve[1]=false;//0,1 not primefor(int i=2;i*i<=n;i++)if(sieve[i])for(int j=i*2;j<=n;j+=i)//for multiple of isieve[j]=false;//can't be primecout<<"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";elsecout<<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
總結
以上是生活随笔為你收集整理的Eratosthenes筛的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: scala 字符串转换数组_如何在Sca
- 下一篇: vector clone_Java Ve