标签:
Count the number of prime numbers less than a non-negative number, n
1 public int countPrimes(int n) { 2 3 ArrayList<Integer> primes = new ArrayList<Integer>(); 4 5 if (n <= 3) return Math.max(0, n - 1); 6 7 primes.add(2); 8 primes.add(3); 9 10 for (int i = 4; i <= n; i++) { 11 boolean isPrime = true; 12 for (int p : primes) { 13 int m = i % p; 14 if (m == 0) { 15 isPrime = false; 16 break; 17 } 18 } 19 20 if (isPrime) { 21 primes.add(i); 22 } 23 } 24 25 return primes.size(); 26 }
标签:
原文地址:http://www.cnblogs.com/beiyeqingteng/p/5722653.html