Well some things kinda work I guess.

This commit is contained in:
Jacob Signorovitch
2025-02-18 09:35:29 -05:00
parent 67683a7ef9
commit 6ef71e851c

View File

@@ -7,7 +7,7 @@ import tester.Tester;
class Sieve implements Iterator<Integer> {
int cur; // The current number.
int lim; // The largest number to check.
boolean[] isntPrime; // Where true, not a prime.
boolean[] isntPrime; // One boolean for each number 0 to lim.
Sieve(int lim) {
if (lim < 2)
@@ -19,7 +19,24 @@ class Sieve implements Iterator<Integer> {
this.lim = lim;
this.isntPrime = new boolean[lim + 1];
this.isntPrime[3] = true;
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) {
@@ -30,23 +47,20 @@ class Sieve implements Iterator<Integer> {
*/
}
// [ false false false false false false false ]
// [ 1 2 3 4 5 6 7 ]
//
// [ false false false false false false false ]
// [ 1 2 3 4 5 6 7 ]
// [ false false true true false true false true ]
// [ 0 1 2 3 4 5 6 7 ]
public boolean hasNext() { return true; }
public boolean hasNext() { return this.cur < this.lim; }
public Integer next() {
while (this.isntPrime[this.cur]) this.cur++;
return this.cur;
return this.cur++;
}
}
class Examples {
void testSieve(Tester t) {
Sieve sieve = new Sieve(10);
Sieve sieve = new Sieve(29);
t.checkExpect(sieve.hasNext(), true);
t.checkExpect(sieve.next(), 2);
t.checkExpect(sieve.next(), 3);
@@ -58,6 +72,6 @@ class Examples {
t.checkExpect(sieve.next(), 19);
t.checkExpect(sieve.next(), 23);
t.checkExpect(sieve.next(), 29);
t.checkExpect(sieve.next(), false);
t.checkExpect(sieve.hasNext(), false);
}
}