ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Factorial.php
Go to the documentation of this file.
1 <?php
2 
4 
8 
9 class Factorial
10 {
24  public static function fact($factVal)
25  {
26  try {
27  $factVal = Helpers::validateNumericNullBool($factVal);
29  } catch (Exception $e) {
30  return $e->getMessage();
31  }
32 
33  $factLoop = floor($factVal);
34  if ($factVal > $factLoop) {
37  }
38  }
39 
40  $factorial = 1;
41  while ($factLoop > 1) {
42  $factorial *= $factLoop--;
43  }
44 
45  return $factorial;
46  }
47 
60  public static function factDouble($factVal)
61  {
62  try {
63  $factVal = Helpers::validateNumericNullSubstitution($factVal, 0);
65  } catch (Exception $e) {
66  return $e->getMessage();
67  }
68 
69  $factLoop = floor($factVal);
70  $factorial = 1;
71  while ($factLoop > 1) {
72  $factorial *= $factLoop;
73  $factLoop -= 2;
74  }
75 
76  return $factorial;
77  }
78 
88  public static function multinomial(...$args)
89  {
90  $summer = 0;
91  $divisor = 1;
92 
93  try {
94  // Loop through arguments
95  foreach (Functions::flattenArray($args) as $argx) {
96  $arg = Helpers::validateNumericNullSubstitution($argx, null);
98  $arg = (int) $arg;
99  $summer += $arg;
100  $divisor *= self::fact($arg);
101  }
102  } catch (Exception $e) {
103  return $e->getMessage();
104  }
105 
106  $summer = self::fact($summer);
107 
108  return $summer / $divisor;
109  }
110 }
static validateNumericNullBool($number)
Many functions accept null/false/true argument treated as 0/0/1.
Definition: Helpers.php:27
static validateNumericNullSubstitution($number, $substitute)
Validate numeric, but allow substitute for null.
Definition: Helpers.php:51
static flattenArray($array)
Convert a multi-dimensional array to a simple 1-dimensional array.
Definition: Functions.php:583
static validateNotNegative($number, ?string $except=null)
Confirm number >= 0.
Definition: Helpers.php:69
static getCompatibilityMode()
Return the current Compatibility Mode.
Definition: Functions.php:93