-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path204.cpp
More file actions
40 lines (37 loc) · 814 Bytes
/
204.cpp
File metadata and controls
40 lines (37 loc) · 814 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// 204.cpp
// LeetCode
//
// Created by 张佐玮 on 15/8/3.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Count Primes
//
#include <iostream>
#include <math.h>
using namespace std;
class Solution {
public:
int countPrimes(int n) {
bool isPrime[n];
memset(isPrime, 1, sizeof(bool) * n);
int index = 2;
while (index <= sqrt(n)) {
if (!isPrime[index]) {
index++;
continue;
}
for (int i = index; i <= n / index; i++) {
isPrime[i*index] = false;
}
index++;
}
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i]) {
count++;
}
}
return count;
}
};