You are here

public static function PHPExcel_Calculation_MathTrig::GCD 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::GCD()

* GCD * * Returns the greatest common divisor of a series of numbers. * The greatest common divisor is the largest integer that divides both * number1 and number2 without a remainder. * * Excel Function: * GCD(number1[,number2[, ...]]) * * @access public * @category Mathematical and Trigonometric Functions *

Parameters

mixed $arg,... Data values: * @return integer Greatest Common Divisor

1 call to PHPExcel_Calculation_MathTrig::GCD()
PHPExcel_Style_NumberFormat::_formatAsFraction in vendor/phpoffice/phpexcel/Classes/PHPExcel/Style/NumberFormat.php

File

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

Class

PHPExcel_Calculation_MathTrig
PHPExcel_Calculation_MathTrig

Code

public static function GCD() {
  $returnValue = 1;
  $allValuesFactors = array();

  // Loop through arguments
  foreach (PHPExcel_Calculation_Functions::flattenArray(func_get_args()) as $value) {
    if (!is_numeric($value)) {
      return PHPExcel_Calculation_Functions::VALUE();
    }
    elseif ($value == 0) {
      continue;
    }
    elseif ($value < 0) {
      return PHPExcel_Calculation_Functions::NaN();
    }
    $myFactors = self::_factors($value);
    $myCountedFactors = array_count_values($myFactors);
    $allValuesFactors[] = $myCountedFactors;
  }
  $allValuesCount = count($allValuesFactors);
  if ($allValuesCount == 0) {
    return 0;
  }
  $mergedArray = $allValuesFactors[0];
  for ($i = 1; $i < $allValuesCount; ++$i) {
    $mergedArray = array_intersect_key($mergedArray, $allValuesFactors[$i]);
  }
  $mergedArrayValues = count($mergedArray);
  if ($mergedArrayValues == 0) {
    return $returnValue;
  }
  elseif ($mergedArrayValues > 1) {
    foreach ($mergedArray as $mergedKey => $mergedValue) {
      foreach ($allValuesFactors as $highestPowerTest) {
        foreach ($highestPowerTest as $testKey => $testValue) {
          if ($testKey == $mergedKey && $testValue < $mergedValue) {
            $mergedArray[$mergedKey] = $testValue;
            $mergedValue = $testValue;
          }
        }
      }
    }
    $returnValue = 1;
    foreach ($mergedArray as $key => $value) {
      $returnValue *= pow($key, $value);
    }
    return $returnValue;
  }
  else {
    $keys = array_keys($mergedArray);
    $key = $keys[0];
    $value = $mergedArray[$key];
    foreach ($allValuesFactors as $testValue) {
      foreach ($testValue as $mergedKey => $mergedValue) {
        if ($mergedKey == $key && $mergedValue < $value) {
          $value = $mergedValue;
        }
      }
    }
    return pow($key, $value);
  }
}