当前位置:网站首页>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.
边栏推荐
猜你喜欢

SQLi-LABS Page-2 (Adv Injections)

LoRa无线技术在物联网应用市场的概况和发展

论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》

角度和弧度的相互换算

AI+医疗:使用神经网络进行医学影像识别分析

poj 3070 Fibonacci(简单矩阵连乘)

AI Knows Everything: Building and Deploying a Sign Language Recognition System from Zero

Install Mysql8.0 on windos, and solve the problem of re-login exception ERROR 1045 (28000)

Visual studio 2022 debugging skills introduction

Two methods of implementing inverted strings in C language
随机推荐
Access Characteristics of Constructor under Inheritance Relationship
同步锁synchronized追本溯源
字符串哈希(2014 SERC J题)
筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
AI识万物:从0搭建和部署手语识别系统
自监督学习 —— MoCo v2
微软word怎么转换成pdf文件?微软word转换为pdf格式的方法
knn到底咋回事?
STC8H开发(十五): GPIO驱动Ci24R1无线模块
Reverse Analysis of Unknown Cryptographic Protocol Based on Network Data Flow
SQL语句及索引的优化
poj 3070 Fibonacci(简单矩阵连乘)
TF中random.normal()与random.truncated_normal()
10个 Istio 流量管理 最常用的例子,你知道几个?
技术分享 | 接口自动化测试之JSON Schema模式该如何使用?
MySQL跨表、多表更新SQL语句总结
DSPE-PEG-PDP, DSPE-PEG-OPSS, phospholipid-polyethylene glycol-mercaptopyridine reduce the immunogenicity of peptides
Puyuan Jingdian turned losses into profits in the first half of the year, and high-end products continued to develop!Are you optimistic about "Huawei" in the instrument industry?
Ali Ermi: Without accept, can a TCP connection be established?
Definition and Basic Operations of Sequence Tables