fork download
  1. /*
  2.   Calculating the probability for a random testing target selection
  3.   strategy to achieve the same or better accuracy than a given selection.
  4.  
  5.   Requires:
  6.   (GNU) bc
  7.  
  8.   Usage Example:
  9.   echo "prob(450,39,85,34)" | bc -l prob.l
  10. */
  11.  
  12. # To count the number of leading zeros:
  13. # echo "prob(450,39,85,34)" | bc -l prob.l | sed -n "s/\.\(0\+\)\([1-9][0-9]*\)*.*/\1/p" | wc -c
  14.  
  15. scale=100
  16.  
  17. define comb(n,k) {
  18. auto temp, i
  19. temp=1;
  20. if (k==0)
  21. return (1);
  22. for(i=1; i<=k; i++)
  23. temp=temp*(n+1-i)/i;
  24. return (temp);
  25. }
  26.  
  27. define prob(total,risky,recommended,actual_risky) {
  28. auto i, j, good, all, cases
  29. good=total-risky;
  30. cases=0;
  31. all=2^total;
  32. for(j=recommended; j>=actual_risky; j--) {
  33. for(i=actual_risky; i<=risky && i<=j; i++) {
  34. c1=comb(risky, i);
  35. c2=comb(good, j-i);
  36. cases=cases+c1*c2;
  37. }
  38. }
  39. return (cases/all);
  40. }
  41.  
  42. total = read()
  43. risky = read()
  44. recommend = read()
  45. actual = read()
  46. print "Probability=",prob(total,risky,recommend,actual),"\n"
  47.  
  48.  
Success #stdin #stdout 0.17s 2288KB
stdin
450
39
85
34
stdout
Probability=.0000000000000000000000000000000000000000000000000000000\
000000001203892499632531865200072947144402983