Home PHP Interview Programs GCD PHP GCD of Two Numbers Program Definition
What Is GCD? The GCD (Greatest Common Divisor), also called HCF , is the largest positive integer that divides both numbers with no remainder.
gcd(a, b) = largest d > 0 such that
a % d == 0 and b % d == 0
Example: gcd(48, 18) = 6
48 = 6 * 8
18 = 6 * 3
No larger positive integer divides both a b gcd(a, b) 481861713102121-12186
Approach
How to Find GCD in PHP Four clear steps — Euclid’s remainder loop is the interview default.
Take absolute values of $a and $b with abs(). While $b !== 0, replace ($a, $b) with ($b, $a % $b). When $b becomes 0, return $a — that is the GCD. Optionally derive LCM with abs($a / gcd * $b). Method Idea Time Best for Iterative Euclid Loop with $a % $b until $b === 0 O(log min)Interviews, production default Recursive Euclid gcd($b, $a % $b) until base caseO(log min)Showing the recurrence LCM via GCD |a / gcd * b|O(log min)Follow-up questions
Pseudocode function Gcd(a, b):
a = abs(a)
b = abs(b)
while b != 0:
temp = b
b = a mod b
a = temp
return a
function Lcm(a, b):
if a == 0 or b == 0:
return 0
return abs(a / Gcd(a, b) * b) Reference
Cheat Sheet Keep these facts handy while writing the program.
Topic Detail Core step (a, b) ← (b, a % b)Stop when b === 0; answer is aSigns Use abs() first gcd(0, n) |n| for n !== 0gcd(0, 0) Usually defined as 0 LCM link lcm = |a / gcd * b|Classic check gcd(48, 18) === 6
Try it
Live Preview Enter two integers to see the Euclidean remainder chain and the GCD.
Trace
Worked Walkthrough — gcd(48, 18) Apply Euclid’s step until the remainder is zero.
Start: a = 48, b = 18
Step 1: 48 % 18 = 12 → (18, 12)
Step 2: 18 % 12 = 6 → (12, 6)
Step 3: 12 % 6 = 0 → (6, 0)
Stop (b === 0)
gcd(48, 18) = 6
Check: 48 / 6 = 8, 18 / 6 = 3 Code
PHP Programs Start with the iterative loop — it is the clearest interview answer. Then show the recursive form, and finish with LCM built from GCD.
1. Iterative Euclid 2. Recursive Euclid 3. LCM via GCD Example 1 — Iterative Euclidean Algorithm Loop with remainders until the second value becomes zero.
<?php
function gcd(int $a, int $b): int {
$a = abs($a);
$b = abs($b);
while ($b !== 0) {
$temp = $b;
$b = $a % $b;
$a = $temp;
}
return $a;
}
$number1 = 48;
$number2 = 18;
echo "GCD of " . $number1 . " and " . $number2 . " is: " . gcd($number1, $number2) . "\n";
?> How It Works Take absolute values so negatives do not break the remainder chain. While $b !== 0, store $b, set $b = $a % $b, then set $a to the old $b. For (48, 18) the pairs become (18, 12), (12, 6), then (6, 0). When $b is 0, return $a — here 6. Extra space is O(1); time is logarithmic in the smaller number. Example 2 — Recursive Euclidean Algorithm Same remainder idea, written as a recurrence with a clear base case.
<?php
function gcd(int $a, int $b): int {
$a = abs($a);
$b = abs($b);
if ($b === 0) {
return $a;
}
return gcd($b, $a % $b);
}
$number1 = 48;
$number2 = 18;
echo "GCD of " . $number1 . " and " . $number2 . " is: " . gcd($number1, $number2) . "\n";
?> How It Works Base case: if $b === 0, return $a. Otherwise recurse on gcd($b, $a % $b). Calls for (48, 18): (18, 12) → (12, 6) → (6, 0) → return 6. Same answer as the loop; stack depth is about O(log min(a, b)). Prefer the iterative version when you must mention space carefully. Example 3 — LCM via GCD Use the identity lcm(a, b) = |a / gcd * b| after finding the GCD.
<?php
function gcd(int $a, int $b): int {
$a = abs($a);
$b = abs($b);
while ($b !== 0) {
$temp = $b;
$b = $a % $b;
$a = $temp;
}
return $a;
}
function lcm(int $a, int $b): int {
if ($a === 0 || $b === 0) {
return 0;
}
$g = gcd($a, $b);
return abs(intdiv($a, $g) * $b);
}
$a = 48;
$b = 18;
echo "gcd = " . gcd($a, $b) . "\n";
echo "lcm = " . lcm($a, $b) . "\n";
?> How It Works Reuse the same iterative gcd helper. If either input is 0, LCM is defined as 0 here. Divide one value by the GCD first: intdiv($a, $g) * $b reduces product size risk. Wrap with abs() so the LCM is nonnegative. Check: 48 / 6 * 18 = 8 * 18 = 144, and 144 / 48 = 3, 144 / 18 = 8. Edge Cases & Pitfalls Check these before calling the solution done.
Zero gcd(0, n) For n !== 0, the answer is |n|. Euclid already handles this.
Both zero gcd(0, 0) Conventionally return 0. Say your choice out loud in interviews.
Negatives Use abs() PHP % keeps the dividend’s sign. Normalize first for a nonnegative GCD.
LCM Divide before multiply Prefer intdiv($a, $gcd) * $b over $a * $b / $gcd when values are large.
Analysis
Time and Space Complexity Program Time Extra space Iterative Euclid O(log min(a, b))O(1)Recursive Euclid O(log min(a, b))O(log min(a, b))LCM via GCD O(log min(a, b))O(1)
Worst-case Euclid pairs are consecutive Fibonacci numbers, but the step count still grows logarithmically.
Remember
Key Takeaways Definition: largest positive integer that divides both values.
Euclid: replace (a, b) with (b, a % b) until b === 0.
LCM: |a / gcd * b| after finding the GCD.
Test set: (48, 18) → 6; (17, 13) → 1; (0, 21) → 21.
One line: keep taking remainders — when the remainder hits zero, the other value is the GCD.
Frequently Asked Questions What is the GCD of two integers? The greatest common divisor (also called HCF) is the largest positive integer that divides both numbers without a remainder.
How does Euclid's algorithm work? Replace (a, b) with (b, a % b) until b becomes 0. Then a is the GCD. Always take absolute values first.
What is gcd(0, n)? For n != 0, gcd(0, n) equals |n|. By convention, many libraries define gcd(0, 0) as 0.
Iterative or recursive? Both are correct. Prefer the iterative loop in interviews for O(1) extra space; recursion uses O(log min(a,b)) stack depth.
How is LCM related to GCD? For nonzero a and b: lcm(a, b) = |a * b| / gcd(a, b). Compute GCD first and divide before multiplying when values are large.
CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion