File tree 3 files changed +75
-0
lines changed
main/java/com/packt/datastructuresandalg/lesson7/activity/sieve
test/java/com/packt/datastructuresandalg/lesson7/activity/sieve
3 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .packt .datastructuresandalg .lesson7 .activity .sieve ;
2
+
3
+ public class SieveOfEratosthenes {
4
+ public SieveOfEratosthenes (int maxValue ) {
5
+ // Build the sieve here
6
+ }
7
+
8
+ public boolean isPrime (int value ) {
9
+ return false ;
10
+ }
11
+ }
Original file line number Diff line number Diff line change
1
+ package com .packt .datastructuresandalg .lesson7 .activity .sieve .solution ;
2
+
3
+ import java .util .Arrays ;
4
+
5
+ public class SieveOfEratosthenes {
6
+ private boolean [] prime ;
7
+
8
+ public SieveOfEratosthenes (int maxValue ) {
9
+ prime = new boolean [maxValue + 1 ];
10
+ Arrays .fill (prime , true );
11
+ prime [0 ] = prime [1 ] = false ;
12
+ int l = (int ) Math .sqrt (maxValue );
13
+ for (int i = 2 ; i <= l ; i ++) {
14
+ if (prime [i ]) {
15
+ for (int j = i * i ; j <= maxValue ; j += i ) {
16
+ prime [j ] = false ;
17
+ }
18
+ }
19
+ }
20
+ }
21
+
22
+ public boolean isPrime (int value ) {
23
+ return prime [value ];
24
+ }
25
+ }
Original file line number Diff line number Diff line change
1
+ package com .packt .datastructuresandalg .lesson7 .activity .sieve ;
2
+
3
+ import junit .framework .TestCase ;
4
+
5
+ public class SieveOfEratosthenesTest extends TestCase {
6
+ SieveOfEratosthenes sieve = new SieveOfEratosthenes (1000000 );
7
+
8
+ public void test0 () {
9
+ assertFalse (sieve .isPrime (0 ));
10
+ }
11
+
12
+ public void test1 () {
13
+ assertFalse (sieve .isPrime (1 ));
14
+ }
15
+
16
+ public void test2 () {
17
+ assertTrue (sieve .isPrime (2 ));
18
+ }
19
+
20
+ public void test289049 () {
21
+ assertTrue (sieve .isPrime (289049 ));
22
+ }
23
+
24
+ public void test690997 () {
25
+ assertTrue (sieve .isPrime (690997 ));
26
+ }
27
+
28
+ public void test690998 () {
29
+ assertFalse (sieve .isPrime (690998 ));
30
+ }
31
+
32
+ public void test999983 () {
33
+ assertTrue (sieve .isPrime (999983 ));
34
+ }
35
+
36
+ public void test999999 () {
37
+ assertFalse (sieve .isPrime (999999 ));
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments