In this post, you will learn how to find the LCM of two numbers in PHP.
LCM – Least Common Multiple
The LCM of two or more numbers is the smallest number that is divisible by those original integers.
Associative property LCM(a, b) = LCM(b, a)
Commutative property LCM(a, b, c) = LCM(LCM(a, b), c) = LCM(a, LCM(b, c))
Rules – The LCM of two or more numbers is equal to the product of each factor by the maximum number of times it appears in the prime factorization of those numbers.
Code snippet for LCM of Two Number Using PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <?php $num1=6; $num2=4; $max=($num1>$num2) ? $num1 : $num2; while(1) { if($max%$num1==0 && $max%$num2==0) { echo "LCM of " .$num1. " and " .$num2. " is: ".$max; break; } $max=$max+1; } ?> |
Output- LCM of 6 and 4 is: 12