Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
88.89% covered (success)
88.89%
8 / 9
CRAP
98.51% covered (success)
98.51%
66 / 67
Math
0.00% covered (danger)
0.00%
0 / 1
88.89% covered (success)
88.89%
8 / 9
44
98.51% covered (success)
98.51%
66 / 67
 factorial
100.00% covered (success)
100.00%
1 / 1
25
100.00% covered (success)
100.00%
29 / 29
 getPercentageChange
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 Quartile
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
6 / 6
 median
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 average
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 gcd
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 lcm
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 primeFactors
0.00% covered (danger)
0.00%
0 / 1
10
96.15% covered (success)
96.15%
25 / 26
 primesFirst
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
<?php
namespace ia\Math;
class Math {
    /**
     * Math::factorial()
     *
     * @param int $number
     * @return int
     */
    public static function factorial($number) {
        switch($number) {
            case 0: return 1;
            case 1: return 1;
            case 2: return 2;
            case 3: return 6;
            case 4: return 24;
            case 5: return 120;
            case 6: return 720;
            case 7: return 5040;
            case 8: return 40320;
            case 9: return 362880;
            case 10: return 3628800;
            case 11: return 39916800;
            case 12: return 479001600;
            case 13: return 6227020800;
            case 14: return 87178291200;
            case 15: return 1307674368000;
            case 16: return 20922789888000;
            case 17: return 355687428096000;
            case 18: return 6402373705728000;
            case 19: return 121645100408832000;
            case 20: return 2432902008176640000;
            default:
                if($number < 0) {
                    return 0; //@TODO throw error?
                }
        }
        $factorial = $number;
        for($i = $number - 1; $i > 1; --$i) {
            if($i === 20) {
                return $factorial * 2432902008176640000;
            }
            $factorial *= $i;
        }
        // @codeCoverageIgnoreStart
        return $factorial;
        // @codeCoverageIgnoreEnd
    }
    /**
     * Percengage Increase, where 1.0 = 100%
     *
     * @param float $oldNumber
     * @param float $newNumber
     * @return float Percentaje it changes 100, 110 = 0.1
     */
    public static function getPercentageChange($oldNumber, $newNumber){
        return (($newNumber - $oldNumber) / $oldNumber);
    }
    /**
     * Math::Quartile()
     *
     * @param array $array
     * @param float $Quartile
     * @return
     */
    public static function Quartile($array, $Quartile) {
        sort($array);
        $pos = (count($array) - 1) * $Quartile;
        $base = floor($pos);
        if(isset($array[$base+1]) ) {
            return $array[$base] + ($pos - $base) * ($array[$base+1] - $array[$base]);
        }
        return $array[$base];
    }
    /**
     * Math::median()
     *
     * @param array $array
     * @return mixed
     */
    public static function median($array) {
        return self::Quartile($array, 0.5);
    }
    /**
     * Math::average()
     *
     * @param array $array
     * @return float
     */
    public static function average($array) {
        return array_sum($array)/count($array);
    }
    /**
     * Gratest common divisor
     *
     * @param int $a
     * @param int $b
     * @return int
     */
    public static function gcd(int $a,int $b) {
        return $a === 0 ? $b : self::gcd($b % $a, $a);
    }
    /**
     * least common multiplier
     *
     * @param int $a
     * @param int $b
     * @return int
     */
    public static function lcm($a,$b){
        return ($a * $b) / self::gcd($a, $b);
    }
    /**
     * Prime factors
     *
     * @param int $n
     * @return array of primes
     */
    public static function primeFactors($n) {
        if($n <= 1) {
            return [$n];
        }
        //$n = abs($n);
        $primes = self::primesFirst();
        $primeFactors = [];
        $maxFactors = 3000;
        $crazy=1;
        while(++$crazy<$maxFactors) {
            foreach($primes as $p) {
                if( $n % $p === 0) {
                    $primeFactors[$p]=$p;
                    $n = intdiv($n, $p);
                    if($n <= 1) {
                        return array_values($primeFactors);
                    }
                    continue 2;
                }
            }
            break;
        }
        $limit = $n + 1;
        $maxFactors *=2;
        while(++$crazy<$maxFactors) {
            for($i = end($primes); $i < $limit; $i+=2) {
                if( $n % $i === 0) {
                    $primeFactors[$i]=$i;
                    $n = intdiv($n, $i);
                    if($n <= 1) {
                        return array_values($primeFactors);
                    }
                    continue 2;
                }
            }
        }
        return [];
    }
    /**
     * An array with prime number from 2 to 229
     *
     * @return array
     */
    public static function primesFirst() {
        return [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229];
    }
}