当前位置:网站首页>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;
}
}
}
边栏推荐
- MySQL relearn 1-centos install mysql5.7
- awk从入门到入土(14)awk输出重定向
- 4 small ways to make your Tiktok video clearer
- Four essential material websites for we media people to help you easily create popular models
- 2022 examination questions for safety managers of metal and nonmetal mines (underground mines) and examination papers for safety managers of metal and nonmetal mines (underground mines)
- ctfshow web255 web 256 web257
- Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
- 09 softmax regression + loss function
- From scratch, use Jenkins to build and publish pipeline pipeline project
- PHP session variable passed from form - PHP
猜你喜欢
Go zero micro service practical series (IX. ultimate optimization of seckill performance)
From scratch, use Jenkins to build and publish pipeline pipeline project
DM8 command line installation and database creation
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
Unity-Text上标平方表示形式+text判断文本是否为空
Moher college phpMyAdmin background file contains analysis traceability
Developers really review CSDN question and answer function, and there are many improvements~
SSRF vulnerability exploitation - attack redis
How to solve the problem of computer jam and slow down
一文了解數據异常值檢測方法
随机推荐
4 small ways to make your Tiktok video clearer
Use preg_ Match extracts the string into the array between: & | people PHP
Comparison between sentinel and hystrix
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
2022 gas examination registration and free gas examination questions
团体程序设计天梯赛-练习集 L2-002 链表去重
Convert datetime string to datetime - C in the original time zone
Developers really review CSDN question and answer function, and there are many improvements~
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Put a lantern on the website during the Lantern Festival
Leetcode topic [array] -136- numbers that appear only once
deno debugger
How to set multiple selecteditems on a list box- c#
PHP session variable passed from form - PHP
转:优秀的管理者,关注的不是错误,而是优势
【无标题】转发最小二乘法
What if I forget the router password