#include <stdio.h>
#include <math.h>

struct point {
  double x, y;
};


double pig_sqrt(double v)
{
  double ret=v/3.;
  int c;

  for(c=100; c; c--)
    ret=.5*(v+v/ret);

  return ret;
}


int ismandel(double x, double y)
{
  double a=x, b=y, xx, yy, r;
  int c;

  //  printf("called for point (%f, %f)\n", x, y);

  for(c=100; c; c--) {
    xx=x*x-y*y-a;
    yy=2.*x*y-b;
    x=xx; y=yy;
    r=x*x+y*y;
    if(r>120.)
      break;
  }

  //  printf("R: %f\t sqrt(r): %f\n", r, pig_sqrt(r));

  return fabs(pig_sqrt(r))<4.;
}


int main(void)
{
  double x, y, g;
  int c=0;

  struct point *ptr;

  ptr=(struct point*) malloc(16*400*sizeof(struct point));

  if(!ptr) {
    fprintf(stderr, "Error allocating RAM!\n");
    return -1;
  }
  
  for(g=1; g>.02; g-=0.0005) {
    c=0;
    //    printf("granularity: %f\n", g);
    for(x=-2.; x<2.; x+=g)
      for(y=-2.; y<2; y+=g)
        if(ismandel(x, y))
          ptr[c].x=x; ptr[c++].y=y;
    //    printf("Last c value: %d\n", c);
  }

  return 0;
}

          
