| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\MathTrig; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Exception; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 8 |
|
| 9 |
class Gcd |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Recursively determine GCD. |
| 13 |
* |
| 14 |
* Returns the greatest common divisor of a series of numbers. |
| 15 |
* The greatest common divisor is the largest integer that divides both |
| 16 |
* number1 and number2 without a remainder. |
| 17 |
* |
| 18 |
* Excel Function: |
| 19 |
* GCD(number1[,number2[, ...]]) |
| 20 |
* @param float|int $a |
| 21 |
* @param float|int $b |
| 22 |
* @return float|int |
| 23 |
*/ |
| 24 |
private static function evaluateGCD($a, $b) |
| 25 |
{ |
| 26 |
return $b ? self::evaluateGCD($b, $a % $b) : $a; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* GCD. |
| 31 |
* |
| 32 |
* Returns the greatest common divisor of a series of numbers. |
| 33 |
* The greatest common divisor is the largest integer that divides both |
| 34 |
* number1 and number2 without a remainder. |
| 35 |
* |
| 36 |
* Excel Function: |
| 37 |
* GCD(number1[,number2[, ...]]) |
| 38 |
* |
| 39 |
* @param mixed ...$args Data values |
| 40 |
* |
| 41 |
* @return float|int|string Greatest Common Divisor, or a string containing an error |
| 42 |
*/ |
| 43 |
public static function evaluate(...$args) |
| 44 |
{ |
| 45 |
try { |
| 46 |
$arrayArgs = []; |
| 47 |
foreach (Functions::flattenArray($args) as $value1) { |
| 48 |
if ($value1 !== null) { |
| 49 |
$value = Helpers::validateNumericNullSubstitution($value1, 1); |
| 50 |
Helpers::validateNotNegative($value); |
| 51 |
$arrayArgs[] = (int) $value; |
| 52 |
} |
| 53 |
} |
| 54 |
} catch (Exception $e) { |
| 55 |
return $e->getMessage(); |
| 56 |
} |
| 57 |
|
| 58 |
if (count($arrayArgs) <= 0) { |
| 59 |
return ExcelError::VALUE(); |
| 60 |
} |
| 61 |
$gcd = (int) array_pop($arrayArgs); |
| 62 |
do { |
| 63 |
$gcd = self::evaluateGCD($gcd, (int) array_pop($arrayArgs)); |
| 64 |
} while (!empty($arrayArgs)); |
| 65 |
|
| 66 |
return $gcd; |
| 67 |
} |
| 68 |
} |
| 69 |
|