埃拉托斯特尼筛法(Sieve of Eratosthenes)简单c实现
来源:互联网 发布:淘宝争议处理规则 编辑:程序博客网 时间:2024/06/11 22:20
leetcode上的一道题目https://leetcode.com/problems/count-primes/
是关于数素数个数的。
然后根据提示查了下wiki
虽然中文版与英文版出入比较大,但基本上算说清楚了。
int countPrimes(int n) { bool *pb = calloc(n-1,sizeof(bool)); int ret_c=0; // idx 0 represent 2 int idx=0; int pend=n-2; while(idx<pend){ if(0==pb[idx]){ ++ret_c; int op=idx; while(op<pend){ pb[op]=1; op+=(idx+2); } } ++idx; } free(pb); return ret_c;}
感觉还是挺巧妙的一个算法。上面为了省点内存做了个偏移。用idx0代表了素数2
算法的细节wiki已经解释得很详细了。
一句话就是每找到一个素数就把所有素数倍数的数标记为非素数,然后继续找没被标记的数。
0 0
- 埃拉托斯特尼筛法(Sieve of Eratosthenes)简单c实现
- Sieve Of Eratosthenes求素数(埃拉托斯特尼筛法)
- Sieve of Eratosthenes算法实现
- Sieve of Eratosthenes(埃拉托斯特尼素数筛选法)--java实现
- Sieve of Eratosthenes Algorithm算法 JAVA实现
- Sieve of Eratosthenes[ZT]
- The Sieve of Eratosthenes
- Sieve of Eratosthenes
- sieve of Eratosthenes 找质素
- the Sieve of Eratosthenes
- 09 Sieve of Eratosthenes
- Sieve of Eratosthenes
- Sieve of Eratosthenes求质数
- 素数筛法/埃拉托斯特尼筛法(sieve of Eratosthenes)——理解及具体问题
- 找质数算法(Sieve of Eratosthenes筛法)
- 找质数算法(Sieve of Eratosthenes筛法)
- 找质数算法(Sieve of Eratosthenes筛法)
- 素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】
- 栈和队列的相互实现
- LeetCode Minimum Depth of Binary Tree
- linux环境变量之jdk+tomcat+maven
- MySQL学习笔记_11_Linux下C++/C连接MySQL数据库(一)
- MyBatis Invalid bound statement
- 埃拉托斯特尼筛法(Sieve of Eratosthenes)简单c实现
- 黑马程序员——Java高新技术——JDK1.5版本的新特性泛型
- Linux下硬盘读写测试以及编译用时分析
- 设计模式学习--观察者模式(Oberser Pattern)
- hibernate连接mysql配置文件
- 存档IMAP或POP3汇集邮箱
- PMP项目管理之核心:项目经理角色定位
- 图像预处理——二值化(大律法)
- js原型