ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Gcd.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
8 class Gcd
9 {
25  private static function evaluateGCD($a, $b)
26  {
27  return $b ? self::evaluateGCD($b, $a % $b) : $a;
28  }
29 
44  public static function evaluate(...$args)
45  {
46  try {
47  $arrayArgs = [];
48  foreach (Functions::flattenArray($args) as $value1) {
49  if ($value1 !== null) {
50  $value = Helpers::validateNumericNullSubstitution($value1, 1);
52  $arrayArgs[] = (int) $value;
53  }
54  }
55  } catch (Exception $e) {
56  return $e->getMessage();
57  }
58 
59  if (count($arrayArgs) <= 0) {
60  return Functions::VALUE();
61  }
62  $gcd = (int) array_pop($arrayArgs);
63  do {
64  $gcd = self::evaluateGCD($gcd, (int) array_pop($arrayArgs));
65  } while (!empty($arrayArgs));
66 
67  return $gcd;
68  }
69 }
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 evaluateGCD($a, $b)
Recursively determine GCD.
Definition: Gcd.php:25
static validateNotNegative($number, ?string $except=null)
Confirm number >= 0.
Definition: Helpers.php:69