calcest.c 1.0 KB

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