Initial commit for 4.2
parent
801008236f
commit
7cdfc0e9fb
@ -0,0 +1,9 @@
|
|||||||
|
package _4._2;
|
||||||
|
|
||||||
|
public abstract class Filter implements Sequence {
|
||||||
|
protected Sequence sequence;
|
||||||
|
|
||||||
|
public Filter(Sequence sequence) {
|
||||||
|
this.sequence = sequence;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,10 @@
|
|||||||
|
package _4._2;
|
||||||
|
|
||||||
|
public class Naturals implements Sequence {
|
||||||
|
private int last;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNext() {
|
||||||
|
return ++this.last;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
package _4._2;
|
||||||
|
|
||||||
|
public class Primes extends Filter {
|
||||||
|
public Primes() {
|
||||||
|
super(new Naturals());
|
||||||
|
this.sequence.getNext(); // remove 1 form the sequence
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNext() {
|
||||||
|
int ret = this.sequence.getNext(); // also removes ret form the sequence
|
||||||
|
this.sequence = new ZapMultiples(ret, this.sequence);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
package _4._2;
|
||||||
|
|
||||||
|
public interface Sequence {
|
||||||
|
int getNext();
|
||||||
|
}
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
package _4._2;
|
||||||
|
|
||||||
|
public class ZapMultiples extends Filter {
|
||||||
|
private final int divisor;
|
||||||
|
|
||||||
|
public ZapMultiples(int divisor, Sequence sequence) {
|
||||||
|
super(sequence);
|
||||||
|
if(divisor < 2) {
|
||||||
|
throw new IllegalArgumentException("divisor must be >= 2");
|
||||||
|
}
|
||||||
|
this.divisor = divisor;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNext() {
|
||||||
|
int next = this.sequence.getNext();
|
||||||
|
return next % divisor == 0 ? this.getNext() : next;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue