LeetCode Count Primes 求素数个数(埃拉托色尼筛选法)
2015-06-26 21:27
337 查看
class Solution { public: int countPrimes(int n) { bool* isPrime =new bool ; memset(isPrime,1,n); for(int i=2; i*i<n; i++) { if(!isPrime[i]) continue; for(int j=i*i; j<n; j+=i) isPrime[j]=0; } int cnt=0; for(int i=2; i<n; i++) if(isPrime[i]) cnt++; return cnt; } };
AC代码
相关文章推荐
- 关于基本数据类型转换的一个小问题
- sgu139: Help Needed!
- echop JS Library
- android光传感器数据的获取及储存
- Java基础之集合2
- 树莓派学习笔记——修改树莓派软件源
- 【音乐分享】Let Me Go
- 冒泡算法
- 如何搭建搭建各个版本的openstack的本地yum源?
- [C++]水仙花数
- Hibernate入门教程
- >/dev/null 2>&1 与 2>&1 >/dev/null 的区别及使用技巧
- Message Flood
- poj 3468 线段树 成段增减 区间求和
- HDU0174 Doing Homework ——状压dp
- 物联网系统的搭建(八)
- 深度学习(二)theano环境搭建
- 小记
- 【思维突破】——撑过最难的阶段,接下来就是海阔天空
- 【Codeforces69E】Subsegments