some tweaks
[mkp224o.git] / calcest.c
blob28d9c989b772dfe74bfe369072d60c2cc59814d5
1 #include <stdio.h>
2 #include <stddef.h>
3 #include <math.h>
5 /*
6 * as per scribblemaniac's explanation:
7 * t - number of trials
8 * n - character count
9 * p - probability
10 * condition: >=1 matches
11 * formula: t = log(1-p)/log(1-1/32^n)
12 * comes from:
13 * distribution X~Binomial(t, 1/32^n)
14 * P(X>=1)=p
17 const double probs[] = { 0.5, 0.8, 0.9, 0.95, 0.99 };
18 const int charcounts[] = { 2, 3, 4, 5, 6, 7 };
20 int main(void)
22 printf(" |");
23 for (size_t i = 0; i < sizeof(probs)/sizeof(probs[0]); ++i) {
24 printf(" %11d%% |",(int)((probs[i]*100)+0.5));
26 printf("\n");
28 printf("---+");
29 for (size_t i = 0; i < sizeof(probs)/sizeof(probs[0]); ++i) {
30 printf("--------------+");
32 printf("\n");
34 for (size_t i = 0; i < sizeof(charcounts)/sizeof(charcounts[0]); ++i) {
35 printf("%2d |",charcounts[i]);
36 for (size_t j = 0; j < sizeof(probs)/sizeof(probs[0]); ++j) {
37 double t = log2(1 - probs[j]) / log2(1 - (1 / pow(32,charcounts[i])));
38 printf(" %12.0f |",t);
40 printf("\n");
43 return 0;