首页 > 其他 > 详细

Count Primes

时间:2015-05-06 12:45:53      阅读:85      评论:0      收藏:0      [点我收藏+]

Count Primes

问题:

Count the number of prime numbers less than a non-negative number, n

思路:

  计算质数的方法:http://en.wikipedia.org/wiki/Prime_number

我的代码:

技术分享
public class Solution {
    public int countPrimes(int n) {
        int count=0;
        boolean[] nums = new boolean[n];
        for(int i=2; i<nums.length; i++){
            if(!nums[i]){
                count++;
                for(int j=2*i; j<nums.length; j=j+i){
                        nums[j] = true;
                }
            }
        }
        return count;
    }
}
View Code

学习之处:

  • 计算质数的方法
  • 如何计算质数的数量,代码简单而且有效

Count Primes

原文:http://www.cnblogs.com/sunshisonghit/p/4481380.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!