/** * @dataProvider lcmProvider * * @param integer $a * @param integer $b * @param integer $expected */ public function testLcm($a, $b, $expected) { $result = OperatorsUtils::lcm($a, $b); $this->assertInternalType('integer', $result); $this->assertSame($expected, $expected); }
/** * Process the Lcm operator. * * @return integer|null A single integer equal in value to the lowest common multiple of the sub-expressions. If all arguments are 0, the result is 0, If any of the sub-expressions is NULL, the result is NULL. * @throws \qtism\runtime\expressions\operators\OperatorProcessingException */ public function process() { $operands = $this->getOperands(); if ($operands->containsNull() === true) { return null; } if ($operands->anythingButRecord() === false) { $msg = "The Lcm operator only accepts operands with a cardinality of single, multiple or ordered."; throw new OperatorProcessingException($msg, $this, OperatorProcessingException::WRONG_CARDINALITY); } if ($operands->exclusivelyInteger() === false) { $msg = "The Lcm operator only accepts operands with an integer baseType."; throw new OperatorProcessingException($msg, $this, OperatorProcessingException::WRONG_BASETYPE); } // Make a flat collection first. $flatCollection = new OperandsCollection(); foreach ($operands as $operand) { if ($operand instanceof Scalar) { if ($operand->getValue() !== 0) { $flatCollection[] = $operand; } else { // Operand is 0, return 0. return new Integer(0); } } elseif ($operand->contains(null)) { // Container with at least one null value inside. // -> If any of the sub-expressions is null or not numeric, returns null. return null; } else { // Container with no null values. foreach ($operand as $o) { if ($o->getValue() !== 0) { $flatCollection[] = $o; } else { // If any of the operand is 0, return 0. return new Integer(0); } } } } $g = $flatCollection[0]; $loopLimit = count($flatCollection) - 1; $i = 0; while ($i < $loopLimit) { $g = new Integer(Utils::lcm($g->getValue(), $flatCollection[$i + 1]->getValue())); $i++; } return $g; }