- 12
36 = 12·3- 18
36 = 18·2
Find LCM in PHP
What you’ll learn
- The definition of lcm(
a,b) for nonnegative integers and thegcd · lcm = a · blink. - A gcd-first implementation with
long longto avoid multiplying inintfirst. - A brute multiple scan (same
12,18→36) and a live preview.
Overview
The lcm is the smallest common multiple of two integers. Computing it via gcd is standard, fast, and easy to justify algebraically.
Two programs
Gcd formula and a stepping lcm for 12 and 18.
Live preview
Nonnegative safe integers; uses gcd and wide product in JavaScript.
Rigor
lcm(0, ·), overflow, and dividing by gcd before the final multiply.
Prerequisites
Euclidean gcd, integer division, and long long arithmetic.
- Basic PHP syntax, functions, loops, and
echo. - Optional: type casting and guard checks for safe integer operations.
What is lcm?
For positive integers a and b, lcm(a,b) is the smallest positive integer m with a | m and b | m.
Multiples of 12 include 12, 24, 36, …; multiples of 18 include 18, 36, …. The first common positive multiple is 36 (the reference answer).
gcd–lcm identity
For nonnegative a, b, gcd(a,b) · lcm(a,b) = a · b. Equivalently lcm(a,b) = a / gcd(a,b) · b when gcd(a,b) > 0.
12 and 18gcd(12,18)=6, so lcm = 12/6 · 18 = 2 · 18 = 36.
Intuition
- Check
216 / 36 = 6 = gcd
Takeaway: lcm sits at the intersection of the two multiple ladders; gcd measures overlap in prime factors.
Live preview
Nonnegative integers in the JavaScript safe range. Uses gcd then a wide product (same identity as the PHP examples).
Algorithm
Goal: compute lcm(a,b) for nonnegative integers.
gcd
Use Euclid: gcd(a,b) = gcd(b, a mod b) until the second argument is 0.
Divide before multiply
Return (a / g) * b in a wide type, or 0 if either input is 0.
📜 Pseudocode
function gcd(a, b): // nonnegative
while b != 0:
(a, b) = (b, a mod b)
return a
function lcm(a, b):
if a = 0 or b = 0:
return 0
g = gcd(a, b)
return (a / g) * blcm from gcd (12, 18)
Same numbers and output as the reference. Fixes the mislabeled findGCD.c header from the old page by using this project’s tut-code-header. Uses long long and divides by gcd before the final multiply.
<?php
function findGcd(int $num1, int $num2): int
{
$num1 = abs($num1);
$num2 = abs($num2);
while ($num2 !== 0) {
$temp = $num2;
$num2 = $num1 % $num2;
$num1 = $temp;
}
return $num1;
}
function findLcm(int $num1, int $num2): int
{
if ($num1 === 0 || $num2 === 0) {
return 0;
}
$gcd = findGcd($num1, $num2);
return intdiv(abs($num1), $gcd) * abs($num2);
}
$number1 = 12;
$number2 = 18;
$lcm = findLcm($number1, $number2);
echo "LCM of {$number1} and {$number2} is: {$lcm}\n";
?>Explanation
With g = 6, 12/6 = 2 and 2 · 18 = 36. This matches (12 · 18) / 6 = 216 / 6 without forming 12 * 18 in a narrow int first.
Brute scan along multiples
No explicit gcd function: walk multiples of max(a,b) until both divide. Same 12, 18 → 36. Slower in general but illustrates the definition.
<?php
function lcmByScan(int $a, int $b): int
{
$a = abs($a);
$b = abs($b);
if ($a === 0 || $b === 0) {
return 0;
}
$step = max($a, $b);
$candidate = $step;
while ($candidate % $a !== 0 || $candidate % $b !== 0) {
$candidate += $step;
}
return $candidate;
}
$number1 = 12;
$number2 = 18;
echo "LCM of {$number1} and {$number2} is: " . lcmByScan($number1, $number2) . "\n";
?>Explanation
Start at 18; it is not a multiple of 12. Add another 18 to reach 36, which both divide.
Prime factorization
Factor both numbers, take the maximum exponent per prime, multiply back—useful when you already have factor tables.
Interview: prefer gcd-based lcm; mention overflow and the lcm(0, ·) convention.
❓ FAQ
🔄 Input / output examples
Change number1 and number2 in either program.
| (a, b) | gcd | lcm |
|---|---|---|
(12, 18) | 6 | 36 |
(4, 6) | 2 | 12 |
(17, 13) | 1 | 221 |
(0, 9) | 9 | 0 |
Edge cases and pitfalls
The naive (a * b) / gcd in int can overflow even when the true lcm fits; dividing first helps but does not remove all overflow cases for huge inputs.
a == 0 or b == 0
This page returns 0 for lcm; gcd-based code must avoid dividing by 0.
Scan overflow
Repeatedly adding step can exceed INT_MAX; use wider counters for large inputs.
More arguments
Fold: lcm(a,b,c) = lcm(lcm(a,b), c) (watch zeros at each step).
Negative inputs
Normalize with absolute values if your API promises a positive lcm; keep modulus behavior consistent for negative inputs.
⏱️ Time and space complexity
| Method | Time | Extra space |
|---|---|---|
| gcd + formula | O(log min(a,b)) | O(1) |
| Brute scan | O(lcm / max(a,b)) steps worst case | O(1) |
The gcd-based method dominates in practice.
Summary
- Identity:
gcd(a,b) · lcm(a,b) = a · bfor nonnegativea, b. - Code: Euclid gcd, then
(long long)a / g * b(or return0if either input is zero). - Watch-outs:
intproduct overflow, brute scan overflow, and lcm with a zero argument.
For nonnegative integers a and b, gcd(a,b) · lcm(a,b) = a · b (with the convention lcm(a,0)=lcm(0,b)=0). That identity is why one small gcd loop unlocks lcm.
8 people found this page helpful
