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