Finished Arrays Sieve

This commit is contained in:
2025-02-22 22:25:17 -05:00
parent aa1610bfbb
commit 031fc9c1a6
2 changed files with 9 additions and 44 deletions

View File

@@ -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);

View File

@@ -178,7 +178,6 @@ class BFT<X> implements Iterator<X> {
}
}
/*
class TreeFinder {
// Know the first element of the preorder list will be the root node of the
// tree. Assuming node values are unique, you can then locate that node in
@@ -186,6 +185,7 @@ class TreeFinder {
// subtree, the ones to the right on the right. Repeat the process on the
// subtreees.
// Learned you can do static methods with a type like this :).
static <T> BT<T> find(ArrayList<T> inorder, ArrayList<T> preorder) {
int inorderRootIdx = inorder.indexOf(preorder.getFirst());
System.out.println(inorderRootIdx);
@@ -238,7 +238,6 @@ class TreeFinder {
return new Node<>(val, leftTree, rightTree);
}
}
*/
class Examples {
BT<Integer> tree;
@@ -298,7 +297,6 @@ class Examples {
);
}
/*
void testTreeFinder(Tester t) {
init();
@@ -315,5 +313,4 @@ class Examples {
t.checkExpect(found, tree);
}
*/
}