GCD and LCM Euclidean Algorithm in DSA Typescript - Time & Space Complexity
We want to understand how fast the Euclidean algorithm finds the greatest common divisor (GCD) of two numbers.
This helps us know how the steps grow when numbers get bigger.
Analyze the time complexity of the following code snippet.
function gcd(a: number, b: number): number {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
function lcm(a: number, b: number): number {
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, reducing the numbers each time.
Each step reduces the size of the numbers significantly, so the number of steps grows slowly even if numbers get large.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 5 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The steps grow roughly with the number of digits, not the number size itself.
Time Complexity: O(log min(a, b))
This means the steps grow slowly, about proportional to the number of digits in the smaller number.
[X] Wrong: "The Euclidean algorithm takes as many steps as the size of the numbers."
[OK] Correct: Actually, each step reduces the numbers quickly, so the steps grow with the number of digits, not the full number size.
Understanding this algorithm's time complexity shows you can analyze efficient math-based solutions, a useful skill for many coding problems.
What if we used a recursive version of the Euclidean algorithm? How would the time complexity change?
Practice
Solution
Step 1: Understand the Euclidean algorithm purpose
The Euclidean algorithm is a method to find the greatest common divisor (GCD) of two integers by repeatedly applying division and remainder operations.Step 2: Differentiate GCD from LCM
While GCD finds the largest number dividing both inputs, LCM finds the smallest number divisible by both. The Euclidean algorithm is specifically for GCD.Final Answer:
The Greatest Common Divisor (GCD) -> Option CQuick Check:
Euclidean algorithm = GCD [OK]
- Confusing GCD with LCM
- Thinking it calculates sum or difference
- Assuming it finds prime factors
Solution
Step 1: Check parameter types for GCD function
GCD requires two numbers as input, so parameters must be two numbers.Step 2: Check return type
The function returns a number representing the GCD, so return type must be number.Final Answer:
function gcd(a: number, b: number): number -> Option AQuick Check:
Two number inputs and number output [OK]
- Using string types for numeric operations
- Missing one parameter
- Returning void instead of number
function gcd(a: number, b: number): number {
while (b !== 0) {
const 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, loop ends, gcd is 6.Step 2: Confirm final returned value
The function returns 6 after the loop ends.Final Answer:
6 -> Option DQuick Check:
GCD(48,18) = 6 [OK]
- Returning the wrong variable after loop
- Confusing modulo operation
- Stopping too early in the loop
function gcd(a: number, b: number): number {
while (a !== 0) {
const temp = a;
a = b % a;
b = temp;
}
return b;
}Solution
Step 1: Test the function with example inputs
Trace gcd(48, 18): starts a=48 b=18 -> a=18 b=48 -> a=12 b=18 -> a=6 b=12 -> a=0 b=6, returns 6 (correct GCD).Step 2: Verify algorithm correctness
This is a valid variant of the Euclidean algorithm with swapped roles (gcd(a,b) = gcd(b % a, a)). No syntax/runtime errors, handles edges like gcd(48,0)=48 correctly.Final Answer:
No error, function works correctly -> Option BQuick Check:
Equivalent Euclidean variant computes correct GCD [OK]
- Assuming loop must check b !== 0
- Proposing to change return to a (breaks it)
- Suggesting / instead of %
Solution
Step 1: Recall the formula relating LCM and GCD
LCM(a, b) = (a * b) / GCD(a, b). This formula uses the GCD to find the LCM efficiently.Step 2: Check each code snippet against the formula
const lcm = (a: number, b: number): number => (a * b) / gcd(a, b); matches the formula exactly. Options B, C, and D do not correctly calculate LCM.Final Answer:
const lcm = (a: number, b: number): number => (a * b) / gcd(a, b); -> Option AQuick Check:
LCM = (a*b)/GCD(a,b) [OK]
- Dividing GCD by product instead of product by GCD
- Adding or subtracting instead of dividing
- Using max instead of LCM formula
Ready to prove it?
Pass the test and this pattern moves unknown
