Finished Arrays Sieve
This commit is contained in:
@@ -19,49 +19,16 @@ class Sieve implements Iterator<Integer> {
|
|||||||
this.lim = lim;
|
this.lim = lim;
|
||||||
this.isntPrime = new boolean[lim + 1];
|
this.isntPrime = new boolean[lim + 1];
|
||||||
|
|
||||||
int i = 2;
|
for (int i = 2; i <= lim; i++)
|
||||||
while (i < this.lim) {
|
if (!isntPrime[i]) {
|
||||||
for (int j = i; j < this.lim; j += i) {
|
int mult = i + i; // Start with next multiple of i.
|
||||||
System.out.println(j);
|
while (mult <= lim) {
|
||||||
this.isntPrime[j] = true;
|
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 boolean hasNext() { return this.cur < this.lim; }
|
||||||
|
|
||||||
public Integer next() {
|
public Integer next() {
|
||||||
@@ -79,6 +46,7 @@ class Examples {
|
|||||||
t.checkExpect(sieve.next(), 5);
|
t.checkExpect(sieve.next(), 5);
|
||||||
t.checkExpect(sieve.next(), 7);
|
t.checkExpect(sieve.next(), 7);
|
||||||
t.checkExpect(sieve.next(), 11);
|
t.checkExpect(sieve.next(), 11);
|
||||||
|
t.checkExpect(sieve.hasNext(), true);
|
||||||
t.checkExpect(sieve.next(), 13);
|
t.checkExpect(sieve.next(), 13);
|
||||||
t.checkExpect(sieve.next(), 17);
|
t.checkExpect(sieve.next(), 17);
|
||||||
t.checkExpect(sieve.next(), 19);
|
t.checkExpect(sieve.next(), 19);
|
||||||
|
@@ -178,7 +178,6 @@ class BFT<X> implements Iterator<X> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
class TreeFinder {
|
class TreeFinder {
|
||||||
// Know the first element of the preorder list will be the root node of the
|
// 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
|
// 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
|
// subtree, the ones to the right on the right. Repeat the process on the
|
||||||
// subtreees.
|
// subtreees.
|
||||||
|
|
||||||
|
// Learned you can do static methods with a type like this :).
|
||||||
static <T> BT<T> find(ArrayList<T> inorder, ArrayList<T> preorder) {
|
static <T> BT<T> find(ArrayList<T> inorder, ArrayList<T> preorder) {
|
||||||
int inorderRootIdx = inorder.indexOf(preorder.getFirst());
|
int inorderRootIdx = inorder.indexOf(preorder.getFirst());
|
||||||
System.out.println(inorderRootIdx);
|
System.out.println(inorderRootIdx);
|
||||||
@@ -238,7 +238,6 @@ class TreeFinder {
|
|||||||
return new Node<>(val, leftTree, rightTree);
|
return new Node<>(val, leftTree, rightTree);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
class Examples {
|
class Examples {
|
||||||
BT<Integer> tree;
|
BT<Integer> tree;
|
||||||
@@ -298,7 +297,6 @@ class Examples {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
void testTreeFinder(Tester t) {
|
void testTreeFinder(Tester t) {
|
||||||
init();
|
init();
|
||||||
|
|
||||||
@@ -315,5 +313,4 @@ class Examples {
|
|||||||
|
|
||||||
t.checkExpect(found, tree);
|
t.checkExpect(found, tree);
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user