PHP Chocolates By Numbers
function chocolatesByNumbers(int $n, int $m): int
{
$gcd = static function (int $n, int $m) use (&$gcd) {
if ($n % $m === 0) {
return $m;
}
return $gcd($m, $n % $m);
};
return (($n * $m) / $gcd($n, $m)) / $m;
}
This uses the greatest common divisor to figure out how many chocolates get eaten before the pattern repeats.