We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 876fab0 commit f18f8fdCopy full SHA for f18f8fd
src/main/java/com/fishercoder/solutions/firstthousand/_204.java
@@ -1,5 +1,7 @@
1
package com.fishercoder.solutions.firstthousand;
2
3
+import java.util.Arrays;
4
+
5
public class _204 {
6
public static class Solution1 {
7
/**
@@ -9,13 +11,19 @@ public static class Solution1 {
9
11
* starting from 2, then all remaining ones are prime.
10
12
*/
13
public int countPrimes(int n) {
- 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;
21
int count = 0;
22
for (int i = 2; i < n; i++) {
- if (!notPrimes[i]) {
23
+ if (isPrime[i]) {
24
count++;
25
for (int j = 2; i * j < n; j++) {
- notPrimes[i * j] = true;
26
+ isPrime[i * j] = false;
27
}
28
29
0 commit comments