码迷,mamicode.com
首页 > 其他好文 > 详细

[Hackerrank] Prime Number Less Than N

时间:2015-01-09 01:31:29      阅读:458      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!