Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;

public class Numbers implements Iterator<Integer> {
    int top, curr = 0;
    public Numbers(int n) { top = n; }

    public boolean hasNext() { return curr < top; }
    public Integer next() { return curr++; }
    public void remove() { throw new RuntimeException("NO"); }


    public static void main(String args[])
    {
        Iterator<Integer> it = new Numbers(100);
        it.next();
        it.next();

        while (it.hasNext()) {
            int i = it.next();
            System.out.println(i);

            it = new Filter(it, i);
        }
    }
}

class Filter implements Iterator<Integer> {
    Iterator<Integer> stream;
    Integer front;

    int factor;

    public Filter(Iterator<Integer> s, int f) { stream = s; factor = f; }

    public boolean hasNext()
    {
        if (front == null) {
            if (!stream.hasNext()) return false;

            front = stream.next();
        }

        while (front % factor == 0) {
            if (!stream.hasNext()) return false;
            front = stream.next();
        }

        return true;
    }

    public Integer next() 
    {
        if (!hasNext()) throw new RuntimeException("You are dumb");

        Integer rv = front;
        front = null;

        return rv;
    }

    public void remove() { throw new RuntimeException("NO"); }
}