You are here

public static function PHPExcel_Calculation_MathTrig::LCM in Loft Data Grids 7.2

Same name and namespace in other branches
  1. 6.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/MathTrig.php \PHPExcel_Calculation_MathTrig::LCM()

* LCM * * Returns the lowest common multiplier of a series of numbers * The least common multiple is the smallest positive integer that is a multiple * of all integer arguments number1, number2, and so on. Use LCM to add fractions * with different denominators. * * Excel Function: * LCM(number1[,number2[, ...]]) * * @access public * @category Mathematical and Trigonometric Functions *

Parameters

mixed $arg,... Data values: * @return int Lowest Common Multiplier

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Calculation/MathTrig.php, line 454

Class

PHPExcel_Calculation_MathTrig
PHPExcel_Calculation_MathTrig

Code

public static function LCM() {
  $returnValue = 1;
  $allPoweredFactors = array();

  // Loop through arguments
  foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) {
    if (!is_numeric($value)) {
      return PHPExcel_Calculation_Functions::VALUE();
    }
    if ($value == 0) {
      return 0;
    }
    elseif ($value < 0) {
      return PHPExcel_Calculation_Functions::NaN();
    }
    $myFactors = self::_factors(floor($value));
    $myCountedFactors = array_count_values($myFactors);
    $myPoweredFactors = array();
    foreach ($myCountedFactors as $myCountedFactor => $myCountedPower) {
      $myPoweredFactors[$myCountedFactor] = pow($myCountedFactor, $myCountedPower);
    }
    foreach ($myPoweredFactors as $myPoweredValue => $myPoweredFactor) {
      if (array_key_exists($myPoweredValue, $allPoweredFactors)) {
        if ($allPoweredFactors[$myPoweredValue] < $myPoweredFactor) {
          $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
        }
      }
      else {
        $allPoweredFactors[$myPoweredValue] = $myPoweredFactor;
      }
    }
  }
  foreach ($allPoweredFactors as $allPoweredFactor) {
    $returnValue *= (int) $allPoweredFactor;
  }
  return $returnValue;
}