204. Count Primes
2016-04-18 15:47
330 查看
除了C语言课本里方法,另一种求质数的算法:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
给出的参考代码:
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
给出的参考代码:
public int countPrimes(int n) { boolean[] isPrime = new boolean ; for (int i = 2; i < n; i++) { isPrime[i] = true; } // Loop's ending condition is i * i < n instead of i < sqrt(n) // to avoid repeatedly calling an expensive function sqrt(). for (int i = 2; i * i < n; i++) { if (!isPrime[i]) continue; for (int j = i * i; j < n; j += i) { isPrime[j] = false; } } int count = 0; for (int i = 2; i < n; i++) { if (isPrime[i]) count++; } return count; }
相关文章推荐
- 帧动画
- 从源码角度理清memcache缓存服务
- java实现FTP文件上传与文件下载
- android studio百度地图无法定位问题4.9E324
- ‘ORA-00904: "AGE": invalid identifier’ The association between double quote and case sensitivity
- 【自用】UITableView每个cell高度自适应
- Redis及其应用
- MySQL存储过程
- 一个自定义服务以执行脚本实现类属性的快速整齐地排列
- TestNG Parameters and DataProvider
- linux命令详解——top
- linux命令详解——top
- 分享一下学习css,js心得
- LeftBarbuttonItem左滑返回&UINavigationBar透明
- 各种文字特效
- 欢迎使用CSDN-markdown编辑器
- iOS 面试题小总结
- 排序专题
- opencv for android 教程(环境搭建篇)
- Android Studio你不知道的调试技巧