/** * Finds the LCM of an array of integers * @param array $integers * @return int */ function lcm(array $integers) { if (count($integers) > 1) { $a = array_shift($integers); $b = array_shift($integers); $integers[] = ($a / gcf($a, $b) * $b); return lcm($integers); } else { return $integers[0]; } }
/** * Calculates the Least Common Multiple of an array of integers. * * @param array $array * @return integer */ function gcf_array($array) { if (!is_array($array)) { throw new \InvalidArgumentException('lcm_array expects an array of integers.'); } if (count($array) > 1) { $array[] = gcf(array_shift($array), array_shift($array)); return gcf_array($array); } return $array[0]; }