How to find the smallest number that is evenly divisible by all of the numbers from 1 to 20 in PHP

1 Answer

0 votes
function LMC($a, $b) {
    $lmc = ($a > $b) ? $a : $b;
    
    while (true) {
        if ($lmc % $a == 0 && $lmc % $b == 0) {
            return $lmc;
        }
        $lmc++;
    }
}

$result = 1;

for ($i = 1; $i <= 20; $i++) {
    $result = LMC($result, $i);
}

echo $result;




/*
run:
   
232792560
   
*/

 



answered Oct 18, 2023 by avibootz
...