| 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 Lcm |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Private method to return an array of the factors of the input value. |
| 13 |
* |
| 14 |
* @return int[] |
| 15 |
*/ |
| 16 |
private static function factors(float $value): array |
| 17 |
{ |
| 18 |
$startVal = floor(sqrt($value)); |
| 19 |
|
| 20 |
$factorArray = []; |
| 21 |
for ($i = $startVal; $i > 1; --$i) { |
| 22 |
if (($value % $i) == 0) { |
| 23 |
$factorArray = array_merge($factorArray, self::factors($value / $i)); |
| 24 |
$factorArray = array_merge($factorArray, self::factors($i)); |
| 25 |
if ($i <= sqrt($value)) { |
| 26 |
break; |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
if (!empty($factorArray)) { |
| 31 |
rsort($factorArray); |
| 32 |
/** @var int[] $factorArray */ |
| 33 |
|
| 34 |
return $factorArray; |
| 35 |
} |
| 36 |
|
| 37 |
return [(int) $value]; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* LCM. |
| 42 |
* |
| 43 |
* Returns the lowest common multiplier of a series of numbers |
| 44 |
* The least common multiple is the smallest positive integer that is a multiple |
| 45 |
* of all integer arguments number1, number2, and so on. Use LCM to add fractions |
| 46 |
* with different denominators. |
| 47 |
* |
| 48 |
* Excel Function: |
| 49 |
* LCM(number1[,number2[, ...]]) |
| 50 |
* |
| 51 |
* @param mixed ...$args Data values |
| 52 |
* |
| 53 |
* @return int|string Lowest Common Multiplier, or a string containing an error |
| 54 |
*/ |
| 55 |
public static function evaluate(...$args) |
| 56 |
{ |
| 57 |
try { |
| 58 |
$arrayArgs = []; |
| 59 |
$anyZeros = 0; |
| 60 |
$anyNonNulls = 0; |
| 61 |
foreach (Functions::flattenArray($args) as $value1) { |
| 62 |
$anyNonNulls += (int) ($value1 !== null); |
| 63 |
$value = Helpers::validateNumericNullSubstitution($value1, 1); |
| 64 |
Helpers::validateNotNegative($value); |
| 65 |
$arrayArgs[] = (int) $value; |
| 66 |
$anyZeros += (int) !((bool) $value); |
| 67 |
} |
| 68 |
self::testNonNulls($anyNonNulls); |
| 69 |
if ($anyZeros) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
} catch (Exception $e) { |
| 73 |
return $e->getMessage(); |
| 74 |
} |
| 75 |
|
| 76 |
$returnValue = 1; |
| 77 |
$allPoweredFactors = []; |
| 78 |
// Loop through arguments |
| 79 |
foreach ($arrayArgs as $value) { |
| 80 |
$myFactors = self::factors(floor($value)); |
| 81 |
$myCountedFactors = array_count_values($myFactors); |
| 82 |
$myPoweredFactors = []; |
| 83 |
foreach ($myCountedFactors as $myCountedFactor => $myCountedPower) { |
| 84 |
$myPoweredFactors[$myCountedFactor] = $myCountedFactor ** $myCountedPower; |
| 85 |
} |
| 86 |
self::processPoweredFactors($allPoweredFactors, $myPoweredFactors); |
| 87 |
} |
| 88 |
foreach ($allPoweredFactors as $allPoweredFactor) { |
| 89 |
/** @var scalar $allPoweredFactor */ |
| 90 |
$returnValue *= (int) $allPoweredFactor; |
| 91 |
} |
| 92 |
|
| 93 |
return $returnValue; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @param mixed[] $allPoweredFactors |
| 98 |
* @param mixed[] $myPoweredFactors |
| 99 |
*/ |
| 100 |
private static function processPoweredFactors(array &$allPoweredFactors, array &$myPoweredFactors): void |
| 101 |
{ |
| 102 |
foreach ($myPoweredFactors as $myPoweredValue => $myPoweredFactor) { |
| 103 |
if (isset($allPoweredFactors[$myPoweredValue])) { |
| 104 |
if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) { |
| 105 |
$allPoweredFactors[$myPoweredValue] = $myPoweredFactor; |
| 106 |
} |
| 107 |
} else { |
| 108 |
$allPoweredFactors[$myPoweredValue] = $myPoweredFactor; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
private static function testNonNulls(int $anyNonNulls): void |
| 114 |
{ |
| 115 |
if (!$anyNonNulls) { |
| 116 |
throw new Exception(ExcelError::VALUE()); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|