当前位置:网站首页>Ehrlich screening method: Counting the number of prime numbers
Ehrlich screening method: Counting the number of prime numbers
2022-08-09 23:11:00 【Doraemon 0219】
The core idea of the sieve method:When we find a prime number, then all multiples of this number less than n are definitely not prime numbers, while marking.
#include using namespace std;class Solution {public:bool isprime(int n){for (int i = 2; i*i <= n; i++){if (n % i == 0)return false;}return true;}int cout_prime(int n){int* tag = new int[n] {0};//0 represents a prime numberint count = 0;for (int i = 2; i < n; i++){if (tag[i]==0){count++;for (int j = i * i; j < n; j += i)//j+=i realizes the increment of i{tag[j] = 1;}}}return count;}};int main(){Solutions;int n = s.cout_prime(100);cout << n;return 0;} In the above code, focus on this code:
for (int j = i * i; j < n; j += i)//j+=i realizes the increment of i{tag[j] = 1;}The original version of this code is j = i*2; but since each i pair increments will be marked repeatedly, the code is optimized and incremented by i itself as a multiple each time to improve efficiency.
边栏推荐
猜你喜欢

Word第一页空白页怎么删除?删除Word第一页空白页方法教程

Ali Ermi: Without accept, can a TCP connection be established?

数独 | 回溯-7

Unity2D_背景粒子效果

抽象类 or 接口

DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles

Beat the interviewer, the CURD system can also make technical content

LoRa无线技术在物联网应用市场的概况和发展
![[corctf 2022] 部分](/img/03/ee1ead55805a2ac690ec79c675c3e6.png)
[corctf 2022] 部分

Number of daffodils within a thousand
随机推荐
Lyapp exponents and bifurcation diagrams for fractional chaotic systems
RHEL7系统修复rm -rf /boot /etc/fstab
Deceptive Dice(期望计算)
STC8H开发(十五): GPIO驱动Ci24R1无线模块
LED闪烁 闪灯芯片IC 手电筒IC 闪灯控制IC 闪烁IC流水灯
supervisor 命令操作大全「建议收藏」
Tensorflow中placeholder函数的用法
AI识万物:从0搭建和部署手语识别系统
10个 Istio 流量管理 最常用的例子,你知道几个?
Leetcode 93 复原IP地址
Daily practice of PMP | Do not get lost in the exam -8.8 (including agility + multiple choice)
Skywalking系列学习之Trace Profiling源码分析
DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified silica particles
UE4_定序器控制蓝图对象
上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具
Word怎么制作双面席卡?使用Word制作双面席卡方法
TF使用constant生成数据
windos安装Mysql8.0,及解决重新登录异常问题 ERROR 1045 (28000)
SQLi-LABS Page-2 (Adv Injections)
Cookie、session、token