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

#define SQR(a) ((a) * (a))

#define FULL_WELL 500000
#define MAX_DN    4096

main()
{
    int dn, prev_dn;
    double k, x, n, scale, dn_center, min_dn, max_dn;

    scale = FULL_WELL / (double) MAX_DN;

    prev_dn = -1;

    /* set the max for the first bin */
    max_dn = 4096;
    
    for(k = FULL_WELL; k >= 1; k = n - sqrt(n)) {
	/* determine the center of the next bin */
	x = (-1.0 + sqrt(1.0 + (4.0 * k))) / 2.0;
	n = SQR(x);

	/* convert the solution to dn space */
	dn_center = n / scale;
	dn = dn_center;

	/*
	  Break out of the loop if the subdivision has become smaller
	  than a single dn value.
	*/
	if(dn == prev_dn) {
	    break;
	}

	/* determine the minimum value for this bin in dn space */
	min_dn = (n - x) / scale;
	
	printf("%11.4lf %8.4lf %9.4lf %9.4lf %9.4lf\n",
	       n, x, dn_center, min_dn, max_dn);
	
	prev_dn = dn;
	max_dn = min_dn;
    }

    /* subdivide the remaining space on intervals of one dn */
    for(dn--; dn >= 0; dn--) {
	n = ((dn + 0.5) * scale);
	x = sqrt(n);

	dn_center = dn + 0.5;
	
	min_dn = dn;
	
	printf("%11.4lf %8.4lf %9.4lf %9.4lf %9.4lf\n",
	       n, x, dn_center, min_dn, max_dn);
	
	max_dn = min_dn;
    }
}
