Skip to content

Commit cc5fc8c

Browse files
Implement prime counting function in countPrimes.java
1 parent 6d611f7 commit cc5fc8c

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

countPrimes.java

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

0 commit comments

Comments
 (0)