LeetCode solutions by tgic

Count Primes

https://leetcode.com/problems/count-primes

Last Update 2015-04-27 08:40:41 +0000 View on Github

Source code Read on Github

 1 public class Solution {
 2     public int countPrimes(int n) {
 3 
 4         if(n < 2) return 0;
 5 
 6         BitSet b = new BitSet();
 7 
 8         b.set(0);
 9         b.set(1);
10 
11         for(int p = 2; p * 2 < n ; p = b.nextClearBit(p + 1)){
12             for(int i = 2; p * i < n ;i++){
13                 b.set(p * i);
14             }
15         }
16 
17         b.flip(0, n);
18 
19         return b.cardinality();
20     }
21 }
comments powered by Disqus

LeetCode solutions by tgic

  • LeetCode solutions by tgic
  • [email protected]
  • Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
  • tg123
  • tg123/leetcode

LeetCode java solutions by tgic