jaige.util
Class MathTools

java.lang.Object
  extended by jaige.util.MathTools

public class MathTools
extends java.lang.Object


Method Summary
static int combinations(int n, int r)
          Given the total number of choices 'n', and the total number chosen 'r', returns the total number of combinations that can be chosen.
static java.lang.Object[][] combinations(java.lang.Object[] choices, int choose)
          Returns 'choose' combinations of 'choices' in an Object[][].
static void combinations(java.lang.Object[] choices, java.lang.Object[][] results)
          Returns all combinations of the 'choices'.
static int factorial(int n)
          Returns the mathmatical factorial of 'n'.
static long factorial(long n)
          Returns the mathmatical factorial of 'n'.
static void main(java.lang.String[] args)
           
static
<T> void
permutations(java.util.ArrayList<T> choices, int choose, java.util.ArrayList<java.util.ArrayList<T>> results)
           
static int permutations(int n, int r)
          Given the total number of choices 'n', and the total number chosen 'r', returns the total number of permutations that can be chosen.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

combinations

public static java.lang.Object[][] combinations(java.lang.Object[] choices,
                                                int choose)
Returns 'choose' combinations of 'choices' in an Object[][]. The disadvantage of this is that the returning Object[][] may not be of the desired type.

Parameters:
choices - Object[] Things to choose from
choose - int Number to choose
Returns:
Object[][] All combinations of picking 'choose' from 'choices'

combinations

public static void combinations(java.lang.Object[] choices,
                                java.lang.Object[][] results)
Returns all combinations of the 'choices'. The total chosen is dependant on the length of the results passed in. This method assumes the user has properly calculated the needed size of their Object[][]. Since this takes Object[]([])s, this method can take any class type.

Parameters:
choices - Object[] Things to choose from
results - Object[][] All combinations requested from 'choices'.

permutations

public static <T> void permutations(java.util.ArrayList<T> choices,
                                    int choose,
                                    java.util.ArrayList<java.util.ArrayList<T>> results)

combinations

public static int combinations(int n,
                               int r)
Given the total number of choices 'n', and the total number chosen 'r', returns the total number of combinations that can be chosen. This is equivalent to nCr on a calculator.

Parameters:
n - int Number of choices
r - int Number to choose
Returns:
int Total number of combinations

permutations

public static int permutations(int n,
                               int r)
Given the total number of choices 'n', and the total number chosen 'r', returns the total number of permutations that can be chosen. This is equivalent to nPr on a calculator. Instead of simply doing return factorial(n) / factorial(n-r); where there are 2n-r multiplications, this method is recursive and performs n-r multiplications.

Parameters:
n - int Number of choices
r - int Number to choose
Returns:
int Total number of permutations

factorial

public static long factorial(long n)
Returns the mathmatical factorial of 'n'. This is equal to n! in mathmatics.

Parameters:
n - long value to perform factorial on, but be <= 20 and >= 0
Returns:
long factorial of n

factorial

public static int factorial(int n)
Returns the mathmatical factorial of 'n'. This is equal to n! in mathmatics.

Parameters:
n - long value to perform factorial on, but be <= 12 and >= 0
Returns:
long factorial of n

main

public static void main(java.lang.String[] args)