int maxx,maxy;
int max;
boolean[] sieve;

void plot(int pos, boolean active) {
  set(pos%maxx,pos/maxx, active?#000000:#ffffff);
}

void setup() {
  size(1000, 1000, P2D);
  frameRate(2);
  maxx=width;
  maxy=height;
  max=width*height;
  sieve=new boolean[max+1];

  sieve[1]=false;
  plot(0,false);
  plot(1,false);
  for(int i=2;i<=max;i++) {
    sieve[i]=true;
    plot(i,true);
  }
}

int i=2;

void draw() {
  if(!sieve[i]) {
    while(i*i<max && !sieve[i]) {
      i++;
    }
  }
  if(sieve[i]) {
    print(i+" ");
    for(int j=i*i;j<=max;j+=i) {
      if(sieve[j]) {
        sieve[j]=false;
        plot(j,false);
      }
    }
  }
  if(i*i<max) {
    i++;
  } else {
    noLoop();
    println("finished");
  }
}
