GCD and LCM Euclidean Algorithm in DSA Python - 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.
def gcd(a: int, b: int) -> int:
while b != 0:
a, b = b, a % b
return a
# LCM can be found using GCD:
def lcm(a: int, b: int) -> int:
return a * b // gcd(a, b)
This code finds the GCD of two numbers 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 replaces the pair (a, b) with (b, a % b).
- How many times: The loop runs until b becomes zero, which depends on how quickly the remainder shrinks.
Each step reduces the size of the numbers roughly by at least one digit, so the number of steps grows slowly as numbers get bigger.
| Input Size (digits) | Approx. Steps |
|---|---|
| 2 (e.g., 99) | 5-7 |
| 4 (e.g., 9999) | 15-20 |
| 6 (e.g., 999999) | 30-40 |
Pattern observation: The steps grow roughly proportional to the number of digits, not the number size itself.
Time Complexity: O(log(min(a, b)))
This means the number of steps grows roughly with the number of digits in the smaller input number.
[X] Wrong: "The Euclidean algorithm takes time proportional to the size of the numbers themselves (like a or b)."
[OK] Correct: The algorithm works on remainders, which shrink quickly, so the steps depend on the number of digits (logarithm), not the full number size.
Understanding this algorithm's time complexity shows you can analyze efficient number-based algorithms, a useful skill for many coding problems.
"What if we used subtraction instead of modulo in the GCD calculation? How would the time complexity change?"
Practice
Solution
Step 1: Understand the Euclidean algorithm purpose
The Euclidean algorithm is designed 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 DQuick Check:
Euclidean algorithm = GCD [OK]
- Confusing GCD with LCM
- Thinking it calculates sum or difference
- Assuming it finds prime factors
Solution
Step 1: Analyze recursive base case and recursive call
The base case is when b == 0, returning a. Otherwise, call gcd with parameters (b, a % b) to reduce the problem.Step 2: Check each option's correctness
def gcd(a, b): return a if b == 0 else gcd(b, a % b) matches the correct recursive Euclidean algorithm syntax. def gcd(a, b): return b if a == 0 else gcd(a, b % a) swaps parameters incorrectly. def gcd(a, b): while b != 0: a, b = b, a % b return a is iterative, not recursive. def gcd(a, b): return a + b if b == 0 else gcd(b, a % b) incorrectly adds a and b.Final Answer:
def gcd(a, b): return a if b == 0 else gcd(b, a % b) -> Option BQuick Check:
Recursive gcd uses b == 0 base and gcd(b, a % b) call [OK]
- Swapping parameters incorrectly
- Using addition instead of modulo
- Confusing recursive and iterative syntax
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
print(gcd(48, 18))Solution
Step 1: Trace the while loop iterations
Start with a=48, b=18. Iteration 1: a=18, b=48%18=12. Iteration 2: a=12, b=18%12=6. Iteration 3: a=6, b=12%6=0. Loop ends.Step 2: Return the final value of a
When b=0, return a=6, which is the GCD of 48 and 18.Final Answer:
6 -> Option AQuick Check:
GCD(48,18) = 6 [OK]
- Returning b instead of a
- Confusing modulo with division
- Stopping loop too early
def gcd(a, b):
while b:
a, b = b, a % b
return a
def lcm(a, b):
return a * b // gcd(a, b)
print(lcm(12, 15))Solution
Step 1: Check gcd function correctness
The gcd function uses while b (equivalent to while b != 0), swapping a and b and updating b with a % b. This is correct.Step 2: Verify lcm formula correctness
LCM is calculated as (a * b) divided by gcd(a, b), which is mathematically correct.Final Answer:
No error; output is correct -> Option AQuick Check:
LCM formula = (a*b)//GCD(a,b) [OK]
- Using addition instead of division in LCM
- Incorrect loop condition in gcd
- Swapping parameters unnecessarily
Solution
Step 1: Calculate GCD of 21 and 6 using Euclidean algorithm
21 % 6 = 3, then 6 % 3 = 0, so GCD is 3.Step 2: Calculate LCM using formula
LCM = (21 * 6) // 3 = 126 // 3 = 42.Final Answer:
42 -> Option CQuick Check:
LCM = (21*6)//GCD(21,6) = 42 [OK]
- Calculating LCM as product without dividing by GCD
- Mixing up GCD and LCM values
- Incorrect modulo calculations
Ready to prove it?
Pass the test and this pattern moves unknown
