// Probability that n bit number has less than k bits when P(1) = f. // Print from k = 0 thru n. #include #include #include typedef double R; typedef long unsigned int L; int main(int argc, char * * argv){ if(argc != 3) exit(printf("int and float args %d\n", argc)); int n = atoi(argv[1]); R f1 = atof(argv[2]), f0 = 1.-f1, fr = f1/f0, sm = 0; R cc = 0; {R t = 1, cn = 1; {int j=n; while(j--) {t *= f0; cn *= 0.5;}} int g = n, h = 0; if(t == 0.) exit(printf("Underflow\n")); {int j = n+1, j1=1; while(j--) { printf("%3d %8.5f %8.5f %8.5f %4.1f\n", h, t, sm, cc, log((0x1P64)*cc)/log(2)); cc += cn; cn *= j; cn /= j1++; sm += t; ++h; t *= fr*g/h; --g;}}} printf("ultimately: %9.7f %9.7f\n", sm, cc); return 0;}