Finished Arrays Sieve
This commit is contained in:
@@ -19,49 +19,16 @@ class Sieve implements Iterator<Integer> {
|
||||
this.lim = lim;
|
||||
this.isntPrime = new boolean[lim + 1];
|
||||
|
||||
int i = 2;
|
||||
while (i < this.lim) {
|
||||
for (int j = i; j < this.lim; j += i) {
|
||||
System.out.println(j);
|
||||
this.isntPrime[j] = true;
|
||||
for (int i = 2; i <= lim; i++)
|
||||
if (!isntPrime[i]) {
|
||||
int mult = i + i; // Start with next multiple of i.
|
||||
while (mult <= lim) {
|
||||
isntPrime[mult] = true;
|
||||
mult += i;
|
||||
}
|
||||
}
|
||||
this.isntPrime[i] = false;
|
||||
while (i < this.lim && this.isntPrime[i]) i++;
|
||||
}
|
||||
|
||||
/*
|
||||
this.isntPrime[4] = true;
|
||||
this.isntPrime[6] = true;
|
||||
this.isntPrime[8] = true;
|
||||
this.isntPrime[9] = true;
|
||||
this.isntPrime[10] = true;
|
||||
this.isntPrime[12] = true;
|
||||
this.isntPrime[14] = true;
|
||||
this.isntPrime[15] = true;
|
||||
this.isntPrime[16] = true;
|
||||
this.isntPrime[18] = true;
|
||||
this.isntPrime[20] = true;
|
||||
this.isntPrime[21] = true;
|
||||
this.isntPrime[22] = true;
|
||||
this.isntPrime[24] = true;
|
||||
this.isntPrime[25] = true;
|
||||
this.isntPrime[26] = true;
|
||||
this.isntPrime[27] = true;
|
||||
this.isntPrime[28] = true;
|
||||
*/
|
||||
|
||||
/*
|
||||
for (int i = this.cur; i <= this.lim; i += this.cur) {
|
||||
this.isntPrime[i] = true;
|
||||
}
|
||||
|
||||
for (int i = this.cur; !this.isntPrime[i]; i++) {}
|
||||
*/
|
||||
}
|
||||
|
||||
// [ false false true true false true false true ]
|
||||
// [ 0 1 2 3 4 5 6 7 ]
|
||||
|
||||
public boolean hasNext() { return this.cur < this.lim; }
|
||||
|
||||
public Integer next() {
|
||||
@@ -79,6 +46,7 @@ class Examples {
|
||||
t.checkExpect(sieve.next(), 5);
|
||||
t.checkExpect(sieve.next(), 7);
|
||||
t.checkExpect(sieve.next(), 11);
|
||||
t.checkExpect(sieve.hasNext(), true);
|
||||
t.checkExpect(sieve.next(), 13);
|
||||
t.checkExpect(sieve.next(), 17);
|
||||
t.checkExpect(sieve.next(), 19);
|
||||
|
Reference in New Issue
Block a user