-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCountPrimes.java
51 lines (43 loc) · 1.03 KB
/
CountPrimes.java
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
41
42
43
44
45
46
47
48
49
50
51
class Solution {
public int countPrimes(int n) {
boolean[] primes = new boolean[n];
for(int i=2; i*i<n ; i++){
if(!primes[i]){
for(int j = i*i; j<n; j+=i){
primes[j] = true;
}
}
}
int count =0;
for(int i = 2; i <n; i++){
if(!primes[i]){
count++;
}
}
return count;
}
}
/*
class Solution {
public int countPrimes(int n) {
boolean[] primes = new boolean[n];
for(int i=0; i<n; i++){
primes[i] = true;
}
for(int i=2; i*i<n; i++){
if(primes[i]){
for(int j=i; j*i<n; j++){
primes[i*j] = false;
}
}
}
int primeCount = 0;
for(int i=2; i<primes.length; i++){
if(primes[i]){
primeCount++;
}
}
return primeCount;
}
}
*/