首页 > 其他 > 详细

[Hackerrank] Prime Number Less Than N

时间:2015-01-09 01:28:35      阅读:224      评论:0      收藏:0      [点我收藏+]
static int getNumberOfPrimes(int N) {
        int n = N+1;//to include 0 as the first number for easy index operations later
        final boolean a[] = new boolean[n]; //Initialized to null by default. 
        Arrays.fill(a, true);  
        
        a[0] = false; 
        a[1] = false; 

        for (int i = 2; i < n / 2; ++i) 
        { 
            if (a[i]) 
            { 
                for (int j = i + i; j < n; j += i) 
                    a[j] = false;
            } 
        } 
        
        int count = 0;for(int i = 2;i<n;++i)
        {
            if(a[i])
            {
                //System.out.print(i+",");
                ++count;
            }
        }
        return count;
    }

 

[Hackerrank] Prime Number Less Than N

原文:http://www.cnblogs.com/neweracoding/p/4212278.html

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