mkp224o/calcest.c

49 lines
1 KiB
C
Raw Normal View History

2020-01-14 14:15:44 -03:00
#include <stdio.h>
2020-05-23 12:41:51 -04:00
#include <stddef.h>
2020-01-14 14:15:44 -03:00
#include <math.h>
/*
* as per scribblemaniac's explanation:
* t - number of trials
* n - character count
* p - probability
* condition: >=1 matches
* formula: t = log(1-p)/log(1-1/32^n)
* comes from:
* distribution X~Binomial(t, 1/32^n)
* P(X>=1)=p
*/
const double probs[] = { 0.5, 0.8, 0.9, 0.95, 0.99 };
2021-11-02 21:20:43 -03:00
const int charcounts[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10 };
2020-01-14 14:15:44 -03:00
2021-11-02 21:20:43 -03:00
int main(int argc,char **argv)
2020-01-14 14:15:44 -03:00
{
2022-05-17 12:01:52 -04:00
// TODO
(void) argc;
(void) argv;
2021-11-02 21:20:43 -03:00
2020-01-14 14:15:44 -03:00
printf(" |");
2020-05-23 12:41:51 -04:00
for (size_t i = 0; i < sizeof(probs)/sizeof(probs[0]); ++i) {
2021-11-02 21:20:43 -03:00
printf(" %15d%% |",(int)((probs[i]*100)+0.5));
2020-01-14 14:15:44 -03:00
}
printf("\n");
printf("---+");
2020-05-23 12:41:51 -04:00
for (size_t i = 0; i < sizeof(probs)/sizeof(probs[0]); ++i) {
2021-11-02 21:20:43 -03:00
printf("------------------+");
2020-01-14 14:15:44 -03:00
}
printf("\n");
2020-05-23 12:41:51 -04:00
for (size_t i = 0; i < sizeof(charcounts)/sizeof(charcounts[0]); ++i) {
2020-01-14 14:15:44 -03:00
printf("%2d |",charcounts[i]);
2020-05-23 12:41:51 -04:00
for (size_t j = 0; j < sizeof(probs)/sizeof(probs[0]); ++j) {
2020-01-14 14:15:44 -03:00
double t = log2(1 - probs[j]) / log2(1 - (1 / pow(32,charcounts[i])));
2021-11-02 21:20:43 -03:00
printf(" %16.0f |",t);
2020-01-14 14:15:44 -03:00
}
printf("\n");
}
return 0;
}