1. Until remainder zero
Repeat until the second value becomes zero.
while (b !== 0)Typorithm · easy
Step 1 of 8 · intro card
Math & Number Theory
Euclid's algorithm: replace (a, b) with (b, a mod b) until b is zero; a is the GCD.
The GCD of two numbers also divides their remainder, so a mod b preserves it.
Each step shrinks the pair quickly, reaching zero in O(log min(a, b)) iterations.
LCM follows directly: a / gcd(a, b) * b, dividing before multiplying to stay safe.
Replace (a, b) with (b, a mod b).
Stop when b is zero.
lcm = a / gcd * b.
function gcd(a, b) {
while (b !== 0) {
[a, b] = [b, a % b];
}
return a;
}
function lcm(a, b) {
return (a / gcd(a, b)) * b;
}Connect each part of the code to the concept before you type.
Repeat until the second value becomes zero.
while (b !== 0)Replace (a, b) with (b, a mod b) — the core of Euclid's algorithm.
[a, b] = [b, a % b]When b hits zero, a holds the greatest common divisor.
return alcm(a, b) = a / gcd(a, b) * b, dividing first to avoid overflow.
return (a / gcd(a, b)) * bRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputGCD of 48 and 18
Follow temporary values, local state and supporting structures at this step.
No temporary values have been saved in this frame yet.
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state