Piet van Dongen 11 лет назад
Родитель
Сommit
0cec49a6c8

+ 2
- 1
config.json Просмотреть файл

@@ -28,7 +28,8 @@
28 28
     "accumulate",
29 29
     "crypto-square",
30 30
     "trinary",
31
-    "rna-transcription"
31
+    "rna-transcription",
32
+    "sieve"
32 33
   ],
33 34
   "deprecated": [
34 35
   ],

+ 11
- 0
sieve/build.gradle Просмотреть файл

@@ -0,0 +1,11 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.10"
11
+}

+ 37
- 0
sieve/example.java Просмотреть файл

@@ -0,0 +1,37 @@
1
+import java.util.ArrayList;
2
+import java.util.LinkedList;
3
+import java.util.List;
4
+import java.util.stream.Collectors;
5
+import java.util.stream.IntStream;
6
+
7
+public class Sieve {
8
+
9
+    private int maximalPrime;
10
+    private List<Integer> primes;
11
+
12
+    public Sieve(int maximalPrime) {
13
+        this.maximalPrime = maximalPrime;
14
+        this.primes = calculatePrimes();
15
+    }
16
+
17
+    public List<Integer> getPrimes() {
18
+        return primes;
19
+    }
20
+
21
+    private List<Integer> calculatePrimes() {
22
+        List<Integer> primes = new ArrayList<>();
23
+        LinkedList<Integer> candidates = IntStream.range(2, maximalPrime + 1)
24
+                .boxed()
25
+                .collect(Collectors.toCollection(LinkedList::new));
26
+
27
+        while (candidates.size() > 0) {
28
+            Integer prime = candidates.remove();
29
+            primes.add(prime);
30
+            candidates = candidates.stream()
31
+                    .filter(x -> x % prime != 0)
32
+                    .collect(Collectors.toCollection(LinkedList::new));
33
+        }
34
+
35
+        return primes;
36
+    }
37
+}

+ 0
- 0
sieve/src/main/java/.keep Просмотреть файл


+ 2
- 0
sieve/src/main/java/Sieve.java Просмотреть файл

@@ -0,0 +1,2 @@
1
+public class Sieve {
2
+}

+ 41
- 0
sieve/src/test/java/SieveTest.java Просмотреть файл

@@ -0,0 +1,41 @@
1
+import org.junit.Test;
2
+
3
+import java.util.Arrays;
4
+import java.util.List;
5
+
6
+import static org.junit.Assert.assertEquals;
7
+
8
+public class SieveTest {
9
+
10
+    @Test
11
+    public void findFirstPrime() {
12
+        Sieve sieve = new Sieve(2);
13
+        List<Integer> expectedOutput = Arrays.asList(new Integer[]{2});
14
+
15
+        assertEquals(expectedOutput, sieve.getPrimes());
16
+    }
17
+
18
+    @Test
19
+    public void findPrimesUpTo10() {
20
+        Sieve sieve = new Sieve(10);
21
+        List<Integer> expectedOutput = Arrays.asList(2, 3, 5, 7);
22
+
23
+        assertEquals(expectedOutput, sieve.getPrimes());
24
+    }
25
+
26
+    @Test
27
+    public void findPrimesUpTo1000() {
28
+        Sieve sieve = new Sieve(1000);
29
+        List<Integer> expectedOutput = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61,
30
+                67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173,
31
+                179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283,
32
+                293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421,
33
+                431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563,
34
+                569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683,
35
+                691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
36
+                839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983,
37
+                991, 997);
38
+
39
+        assertEquals(expectedOutput, sieve.getPrimes());
40
+    }
41
+}