Created
May 17, 2012 08:57
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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