Skip to content

Commit 67e4532

Browse files
Merge pull request #9 from InnocentDaksh63/patch-1
Create 204_count_primes.java
2 parents d4a3ec0 + 4e108e1 commit 67e4532

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

204_count_primes.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution{
2+
public int countPrimes(int n) {
3+
if(n <=1 ) return 0;
4+
5+
boolean[] notPrime = new boolean[n];
6+
notPrime[0] = true;
7+
notPrime[1] = true;
8+
9+
for(int i = 2; i < Math.sqrt(n); i++){
10+
if(!notPrime[i]){
11+
for(int j = 2; j*i < n; j++){
12+
notPrime[i*j] = true;
13+
}
14+
}
15+
}
16+
17+
int count = 0;
18+
for(int i = 2; i< notPrime.length; i++){
19+
if(!notPrime[i]) count++;
20+
}
21+
return count;
22+
}
23+
}

0 commit comments

Comments
 (0)