当前位置:网站首页>Linear sieve method (prime sieve)
Linear sieve method (prime sieve)
2022-04-23 05:33:00 【Ice Cream~】
One O(n) The algorithm of filtering prime number with time complexity :
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=1000000;
int primes[N],cnt;//primes Array storage prime
int minp[N];// Store the minimum quality factor
bool st[N];// Is it currently filtered
void get_primes(int n)
{
for(int i=2;i<=n;i++)
{
if(!st[i])minp[i]=i,primes[cnt++]=i;
for(int j=0;primes[j]*i<=n;j++)// Screening prime
{
st[primes[j]*i]=true;
minp[primes[j]*i]=primes[j];//primes[j]*i The minimum quality factor of is priems
if(i%primes[j]==0)break;//primes No more than i The minimum quality factor of
}
}
}
int main()
{
get_primes(10000);
for(int i=2;i<20;i++)
{
cout<<primes[i]<<" "<<minp[i]<<endl;
}
}
版权声明
本文为[Ice Cream~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534524920.html
边栏推荐
- Create process memory management copy_ Mm - processes and threads (IX)
- solidity合约DOS攻击
- How to realize adaptive layout
- 使用宝塔+xdebug+vscode远程调试代码
- d. TS --- for more detailed knowledge, please refer to the introduction on the official website (chapter of declaration document)
- CORS and proxy (づ  ̄ 3  ̄) in egg ~ the process of stepping on the pit and filling the pit ~ tot~
- Rog attack
- Why can't V-IF and V-for be used together
- FileReader API file operation
- CMake基础教程(39)pkgconfig
猜你喜欢
STL learning notes 0x0001 (container classification)
Fast application fuzzy search
Hongji cyclone RPA provides technical support for Guojin securities and realizes process automation in more than 200 business scenarios
跨域CORS的情缘~
Basic knowledge of redis
[the background color changes after clicking a line]
what is wifi6?
On the use of constant pointer and pointer constant -- exercise (record)
Box collapse and margin collapse
双击.jar包无法运行解决方法
随机推荐
selenium預先加載cookie的必要性
Click the Add button - a box appears (similar to adding learning experience - undergraduate - Graduate)
varnish入门
Fast application fuzzy search
Usage and difference of shellexecute, shellexecuteex and winexec in QT
C, class library
String class understanding - final is immutable
How to realize adaptive layout
Rog attack
FileReader API file operation
OSI层常用协议
JS time format conversion
Similarities and differences between vector and array (notes)
Deep learning object detection
CPT 104_TTL 09
踩坑:nacos利用startup.cmd -m standalone启动错误
TSlint注释忽略错误和RESTful理解
巴普洛夫与兴趣爱好
Branch and loop statements
Frequently asked interview questions - 2 (computer network)