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