Binomial coefficent, also known as "n choose k").

@param n Total number of samples. n &gt; 0
@param k Number of elements to choose. <code>n &gt;= k</code>,
<code>k &gt;= 0</code>
@return n! / (k! * (n-k)!)
169584
public static double approximateBinomialCoefficient(int n, int k) {
    final int m = max(k, n - k);
    long temp = 1;
    for(int i = n, j = 1; i > m; i--, j++) {
      temp = temp * i / j;
    }
    return temp;
  }
1247134
public static BigDecimal factorial(int n) {
		if (n < 0) {
			throw new ArithmeticException("Illegal factorial(n) for n < 0: n = " + n);
		}
		if (n < factorialCache.length) {
			return factorialCache[n];
		}

		BigDecimal result = factorialCache[factorialCache.length - 1];
		return result.multiply(factorialRecursion(factorialCache.length, n));
	}
666583
public static Double smallest(Iterator <Double> elements, int k) {
        if (k <= 0 || !elements.hasNext()) {
            return null;
        } 
        else if (k >= Integer.MAX_VALUE / 2) {
            List <Double> list = new ArrayList <> ();
            while (elements.hasNext()) {
                list.add(elements.next());
            }
            Collections.sort(list);
            return list.get(k - 1);
        }

        int bufferCap = k * 2;
        Double[] buffer = new Double[bufferCap];
        Double threshold = elements.next();
        buffer[0] = threshold;
        int bufferSize = 1;

        while (bufferSize < k && elements.hasNext()) {
            Double e = elements.next();
            buffer[bufferSize++] = e;
            threshold = Math.max(threshold, e);
        }

        while (elements.hasNext()) {
            Double e = elements.next();
            if (e >= threshold) {
                continue;
            }

            buffer[bufferSize++] = e;
            if (bufferSize == bufferCap) {

                int left = 0;
                int right = bufferCap - 1;

                int minThresholdPosition = 0;

                while (left < right) {
                    int pivotIndex = (left + right + 1) >>> 1;
                    
                    //--- partition
                    Double pivotValue = buffer[pivotIndex];

                    buffer[pivotIndex] = buffer[right];
                    buffer[right] = pivotValue;

                    int pivotNewIndex = left;
                    for (int l = left; l < right; l++) {
                        if (buffer[l] < pivotValue) {
                            Double temp = buffer[pivotNewIndex];
                            buffer[pivotNewIndex] = buffer[l];
                            buffer[l] = temp;
                            pivotNewIndex++;
                        }
                    }

                    Double temp = buffer[right];
                    buffer[right] = buffer[pivotNewIndex];
                    buffer[pivotNewIndex] = temp;
                    //---
        
                    if (pivotNewIndex > k) {
                        right = pivotNewIndex - 1;
                    } 
                    else if (pivotNewIndex < k) {
                        left = Math.max(pivotNewIndex, left + 1);
                        minThresholdPosition = pivotNewIndex;
                    } 
                    else {
                        break;
                    }
                }
                bufferSize = k;

                threshold = buffer[minThresholdPosition];
                for (int i = minThresholdPosition + 1; i < bufferSize; i++) {
                    threshold = Math.max(threshold, buffer[i]);
                }
            }
        }
        
        Arrays.sort(buffer, 0, bufferSize);

        k = Math.min(bufferSize, k);
        
        return buffer[k-1];
    }
1020257
@VisibleForTesting
    boolean onMinLevel(int k) {
        float kAsFloat = k;
        int exponent = Math.getExponent(kAsFloat);
        return exponent % 2 == 0;
    }
494122
public static PermutationMatrix getInstance(int n)
    {
        PermutationMatrix pm = map.get(n);
        if (pm == null)
        {
            pm = new PermutationMatrix(n);
            map.put(n, pm);
        }
        return pm;
    }
866588
public static double uniformCdf(int k, int n) {
        if(k<0 || n<1) {
            throw new IllegalArgumentException("All the parameters must be positive and n larger than 1.");
        }
        k = Math.min(k, n);
        
        double probabilitySum = k*uniform(n);
        
        return probabilitySum;
    }
699318
public static int gcd(int m, int n) {
    if (m == 0 && n == 0)
      return 0;
    if (m < n) {
      int t = m;
      m = n;
      n = t;
    }
    int r = m % n;
    if (r == 0) {
      return n;
    } else {
      return gcd(n, r);
    }
  }
1014845
public static Rational binomial(Rational n, BigInteger m) {
        if (m.compareTo(BigInteger.ZERO) == 0) {
            return Rational.ONE;
        }
        Rational bin = n;
        for (BigInteger i = BigInteger.valueOf(2); i.compareTo(m) != 1; i = i.add(BigInteger.ONE)) {
            bin = bin.multiply(n.subtract(i.subtract(BigInteger.ONE))).divide(i);
        }
        return bin;
    }
76478
public final double sampleSigma(int n)
  {
    synchronized (_lock) {
      long count = _count.get();
      long lastCount = _lastStdCount;
      _lastStdCount = count;

      double sum = _sum.get();
      double lastSum = _lastStdSum;
      _lastStdSum = sum;

      double sumSquare = _sumSquare;
      _sumSquare = 0;

      if (count == lastCount)
        return 0;

      double avg = (sum - lastSum) / (count - lastCount);
      double part = (count - lastCount) * sumSquare - sum * sum;

      if (part < 0)
        part = 0;

      double std = Math.sqrt(part) / (count - lastCount);

      return _scale * (avg + n * std);
    }
  }
435700
public static int count(int[] histogram, int N) {
		int counts = 0;
		for(int i=0;i<N;i++) {
			counts += histogram[i];
		}
		return counts;
	}
1151715
@Reference(title = "Fast and accurate computation of binomial probabilities", //
      authors = "C. Loader", booktitle = "", //
      url = "http://projects.scipy.org/scipy/raw-attachment/ticket/620/loader2000Fast.pdf", //
      bibkey = "web/Loader00")
  private static double stirlingError(int n) {
    // Try to use a table value:
    if(n < 16) {
      return STIRLING_EXACT_ERROR[n << 1];
    }
    final double nn = n * n;
    // Use the appropriate number of terms
    if(n > 500) {
      return (S0 - S1 / nn) / n;
    }
    if(n > 80) {
      return ((S0 - (S1 - S2 / nn)) / nn) / n;
    }
    if(n > 35) {
      return ((S0 - (S1 - (S2 - S3 / nn) / nn) / nn) / n);
    }
    return ((S0 - (S1 - (S2 - (S3 - S4 / nn) / nn) / nn) / nn) / n);
  }
939683
public static long binomialCoefficient(long n, long k) {
    final long m = Math.max(k, n - k);
    double temp = 1;
    for(long i = n, j = 1; i > m; i--, j++) {
      temp = temp * i / j;
    }
    return (long) temp;
  }
1358657
protected static long nCk(int n, int k)
    {
        if (k > n)
        {
            return 0;
        }
        long result = 1;
        for (int i = 1; i <= k; i++)
        {
            result *= n--;
            result /= i;
        }
        return result;
    }
838983
protected function binom($n, $k)
    {
        $j = $res = 1;

        if ($k < 0 || $k > $n) {
            return 0;
        }
        if (($n - $k) < $k) {
            $k = $n - $k;
        }
        while ($j <= $k) {
            $res *= $n--;
            $res /= $j++;
        }

        return $res;
    }
1116591
protected double kNNDistance() {
    double result = getEntry(0).getKnnDistance();
    for(int i = 1; i < getNumEntries(); i++) {
      double knnDistance = getEntry(i).getKnnDistance();
      result = (result < knnDistance) ? knnDistance : result;
    }
    return result;
  }
1159322
public static long count(int n, int m) {
		if(n == m) {
			return NumberUtil.factorial(n);
		}
		return (n > m) ? NumberUtil.factorial(n, n - m) : 0;
	}
764772
public int[] permutate(int n) {
        int[] x = new int[n];
        for (int i = 0; i < n; i++) {
            x[i] = i;
        }

        permutate(x);

        return x;
    }