当前位置:网站首页>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.
边栏推荐
- Jensen (琴生) 不等式
- laravel 表迁移报错[通俗易懂]
- 场效应管Mosfet之雷卯Leiditech对应英飞凌Infineon
- Problems with compiling SIP with QGIS
- Definition and Basic Operations of Sequence Tables
- 筑牢安全防线 鹤壁经济技术开发区开展安全生产培训
- Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
- An overall security understanding and method of cyberspace based on connection and security entropy
- Reverse Analysis of Unknown Cryptographic Protocol Based on Network Data Flow
- Access control knowledge
猜你喜欢
随机推荐
蓝牙模块的分类和对应的属性特点
论文解读(DropEdge)《DropEdge: Towards Deep Graph Convolutional Networks on Node Classification》
别叫我玩,我要考PMP:考PMP选择机构需要了解的那些事儿
AI识万物:从0搭建和部署手语识别系统
hdu 1333 Smith Numbers(暴力思路)
matlab neural network ANN classification
Lyapp exponents and bifurcation diagrams for fractional chaotic systems
PMP每日一练 | 考试不迷路-8.9(包含敏捷+多选)
TF中使用zeros(),ones(), fill()方法生成数据
消防安全培训|“蓝朋友”,开课了!
什么是IDE(集成开发环境)?
上海控安SmartRocket系列产品推介(三):SmartRocket iVerifier计算机联锁系统验证工具
NetCore路由的Endpoint模式
laravel 表迁移报错[通俗易懂]
数独 | 回溯-7
Unity2D_线框材质
【双链表增删查改接口的实现】
Endpoint mode for NetCore routing
RHEL7系统修复rm -rf /boot /etc/fstab
Beat the interviewer, the CURD system can also make technical content