## 204. Count Primes

Description:

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

Source Code（C++）:

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

class Solution {
public:
int countPrimes(int n) {
if (n<2){
return 0;
}
vector<int> v(n, 1);
v.at(0)=0;v.at(1)=0;
for(int i=2; i<sqrt((float)n); i++) {
if(v.at(i)==1){
for (int j=2; i*j<n; j++){
v.at(i*j)=0;
}
}
}
int prime_counts=0;
for (int i=0; i<n; i++){
if (v.at(i)==1){
prime_counts++;
}
}
return prime_counts;
}
};

int main() {
Solution sol;
cout << sol.countPrimes(0);
cout << sol.countPrimes(100);
return 0;
}

#### LeetCode:Count Primes

2015-05-11 15:56:28

#### leetcode 204: Count Primes

2015-04-29 08:33:10

#### leetcode--Count Primes

2016-03-08 14:40:03

#### 204. Count Primes 统计素数

2016-06-25 11:13:28

#### 【Leetcode】Count Primes

2016-06-01 20:09:54

#### Leetcode 204题 Count Primes

2015-11-24 13:05:41

#### Leetcode题目总结-Math-如何判断素数？

2017-08-13 11:58:01

#### 开始刷题leetcode day6：Count Primes

2015-05-07 13:04:39

#### 经典算法题之求小于N的所有素数

2015-05-03 22:44:47

#### [leetcode]Count Primes C语言

2015-08-30 14:44:11

## 不良信息举报

204. Count Primes