GCD and LCM Euclidean Algorithm in DSA Javascript - Time & Space Complexity
We want to understand how fast the Euclidean algorithm finds the greatest common divisor (GCD) of two numbers.
How does the number of steps change as the input numbers get bigger?
Analyze the time complexity of the following code snippet.
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
This code finds the GCD using the Euclidean algorithm and then calculates the LCM using the GCD.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop repeatedly calculates the remainder (a % b).
- How many times: The loop runs until b becomes zero, which depends on the size of the inputs but reduces quickly each step.
Each step reduces the size of the numbers significantly, so the number of steps grows slowly compared to input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5-6 steps |
| 100 | About 7-8 steps |
| 1000 | About 10-11 steps |
Pattern observation: The steps grow roughly proportional to the number of digits, not the number itself.
Time Complexity: O(log min(a, b))
This means the number of steps grows slowly, roughly with the number of digits of the smaller input.
[X] Wrong: "The Euclidean algorithm takes as many steps as the size of the input numbers."
[OK] Correct: Each step reduces the numbers quickly, so the steps grow much slower than the input size.
Understanding this algorithm's time complexity shows you can analyze efficient number-based algorithms, a useful skill in many coding challenges.
"What if we used subtraction instead of remainder in the loop? How would the time complexity change?"
Practice
a and b?Solution
Step 1: Understand the Euclidean algorithm purpose
The Euclidean algorithm is a method to find the greatest common divisor (GCD) of two numbers by repeatedly applying the remainder operation.Step 2: Differentiate GCD from LCM
While GCD finds the largest number dividing both, LCM finds the smallest number divisible by both. Euclidean algorithm is for GCD.Final Answer:
The greatest common divisor (GCD) ofaandb-> Option DQuick Check:
Euclidean algorithm = GCD [OK]
- Confusing GCD with LCM
- Thinking it calculates sum or difference
- Assuming it finds prime factors directly
a and b?Solution
Step 1: Analyze the correct Euclidean algorithm loop
function gcd(a, b) { while(b !== 0) { let temp = b; b = a % b; a = temp; } return a; } uses a while loop that continues untilbis zero, swapping values and using modulo to find remainder, which matches the Euclidean algorithm.Step 2: Check other options for correctness
function gcd(a, b) { return a + b; } returns sum, not GCD. function gcd(a, b) { return Math.min(a, b); } returns minimum, not GCD. function gcd(a, b) { while(a !== 0) { a = b % a; b = a; } return b; } has incorrect variable updates causing infinite loop or wrong result.Final Answer:
function gcd(a, b) { while(b !== 0) { let temp = b; b = a % b; a = temp; } return a; } -> Option BQuick Check:
Correct Euclidean loop = function gcd(a, b) { while(b !== 0) { let temp = b; b = a % b; a = temp; } return a; } [OK]
- Returning sum or min instead of GCD
- Incorrect variable swapping causing infinite loops
- Using wrong loop conditions
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
console.log(gcd(48, 18));Solution
Step 1: Trace the Euclidean algorithm steps for gcd(48, 18)
48 % 18 = 12, then gcd(18, 12); 18 % 12 = 6, then gcd(12, 6); 12 % 6 = 0, then gcd(6, 0) = 6.Step 2: Return the last non-zero remainder
When second number becomes zero, the first number is the GCD, which is 6 here.Final Answer:
6 -> Option CQuick Check:
GCD(48,18) = 6 [OK]
- Stopping too early and returning wrong remainder
- Confusing modulo with division
- Mixing up order of parameters
function gcd(a, b) {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
console.log(lcm(4, 6));Solution
Step 1: Understand variable mutation in gcd function
The gcd function changesaandbinside the loop, so after calling gcd,aandbin lcm are modified.Step 2: Impact on lcm calculation
Sinceaandbare changed by gcd, the multiplicationa * bin lcm uses modified values, causing incorrect LCM.Final Answer:
The gcd function modifiesaandbdirectly, affecting lcm calculation -> Option AQuick Check:
Mutating inputs in gcd breaks lcm formula [OK]
- Assuming gcd doesn't change inputs
- Using addition instead of multiplication in LCM
- Returning wrong variable from gcd
[12, 15, 20], which JavaScript function correctly computes the LCM of all elements using the Euclidean algorithm for GCD?Solution
Step 1: Verify gcd and lcm functions correctness
function gcd(a, b) { while(b !== 0) { let t = b; b = a % b; a = t; } return a; } function lcm(a, b) { return (a * b) / gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); uses correct Euclidean gcd and calculates lcm as (a*b)/gcd(a,b), which is mathematically correct.Step 2: Check lcmArray function logic
It uses reduce to apply lcm pairwise across the array, correctly computing LCM of all elements.Step 3: Validate other options
function gcd(a, b) { return a + b; } function lcm(a, b) { return a * b; } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); uses incorrect gcd and lcm formulas. function gcd(a, b) { while(a !== 0) { a = b % a; b = a; } return b; } function lcm(a, b) { return (a + b) / gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); has wrong gcd and lcm formulas. function gcd(a, b) { while(b !== 0) { let t = b; b = a % b; a = t; } return a; } function lcm(a, b) { return (a * b) * gcd(a, b); } function lcmArray(arr) { return arr.reduce((acc, val) => lcm(acc, val)); } console.log(lcmArray([12, 15, 20])); incorrectly multiplies gcd in lcm formula.Final Answer:
Option A code correctly computes LCM of array elements -> Option AQuick Check:
Correct gcd, lcm, and reduce for array LCM [OK]
- Incorrect gcd or lcm formulas
- Not using reduce to combine array elements
- Multiplying gcd instead of dividing in lcm
Ready to prove it?
Pass the test and this pattern moves unknown
