Repository navigation
Expand file tree
/
Copy pathLazyPrime.java
More file actions
executable file
·124 lines (119 loc) · 3.65 KB
/
Copy pathLazyPrime.java
File metadata and controls
executable file
·124 lines (119 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package lazyprimesdriver;
import java.util.*;
public class LazyPrime {
// Static fields
public static final int N = 100; // starting ArrayList size
public static int n = LazyPrime.N; // ArrayList size
// Sieve of Eratosthenes
public static ArrayList<Boolean> primes = new ArrayList<>(
Collections.nCopies( LazyPrime.N, true )
);
// Instance fields
public int j = 2;
public int max;
// Constructors
public LazyPrime( int max ) {
if( !LazyPrime.isInitialized() )
LazyPrime.init();
this.max = max;
}
public LazyPrime() {
this( 1000 );
}
// Static methods
public static void init() {
if( !isInitialized() ) {
primes.set( 0, false ); // initialized flag
}
}
public static boolean isInitialized() {
// During initialization we set 0 and 1 to false
return !primes.get(0);
}
// Return next prime using the Sieve of Eratosthenes, -1 for error
public static int sieve( int f ) {
// set all elements with indices divisable by f (current prime) to false
for( int j=f+f; j<n; j+=f ) {
if( primes.get(j) ) {
//System.out.println( " " +j + " " + f);
primes.set( j, false );
//System.out.println("remov:"+j);
}
}
//System.out.println(primes.get(f));
f++; // go to next element
while( f<n && !primes.get(f) )
f++;//System.out.println("notpr:"+ f++);
if( f<n )
return f;
else // We've reached capacity
return -1;
}
protected static boolean increaseCapacity() {
int j = 2;
try {
primes.addAll( Collections.nCopies( n, true ) ); // fill new candidates w/ true
n *= 2; // double max prime
} catch( Exception e ) {
return false;
}
while( 1 < j && j < n/2 ) {
j = LazyPrime.sieve( j );
//System.out.println(j);
}
return j > 1;
}
public static int next( int current ) {
if( !LazyPrime.isInitialized() )
LazyPrime.init();
if( current > 1 )
current = LazyPrime.sieve( current );
return current;
}
public static boolean hasNext( int current, int max ) {
if( !LazyPrime.isInitialized() )
LazyPrime.init();
int next = next( current );
if( next > 1 && next < max )
return true;
else
while( LazyPrime.n < current )
LazyPrime.increaseCapacity();
next = next( current );
if( next > 1 && next < max )
return true;
else
return false;
}
public static boolean isPrime( int num ) {
LazyPrime p;
if( num < 2 )
return false;
if( num == 2 )
return true;
if( num > LazyPrime.n ) {
p = new LazyPrime(num+1);
while( p.hasNext() )
if( p.next() == num )
return true;
return false;
} // need to loop through existing primes in sieve
return false;
}
// Instance methods
public boolean hasNext() {
if( this.j < this.max-1 )
return hasNext( this.j, this.max );
else
return false;
}
public int next() {
if( primes.get(1) ) { // If 1 is true, we haven't used 2 yet
primes.set( 1, false ); // set 1 to false
return 2; // return f (2)
}
if( j > 1 )
this.j = LazyPrime.sieve( this.j );
return this.j;
}
}