Skip to content

Instantly share code, notes, and snippets.

@glsee
Created May 17, 2012 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glsee/2717532 to your computer and use it in GitHub Desktop.
Save glsee/2717532 to your computer and use it in GitHub Desktop.
Smallest number divisible by a series of continuous whole numbers starting from 1
<?php
/* Greatest Common Divisor */
function gcd($x, $y) {
if ($y === 0) return $x;
return abs(gcd($y, $x % $y));
}
/* Least Common Multiples */
function lcm($x, $y) {
return $x * $y / gcd($x, $y);
}
function recursive_lcm($z) {
if ($z <= 0) return false;
if ($z === 2) return 2;
return lcm($z, recursive_lcm($z-1) );
}
echo recursive_lcm(20);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment