$d) { if ($i % $d === 0) { $div = $d; unset($range[$key]); break; } } $fact *= $i / $div; } $fact2 = 1; foreach ($range as $d) { $fact2 *= $d; } return (int) ($fact / $fact2); } /** * Calculate ackermann function. * * @param int $m m * @param int $n n * * @return int * * @since 1.0.0 */ public static function ackermann(int $m, int $n) : int { if ($m === 0) { return $n + 1; } elseif ($n === 0) { return self::ackermann($m - 1, 1); } return self::ackermann($m - 1, self::ackermann($m, $n - 1)); } /** * Applying abs to every array value * * @param array $values Numeric values * * @return array * * @since 1.0.0 */ public static function abs(array $values) : array { $abs = []; foreach ($values as $value) { $abs[] = abs($value); } return $abs; } /** * Calculate inverse modular. * * @param int $a a * @param int $n Modulo * * @return int * * @since 1.0.0 */ public static function invMod(int $a, int $n) : int { if ($n < 0) { $n = -$n; } if ($a < 0) { $a = $n - (-$a % $n); } $t = 0; $nt = 1; $r = $n; $nr = $a % $n; while ($nr != 0) { $quot = (int) ($r / $nr); $tmp = $nt; $nt = $t - $quot * $nt; $t = $tmp; $tmp = $nr; $nr = $r - $quot * $nr; $r = $tmp; } if ($r > 1) { return -1; } if ($t < 0) { $t += $n; } return $t; } /** * Modular implementation for negative values. * * @param int $a a * @param int $b b * * @return int * * @since 1.0.0 */ public static function mod($a, $b) : int { if ($a < 0) { return ($a + $b) % $b; } return $a % $b; } /** * Check if value is odd. * * @param int $a Value to test * * @return bool * * @since 1.0.0 */ public static function isOdd($a) : bool { return (bool) ($a & 1); } /** * Check if value is even. * * @param int $a Value to test * * @return bool * * @since 1.0.0 */ public static function isEven($a) : bool { return !((bool) ($a & 1)); } /** * Power all values in array. * * @param array $values Values to square * @param float $exp Exponent * * @return array * * @since 1.0.0 * todo: move to utils?! implement sqrt for array as well... could be usefull for others (e.g. matrix) */ public static function powerFloat(array $values, float $exp = 2.0) : array { $squared = []; foreach ($values as $value) { $squared[] = $value ** $exp; } return $squared; } /** * Power all values in array. * * @param array $values Values to square * @param int $exp Exponent * * @return array * * @since 1.0.0 * todo: move to utils?! implement sqrt for array as well... could be usefull for others (e.g. matrix) */ public static function powerInt(array $values, int $exp = 2) : array { $squared = []; foreach ($values as $value) { $squared[] = $value ** $exp; } return $squared; } /** * Gets the relative position on a circular construct. * * @example The relative fiscal month (August) in a company where the fiscal year starts in July. * @example 2 = getRelativeDegree(8, 12, 7); * * @param int $value Value to get degree * @param int $length Circle size * @param int $start Start value * * @return int Lowest value is 0 and highest value is length - 1 * * @since 1.0.0 */ public static function getRelativeDegree(int $value, int $length, int $start = 0) : int { return (int) abs(self::mod($value - $start, $length)); } }