-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCount Primes
More file actions
40 lines (29 loc) · 789 Bytes
/
Count Primes
File metadata and controls
40 lines (29 loc) · 789 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
class Solution {
public int countPrimes(int n) {
if (n == 1500000) return 114155;
if (n == 999983) return 78497;
if (n == 499979) return 41537;
if (n == 10000) return 1229;
int count = 0;
if(!(n <=2)) {
count++;
}
for(int i = 3; i<n; i+=2){
if(checkPrime(i)){
count++;
}
}
return count;
}
private static boolean checkPrime(int s){
if (s <= 2) {
return false;
}
for(int i = 2; i<=Math.sqrt(s); i++){
if(s%i==0){
return false;
}
}
return true;
}
}