Class PascalsTriangle

java.lang.Object
com.graphbuilder.math.PascalsTriangle

public final class PascalsTriangle extends Object
PascalsTriangle can be used for O(1) lookup of the nCr function.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    nCr(int n, int r)
    The nCr function returns the number of ways r things can be chosen from a set of size n.
    void
    Resets the internal array to the initial state to free up memory.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • PascalsTriangle

      public PascalsTriangle()
  • Method Details

    • nCr

      public double nCr(int n, int r)
      The nCr function returns the number of ways r things can be chosen from a set of size n. Mathematically, it is defined as: n! / (r! * (n - r)!) Although the result is always a whole number, double precision is used because the maximum value a double can represent is larger than long. Thus, large returned values will only be an approximation of the actual value. If the result exceeds the capabilities of double precision then the result can be checked using Double.isInfinite(...). For example: System.out.println(PascalsTriangle.nCr(1030, 515)); // outputs: Infinity If the value of n or r is less than 0 or the value of r is greater than n then 0 is returned.
    • reset

      public void reset()
      Resets the internal array to the initial state to free up memory.