Location: PHPKode > scripts > Simple Math > simple-math/lsSimpleMath.class.php
<?

/*

    lsSimpleMath - Math Class for calculating Factorials, Permutations, and Combinations
    Written by Travis Conway - Loni Software, LLC
    This class is released under the GNU GPL v2.
    
    This class is not intended for upper level math that requires Primorial, Double
        Factorials, Multifactorials, Quadruple factorials, Superfactorials, or
        Hyperfactorials.  This class is for basic Factorial computation for the
        purpose of determining Basic Permutations and Combinations.

*/

class lsSimpleMath 
{

function Factorial($int)
    {
        if(is_null($int)) {
            trigger_error("NULL value sent to lsSimpleMath::Factorial, assuming 0", E_NOTICE);
            $int = 0;
        }

        if($int < 0) {
            trigger_error("Negative value sent to lsSimpleMath::Factorial, assuming 0", E_NOTICE);
            $int = 0;
        }

        $int = floor($int);    //We floor to get rid of any decimals sent to Factorial. No rounding
        $ret = (int)1;

        $counter = (int)1;

        while($counter <= $int) {
            //print("counter = $counter ret = $ret int = $int\n");
            $ret = $ret * $counter;
            $counter++;
        }
        return($ret);
    } //end of Factorial

function Permutation($n, $r)
    {
        /* Permutations are nPr and are calculated nPr  = n! / (n - r)! */

        if(is_null($n)) {
            trigger_error("NULL value sent to lsSimpleMath::Permutation for n, assuming 1", E_NOTICE);
            $n = 1;
        }

        if($n < 0) {
            trigger_error("Negative value sent to lsSimpleMath::Permutation for n, assuming 1", E_NOTICE);
            $n = 1;
        }

        if(is_null($r)) {
            trigger_error("NULL value sent to lsSimpleMath::Permutation for r, assuming 1", E_NOTICE);
            $r = 1;
        }

        if($r < 0) {
            trigger_error("Negative value sent to lsSimpleMath::Permutation for r, assuming 1", E_NOTICE);
            $r = 1;
        }

        $ret = $this->Factorial($n) / $this->Factorial($n - $r);
        return($ret);
    } //end of Permutation

function Combination($n, $k)
    {
        /* Combinations are nCk and are calculated nCk = n! / k! * (n - k)! */
        if(is_null($n)) {
            trigger_error("NULL value sent to lsSimpleMath::Combination for n, assuming 1", E_NOTICE);
            $n = 1;
        }

        if($n < 0) {
            trigger_error("Negative value sent to lsSimpleMath::Combination for n, assuming 1", E_NOTICE);
            $n = 1;
        }

        if(is_null($k)) {
            trigger_error("NULL value sent to lsSimpleMath::Combination for k, assuming 1", E_NOTICE);
            $k = 1;
        }

        if($k < 0) {
            trigger_error("Negative value sent to lsSimpleMath::Combination for k, assuming 1", E_NOTICE);
            $k = 1;
        }
        $ret = $this->Factorial($n) / ($this->Factorial($k) * $this->Factorial($n - $k));
        return($ret);
    } //end of Combination

} // end of class lsSimpleMath
?>
Return current item: Simple Math