code: public static double chisquareInverseCdf(double p, int df) {
          final double CHI_EPSILON = 0.000001;   /* Accuracy of critchi approximation */
          final double CHI_MAX = 99999.0;        /* Maximum chi-square value */
          double minchisq = 0.0;
          double maxchisq = CHI_MAX;
  
          if (p <= 0.0) {
              return CHI_MAX;
          } 
          else if (p >= 1.0) {
              return 0.0;
          }
  
          double chisqval = df/Math.sqrt(p);    /* fair first value */
          while ((maxchisq - minchisq) > CHI_EPSILON) {
              if (1-chisquareCdf(chisqval, df) < p) {
                  maxchisq = chisqval;
              } 
              else {
                  minchisq = chisqval;
              }
              chisqval = (maxchisq + minchisq) * 0.5;
          }
          
          return chisqval;
      }
docs: Returns the x score of a specific pvalue and degrees of freedom for Chisquare. It We just do a bisectionsearch for a value within CHI_EPSILON, relying on the monotonicity of chisquareCdf().
  Ported from Javascript code posted at http://www.fourmilab.ch/rpkp/experiments/analysis/chiCalc.js
  
  @param p
  @param df
  @return