Skip to content

Commit 9b62138

Browse files
update 204
1 parent 123d307 commit 9b62138

File tree

2 files changed

+14
-8
lines changed
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

2 files changed

+14
-8
lines changed

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
public class _204 {
44
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+
*/
511
public int countPrimes(int n) {
6-
boolean[] notPrime = new boolean[n];
12+
boolean[] notPrimes = new boolean[n];
713
int count = 0;
814
for (int i = 2; i < n; i++) {
9-
if (!notPrime[i]) {
15+
if (!notPrimes[i]) {
1016
count++;
1117
for (int j = 2; i * j < n; j++) {
12-
notPrime[i * j] = true;
18+
notPrimes[i * j] = true;
1319
}
1420
}
1521
}

Diff for: src/test/java/com/fishercoder/firstthousand/_204Test.java

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

33
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;
66

7-
import static org.junit.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
public class _204Test {
1010
private static _204.Solution1 solution1;
1111

12-
@BeforeClass
13-
public static void setup() {
12+
@BeforeEach
13+
public void setup() {
1414
solution1 = new _204.Solution1();
1515
}
1616

0 commit comments

Comments
 (0)