File tree 1 file changed +11
-3
lines changed
src/main/java/com/fishercoder/solutions/firstthousand
1 file changed +11
-3
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions .firstthousand ;
2
2
3
+ import java .util .Arrays ;
4
+
3
5
public class _204 {
4
6
public static class Solution1 {
5
7
/**
@@ -9,13 +11,19 @@ public static class Solution1 {
9
11
* starting from 2, then all remaining ones are prime.
10
12
*/
11
13
public int countPrimes (int n ) {
12
- boolean [] notPrimes = new boolean [n ];
14
+ if (n <= 1 ) {
15
+ return 0 ;
16
+ }
17
+ boolean [] isPrime = new boolean [n ];
18
+ Arrays .fill (isPrime , true );
19
+ isPrime [0 ] = false ;
20
+ isPrime [1 ] = false ;
13
21
int count = 0 ;
14
22
for (int i = 2 ; i < n ; i ++) {
15
- if (! notPrimes [i ]) {
23
+ if (isPrime [i ]) {
16
24
count ++;
17
25
for (int j = 2 ; i * j < n ; j ++) {
18
- notPrimes [i * j ] = true ;
26
+ isPrime [i * j ] = false ;
19
27
}
20
28
}
21
29
}
You can’t perform that action at this time.
0 commit comments