gistlib
One way to find the least common multiple (LCM) of two numbers in PHP is to use the following algorithm:
Here's the PHP code to implement this algorithm:
main.phpfunction findGCD($a, $b) { // base case if ($b == 0) { return $a; } // recursive case return findGCD($b, $a % $b); } function findLCM($a, $b) { $gcd = findGCD($a, $b); $lcm = ($a * $b) / $gcd; return $lcm; } // example usage $a = 6; $b = 8; $lcm = findLCM($a, $b); echo "The LCM of $a and $b is: $lcm"; 345 chars21 lines
function findGCD($a, $b) { // base case if ($b == 0) { return $a; } // recursive case return findGCD($b, $a % $b); } function findLCM($a, $b) { $gcd = findGCD($a, $b); $lcm = ($a * $b) / $gcd; return $lcm; } // example usage $a = 6; $b = 8; $lcm = findLCM($a, $b); echo "The LCM of $a and $b is: $lcm";
This will output: "The LCM of 6 and 8 is: 24".
gistlibby LogSnag