File tree 2 files changed +14
-8
lines changed
main/java/com/fishercoder/solutions/firstthousand
test/java/com/fishercoder/firstthousand
2 files changed +14
-8
lines changed Original file line number Diff line number Diff line change 2
2
3
3
public class _204 {
4
4
public static class Solution1 {
5
+ /**
6
+ * Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
7
+ * This is an ancient algorithm to efficiently count the number of primes up to a given point:
8
+ * it does so iteratively by marking composite (i.e. not prime) the multiples of each prime,
9
+ * starting from 2, then all remaining ones are prime.
10
+ */
5
11
public int countPrimes (int n ) {
6
- boolean [] notPrime = new boolean [n ];
12
+ boolean [] notPrimes = new boolean [n ];
7
13
int count = 0 ;
8
14
for (int i = 2 ; i < n ; i ++) {
9
- if (!notPrime [i ]) {
15
+ if (!notPrimes [i ]) {
10
16
count ++;
11
17
for (int j = 2 ; i * j < n ; j ++) {
12
- notPrime [i * j ] = true ;
18
+ notPrimes [i * j ] = true ;
13
19
}
14
20
}
15
21
}
Original file line number Diff line number Diff line change 1
1
package com .fishercoder .firstthousand ;
2
2
3
3
import com .fishercoder .solutions .firstthousand ._204 ;
4
- import org .junit .BeforeClass ;
5
- import org .junit .Test ;
4
+ import org .junit .jupiter . api . BeforeEach ;
5
+ import org .junit .jupiter . api . Test ;
6
6
7
- import static org .junit .Assert .assertEquals ;
7
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
8
8
9
9
public class _204Test {
10
10
private static _204 .Solution1 solution1 ;
11
11
12
- @ BeforeClass
13
- public static void setup () {
12
+ @ BeforeEach
13
+ public void setup () {
14
14
solution1 = new _204 .Solution1 ();
15
15
}
16
16
You can’t perform that action at this time.
0 commit comments