Skip to content

Commit f18f8fd

Browse files
update 204
1 parent 876fab0 commit f18f8fd

File tree

1 file changed

+11
-3
lines changed
  • src/main/java/com/fishercoder/solutions/firstthousand

1 file changed

+11
-3
lines changed

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_204.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions.firstthousand;
22

3+
import java.util.Arrays;
4+
35
public class _204 {
46
public static class Solution1 {
57
/**
@@ -9,13 +11,19 @@ public static class Solution1 {
911
* starting from 2, then all remaining ones are prime.
1012
*/
1113
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;
1321
int count = 0;
1422
for (int i = 2; i < n; i++) {
15-
if (!notPrimes[i]) {
23+
if (isPrime[i]) {
1624
count++;
1725
for (int j = 2; i * j < n; j++) {
18-
notPrimes[i * j] = true;
26+
isPrime[i * j] = false;
1927
}
2028
}
2129
}

0 commit comments

Comments
 (0)