当前位置:网站首页>Ehrlich sieve + Euler sieve + interval sieve
Ehrlich sieve + Euler sieve + interval sieve
2022-07-04 08:38:00 【ccsu_ yuyuzi】
Ethmoid O(nloglogn)
The meaning of Ehrlich sieve is very simple , Every time I enumerate the prime numbers I have traversed now , Enumerate all its multiples in the interval , These enumerated numbers are composite numbers , When we enumerate the multiples of all primes in the interval , Mark them all , What is not marked is prime . This also ensures that the next enumeration must be prime , Because the composite number has been marked .
void getp()
{
int cnt=0;
vis[1]=1;
for(int i=2;i<=n;i++)// enumeration
if(vis[i]==0)// Judge whether it is a prime
for(int j=2;j*i<=n;j++)// Enumerating multiples
vis[i*j]=1;
return ;
}
/*
vis Marked as 1 Is not a prime number , Note that after the expansion of the Ehrlich sieve is the interval sieve
*/
But there is a disadvantage of the sieve , We enumerate prime multiples every time , Will produce repeated enumeration , For example, a number is a multiple of some primes , The enumeration will be repeated several times . If we can eliminate duplicate enumeration , Under this premise , Created the Euler sieve .
Euler screen O(N)
The meaning of Euler sieve is optimized Ehrlich sieve . Every time we judge whether a number is prime , Then enumerate , But its European code is that each number will be enumerated only once . When the current number is divided by the prime factor of the traversal , We need to pay attention to , This number has been enumerated , direct break that will do .
void getp()
{
int cnt=0;
for(int i=2;i<=n;i++)
{
if(vis[i]==0)
p[++cnt]=i;
for(int j=1;j<=cnt&&p[j]*i<=n;j++)
{
vis[i*p[j]]=1;
if(i%p[j]==0)
break;
}
}
return ;
}
Interval sieve The time complexity is similar to that of the sieve
Interval sieve is a variant of Ehrlich sieve , It is for a certain value , But the algorithm for solving the prime number problem in the interval whose length is still within the calculation range . We first have to enumerate 2 To The prime number of . Then use these prime numbers to enumerate in the interval [L,R] Multiple of , The multiples of these enumerations are composite numbers , Mark directly , What is not marked in the interval is prime .
void segment_sieve(int a,int b)
{
for(int i=0;i<1e6+5;i++)
isprime[i]=lowisprime[i]=true;
for(int i=2;i*i<b;i++)
{
if(lowisprime[i])
{
for(int j=2*i;j*j<b;j+=i)
lowisprime[j]=false;
for(long long j=(a/i)*i;j<b;j+=i)
if(j>=a&&j<b)
isprime[j-a]=false;
}
}
}
边栏推荐
- [attack and defense world | WP] cat
- What should I do if there is a problem with the graphics card screen on the computer
- DM8 database recovery based on point in time
- The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
- 微服务入门:Gateway网关
- @Role of pathvariable annotation
- [performance test] read JMeter
- Show server status on Web page (on or off) - PHP
- C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
- FOC控制
猜你喜欢
Redis sentinel mechanism
Educational Codeforces Round 119 (Rated for Div. 2)
ctfshow web255 web 256 web257
How to play dapr without kubernetes?
Question 49: how to quickly determine the impact of IO latency on MySQL performance
Unity text superscript square representation +text judge whether the text is empty
Openfeign service interface call
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
DM database password policy and login restriction settings
埃氏筛+欧拉筛+区间筛
随机推荐
AcWing 244. Enigmatic cow (tree array + binary search)
[test de performance] lire jmeter
Put a lantern on the website during the Lantern Festival
DM8 uses different databases to archive and recover after multiple failures
awk从入门到入土(15)awk执行外部命令
1. Kalman filter - the best linear filter
NewH3C——ACL
How to solve the problem that computers often flash
@Role of pathvariable annotation
[untitled] 2022 polymerization process analysis and polymerization process simulation examination
[go basics] 2 - go basic sentences
High order phase difference such as smear caused by myopic surgery
How college students choose suitable computers
How to get bytes containing null terminators from a string- c#
转:优秀的管理者,关注的不是错误,而是优势
How to send pictures to the server in the form of file stream through the upload control of antd
Add log file to slim frame - PHP
Unity text superscript square representation +text judge whether the text is empty
ES6 summary
How does Xiaobai buy a suitable notebook