Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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