// todo
}
+ ////////////////////////////////////////////////////////////////////////////
+ // Helper methods //
+ ////////////////////////////////////////////////////////////////////////////
+
+ /**
+ * Helper function returning the greatest common divisor
+ *
+ * @param int $a
+ * @param int $b
+ * @return int
+ */
+ public static function gcd($a, $b) {
+ return ($b == 0) ? ($a):(self::gcd($b, $a % $b));
+ }
+
+ /**
+ * Helper function returning the least common multiple
+ *
+ * @param int $a
+ * @param int $b
+ * @return int
+ */
+ public static function lcm($a, $b) {
+ return ($a / self::gcd($a,$b)) * $b;
+ }
////////////////////////////////////////////////////////////////////////////////
// Internal methods (implementation details) //
);
}
-
}
// compute the number of <tr> table rows needed to display this participant
if ($numofreceived > 0 and $numofgiven > 0) {
- $numoftrs = self::lcm($numofreceived, $numofgiven);
+ $numoftrs = workshop::lcm($numofreceived, $numofgiven);
$spanreceived = $numoftrs / $numofreceived;
$spangiven = $numoftrs / $numofgiven;
} elseif ($numofreceived == 0 and $numofgiven > 0) {
// Helper methods //
////////////////////////////////////////////////////////////////////////////
- /**
- * Helper function returning the greatest common divisor
- *
- * @param int $a
- * @param int $b
- * @return int
- */
- protected static function gcd($a, $b) {
- return ($b == 0) ? ($a):(self::gcd($b, $a % $b));
- }
-
- /**
- * Helper function returning the least common multiple
- *
- * @param int $a
- * @param int $b
- * @return int
- */
- protected static function lcm($a, $b) {
- return ($a / self::gcd($a,$b)) * $b;
- }
-
/**
* Helper function returning the n-th item of the array
*
$part = workshop::percent_to_value($percent, $total);
}
+ public function test_lcm() {
+ // fixture setup + excercise SUT + verify in one step
+ $this->assertEqual(workshop::lcm(1,4), 4);
+ $this->assertEqual(workshop::lcm(2,4), 4);
+ $this->assertEqual(workshop::lcm(4,2), 4);
+ $this->assertEqual(workshop::lcm(2,3), 6);
+ $this->assertEqual(workshop::lcm(6,4), 12);
+ }
+
+ public function test_lcm_array() {
+ // fixture setup
+ $numbers = array(5,3,15);
+ // excersise SUT
+ $lcm = array_reduce($numbers, 'workshop::lcm', 1);
+ // verify
+ $this->assertEqual($lcm, 15);
+ }
}