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