#include // Visitor pattern. class Visitor { public: virtual void Visit(int value) = 0; virtual ~Visitor() {} }; // Detects record values. class Recorder : public Visitor { public: Recorder() : n(0), max(0) {} void Visit(int value) { n++; an = value; if (max < value) { max = value; printf("%d %lld\n", max, n); fflush(stdout); } } ~Recorder() { fprintf(stderr, "*** Recorder stopped at a(%lld)=%d\n", n, an); } private: long long n; // n int an; // a(n) int max; // max value seen so far }; // Produces runs of the sequence A281579. class Producer : public Visitor { public: Producer(Visitor *client) : client(client), an(2), pd(2) { // Seed: a(1)=2. client->Visit(an); } void Visit(int value) { int d = -((an-2)/(value-1)); if (d==pd) { d++; } for (int i=2; i<=value; i++) { an += d; client->Visit(an); } pd = d; } ~Producer() { delete client; } private: Visitor *client; // client int an; // current value int pd; // previous common difference }; #define DEPTH 42 int main() { Visitor *visitor = new Recorder; // stacking for (int d=1; d<=DEPTH; d++) { fprintf(stderr, "************* At depth %d\n", d); visitor = new Producer(visitor); } delete visitor; return 0; }